You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
Speed up set operations on common built-in keys #162782
interpreter-core(Objects, Python, Grammar, and Parser dirs)pendingThe issue will be closed if no feedback is providedperformancePerformance or resource usagetype-featureA feature request or enhancement
Set membership and insertion can dispatch through type slots even when an exact compact integer has a directly computable hash or a string or tuple already has a cached hash. Equal but distinct compact integer keys also reach generic rich comparison. Can we shorten these paths while preserving subclass behavior and the existing rules for comparisons that mutate a set?
Found and written by an AI optimization pipeline I run; I reviewed every line, ran the tests and sanitizers, and can answer for it.
Quick follow-up! I opened this based on a plain -O2 build, where the int set micros looked great and meteor_contest was about 3% faster. I then rebuilt base and patched with --enable-optimizations --with-lto (PROFILE_TASK="-m test --pgo --randseed 4109", Apple M4 Pro, Clang 21.0.0). The int paths are still faster there, but str and tuple lookups slip a bit (bm_set_ops_micro.py, -p 20 -w 1 -n 3, PYTHONHASHSEED=1):
The full pyperformance run is flat (geometric mean 1.00x slower, 0.9975x), and meteor_contest went from 48.8 ms -> 49.4 ms, so the -O2 win is gone. The str/tuple cost comes from the patch itself: set_keys_eq_fast checks PyLong_CheckExact before PyUnicode_CheckExact, and set_hash_key adds type branches ahead of PyObject_Hash. My guess is that PGO+LTO already gets most of what the manual inlining was buying, so all that's left is the extra branches. I haven't checked the generated code to confirm that, though.
interpreter-core(Objects, Python, Grammar, and Parser dirs)pendingThe issue will be closed if no feedback is providedperformancePerformance or resource usagetype-featureA feature request or enhancement
Activity
picnixz commentedon Nov 28, 2026
Please provide benchmarks, pyperformance results, and use PGO/LTO (same story as for your other issues).
josh-hurst commentedon Nov 28, 2026
Quick follow-up! I opened this based on a plain
-O2build, where the int set micros looked great andmeteor_contestwas about 3% faster. I then rebuilt base and patched with--enable-optimizations --with-lto(PROFILE_TASK="-m test --pgo --randseed 4109", Apple M4 Pro, Clang 21.0.0). The int paths are still faster there, but str and tuple lookups slip a bit (bm_set_ops_micro.py,-p 20 -w 1 -n 3,PYTHONHASHSEED=1):The full pyperformance run is flat (geometric mean 1.00x slower, 0.9975x), and
meteor_contestwent from 48.8 ms -> 49.4 ms, so the-O2win is gone. The str/tuple cost comes from the patch itself:set_keys_eq_fastchecksPyLong_CheckExactbeforePyUnicode_CheckExact, andset_hash_keyadds type branches ahead ofPyObject_Hash. My guess is that PGO+LTO already gets most of what the manual inlining was buying, so all that's left is the extra branches. I haven't checked the generated code to confirm that, though.pyperformance, 57 zero-dep benchmarks (PGO+LTO)
Honestly, flat overall and a bit slower for str and tuple keys isn't worth keeping open, so I'll close this. Thanks for taking a look!