Skip to content

Conversation

@AlexWaygood
Copy link
Member

Summary

This PR changes the DebugStatistics and ReleaseStatistics structs so that they implement a common StatisticsRecorder trait, and makes the KeyValueCache struct generic over a type parameter bound to that trait. The advantage of this approach is that it's much harder for the DebugStatistics and ReleaseStatistics structs to accidentally grow out of sync in the methods that they implement, which was the cause of the release-build failure recently fixed in #11177.

Test Plan

cargo test -p red_knot and cargo build --release both continue to pass for me locally

@AlexWaygood AlexWaygood added the internal An internal refactor or improvement label Apr 27, 2024
Comment on lines 10 to 16
pub struct KeyValueCache<K, V, S = DefaultStatisticsRecorder>
where
S: StatisticsRecorder,
{
map: FxDashMap<K, V>,
statistics: CacheStatistics,
statistics: S,
}
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I like the trait, but making KeyValueCache generic over S feels heavyweight because it increases the complexity of using the type (yes, it defaults to a type, but you still get scared away by three generics when looking at the type).

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Makes sense. Is there a way of using a trait here without making KeyValueCache generic? If not, I'm not wedded to this approach -- happy to do something else with this, or not do anything at all!

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I suppose I could do statistics: Box<dyn StatisticsRecorder>?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You could, but that requires an allocation and adds an overhead to cache lookups (even when it's the release statistics).

I think we should not over-engineer this. We might even go with always having statistics. This is just some prototype code.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Makes sense. Should I just close this for now, then? :-)

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I like the trait. I would just remove the type param.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ohh, I think I see what you mean. Sorry for being dense.

@github-actions
Copy link
Contributor

github-actions bot commented Apr 27, 2024

ruff-ecosystem results

Linter (stable)

✅ ecosystem check detected no linter changes.

Linter (preview)

✅ ecosystem check detected no linter changes.

@AlexWaygood AlexWaygood requested a review from MichaReiser April 27, 2024 17:40
@carljm carljm removed their request for review April 29, 2024 23:49
@carljm
Copy link
Contributor

carljm commented Apr 29, 2024

Going to let @MichaReiser confirm if this looks ok.

@AlexWaygood AlexWaygood merged commit eb6f562 into astral-sh:main Apr 30, 2024
@AlexWaygood AlexWaygood deleted the redknot-trait branch April 30, 2024 06:14
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

internal An internal refactor or improvement

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants