Skip to content

Cache hash value for FrozenSets #5409

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Sep 23, 2024

Conversation

crazymerlyn
Copy link
Contributor

Adds a hash field to the PyFrozenSet data type in order to avoid recomputing the hash of an immutable object.

Similar to the way it's done in cpython but the hash value is stored in the PyFrozenSet struct instead of the inner Set datatype.

Massively speeds up the test_hash_effectiveness test and allows using the same limit of 2**18 elements on the test as cpython.

RustPython is still about 5x slower than python at running this test (0.5s vs 2.5s on my machine).

Of course, in practical use cases repeatedly hashing the same frozenset is not common so I doubt this is a performance win in general. Just adding this to be consistent with cpython.

Fixes #5167

Adds a hash field to the `PyFrozenSet` data type in order to avoid
recomputing the hash of an immutable object.
Copy link
Member

@youknowone youknowone left a comment

Choose a reason for hiding this comment

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

👍

@coolreader18 coolreader18 merged commit bbf7aac into RustPython:main Sep 23, 2024
11 checks passed
@crazymerlyn crazymerlyn deleted the cache-frozenset-hash branch September 23, 2024 22:48
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Investigate test_set.TestFrozenSet.test_hash_effectiveness performance
3 participants