Repository navigation
[mypyc] Hash fixed-length tuples without building a tuple object - #22440
Merged
Merged
Conversation
Collaborator
|
Can you look at the 32-bit build, as it's failing? Otherwise this looks great, and helps with a very common use case. It's also nice that we don't need Python version specific code any more. |
Collaborator
|
Hmm actually the 32-bit build failure seems unrelated. |
JukkaL
approved these changes
Nov 12, 2026
Contributor
Author
|
@JukkaL Yeah, just fyi, it seemed like there were issues with that pipeline. All of my PRs failed on 32-bit, and others did as well. My plan was to try to re-run again today 👍 EDIT: Either way, thanks for reviewing and merging! |
Collaborator
|
This will likely be a really nice performance improvement for mypy itself, thank you! |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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.
Part of mypyc/mypyc#632
hash(t)of a fixed-length tuple boxed the tuple and then calledPyObject_Hash()on it. That allocates a tuple, and a new object for each item that's an unboxed value (most ints, and every float), only to hash them.Now the hash is worked out from the hashes of the items, with the same steps as
tuple_hash()inObjects/tupleobject.c(CPyTupleHash_Start(),CPyTupleHash_Add()andCPyTupleHash_Finish()inCPy.h). The algorithm and its constants are the same in every Python version mypyc supports: it's been based on xxHash since 3.8.The hashes don't change: everything other than ints, bools and nested tuples still goes through
PyObject_Hash(), and only the tuple object isn't built. The other half of #632, thathash()returns anint, which needs boxing for about half of all tuple hashes, is unchanged (mypyc/mypyc#922 is about that too).Performance
A compiled loop (
total ^= hash(...), 200,000 iterations; the loop and the xor are included). Each number is the best of 20 rounds, with master and this PR built as two modules and timed in turn in one process:hash((i, i + 1000, i * 3, -i, 7))hash((i, -i))hash((o, i, 5))witho: Anyastrhash((j, s))withj: i64,s: strhash((x, x + 1.0))withx: floathash(p), where__hash__returnshash((self.x, self.y, self.z))Float items are still boxed to be hashed, so that case gains the least.