Repository navigation
Conversation
Replaces CPython dict's perturbation-based probe sequence with linear probing for every step after the initial slot, in both the C extension and the pure Python implementation. This keeps collision chains in consecutive slots instead of scattering them across the table, which is friendlier to the cache; the effect is largest when many values are added under the same key.
Merging this PR will regress 3 benchmarks
|
Codecov Report✅ All modified and coverable lines are covered by tests. Additional details and impacted files@@ Coverage Diff @@
## master #1473 +/- ##
=========================================
Coverage 100.00% 100.00%
=========================================
Files 37 37
Lines 5792 5782 -10
Branches 489 489
=========================================
- Hits 5792 5782 -10
Flags with carried forward coverage won't be shown. Click here to find out more. ☔ View full report in Codecov by Harness. 🚀 New features to boost your workflow:
|
|
The experiment show bad result with pathological cases, when different keys land on the same slot |
What do these changes do?
Switches the internal hash table probe sequence, in both the C
extension and the pure Python implementation, from CPython dict's
perturbation formula to linear probing after the initial slot.
Collision chains land in consecutive slots instead of being
scattered across the table, which is friendlier to the cache; the
effect is largest when many values are added under the same key.
Are there changes in behavior for the user?
No, only performance. Iteration order is unaffected since it comes
from the separate entries list, not the probe sequence.
Is it a substantial burden for the maintainers to support this?
No. The change is contained to the probe-sequence step in
htkeys_build_indices,htkeys_find_empty_slotandhtkeysiter_next(C extension) and their pure Python counterparts,with no public API change.
Related issue number
N/A. Related to #1466, which targets the same slow path (many values
added under one key) with a different, more targeted technique: it
caches where the probe sequence's post-perturbation "tail" last ended
and resumes from there, leaving the general probe sequence untouched.
This PR is a simpler alternative offered for comparison; see the
benchmark note below.
Checklist
CONTRIBUTORS.txtN/A, file does not exist in this repoCHANGES/foldermake doc-spellingpasses and any new technical words are added todocs/spelling_wordlist.txtAgent run details (optional, for reviewers)
No new dedicated test was added; the existing suite (including the
hypothesis-marked property tests) already exercises every changedline under both backends, confirmed via the coverage report before
committing.
Tests:
pytest -qon master before the change and on this branch,both backends parametrized: 2020 passed, 10 skipped, both runs.
pytest -q -m hypothesis: 142 passed. Debug build withMULTIDICT_DEBUG_BUILD=1 MULTIDICT_ASAN_BUILD=1, ASan+UBSanpreloaded: 2009 passed, no sanitizer reports.
Lint:
pre-commit runon the changed files (ruff, clang-format,mypy x2): all passed.
make doc-spelling: passed.Benchmark (own microbenchmark, not the codspeed one from #1465,
wall clock, C extension, release build): building a
MultiDictfrom200 keys with 200
add()calls each (40000 inserts total, allcolliding within each key's 200-entry chain) went from ~20-24ms to
~6-8ms, roughly 3x. Unique-key workloads (build/get/iterate on 20000
distinct keys) were unchanged within noise. This is a constant-factor
cache-locality win; each insert is still O(chain length). By
contrast, #1466's own numbers show an asymptotic improvement for the
same scenario (2.2B to 6.8M instructions for 20000 values under one
key), since it stops re-walking already-placed entries entirely. The
two approaches are not mutually exclusive but this PR does not
attempt to combine them.
Drafted with Claude Code; reviewed by @asvetlov.