Skip to content

[mypyc] Hash fixed-length tuples without building a tuple object - #22440

Merged
JukkaL merged 1 commit into
python:masterfrom
rheard:fix-mypyc-632-1
Nov 12, 2026
Merged

JukkaL merged 1 commit into
python:masterfrom
rheard:fix-mypyc-632-1

Conversation

@rheard

@rheard rheard commented Nov 11, 2026 •

Copy link
Copy Markdown
Contributor

Part of mypyc/mypyc#632

hash(t) of a fixed-length tuple boxed the tuple and then called PyObject_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() in Objects/tupleobject.c (CPyTupleHash_Start(), CPyTupleHash_Add() and CPyTupleHash_Finish() in CPy.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, that hash() returns an int, 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:

master this PR
hash((i, i + 1000, i * 3, -i, 7)) 179 ns 77 ns 2.32x
hash((i, -i)) 125 ns 72 ns 1.75x
hash((o, i, 5)) with o: Any a str 116 ns 76 ns 1.52x
hash((j, s)) with j: i64, s: str 109 ns 74 ns 1.47x
hash((x, x + 1.0)) with x: float 149 ns 128 ns 1.16x
hash(p), where __hash__ returns hash((self.x, self.y, self.z)) 204 ns 126 ns 1.62x

Float items are still boxed to be hashed, so that case gains the least.

@JukkaL

JukkaL commented Nov 12, 2026

Copy link
Copy Markdown
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.

@JukkaL

JukkaL commented Nov 12, 2026

Copy link
Copy Markdown
Collaborator

Hmm actually the 32-bit build failure seems unrelated.

@JukkaL
JukkaL merged commit 0a2a291 into python:master Nov 12, 2026
18 of 19 checks passed
@rheard

rheard commented Nov 12, 2026 •

Copy link
Copy Markdown
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!

@hauntsaninja

Copy link
Copy Markdown
Collaborator

This will likely be a really nice performance improvement for mypy itself, thank you!

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.

3 participants