Skip to content

gh-162782: Speed up set key comparison and hashing - #162783

Closed
josh-hurst wants to merge 1 commit into
python:mainfrom
josh-hurst:perf/set-fast-key-ops
Closed

josh-hurst wants to merge 1 commit into
python:mainfrom
josh-hurst:perf/set-fast-key-ops

Conversation

@josh-hurst

Copy link
Copy Markdown

Speed up set lookups by comparing exact compact integers and strings directly and by reusing cached hashes for strings and tuples and computing compact integer hashes inline.

Across five alternating pairs on macOS arm64 at -O2, median base/patched ratios were 1.308× for integer membership, 1.278× for intersection, 1.327× for difference, and 2.027× for subset. Meteor measured 1.017×, within noise (two of five pairs were slower). Membership with Python-defined __hash__/__eq__ measured 0.980×, the cost of the extra exact-type checks.

Both debug builds passed the set suites and refleak checks. Free-threaded stress and ASan/UBSan also passed. The hash call sites may need a minor rebase with #161536.

Found and written by an AI optimization pipeline I run; I reviewed every line, ran the tests and sanitizers, and can answer for it.

Compare exact compact integers and strings directly when probing a
locked set or inserting a key. Reuse cached string and tuple hashes and
compute compact integer hashes inline.
@python-cla-bot

python-cla-bot Bot commented Nov 28, 2026 •

Copy link
Copy Markdown

All commit authors signed the Contributor License Agreement.

CLA signed

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants