Cache hash value for FrozenSets #5409
Merged
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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