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
Commit cfd8ea2
Browse filesBrowse the repository at this point in the historyBrowse files
Reserve room for update() and merge() only when keys are added (#1755)
<!-- Thank you for your contribution! -->
## What do these changes do?
`update()` and `merge()` reserved room for every item of their argument
before the batch, so an update of keys already present rebuilt the table
on every call, about 15% of its cost. Like `dict.update()`, they now
grow up front only a table that could not hold the argument even empty,
or one with holes from deletions (a rebuild inside the batch would have
to keep them). Otherwise the first added key makes room for the whole
batch: `update_marks_t.reserve` carries the size, and the add path tests
`usable <= reserve`, which folds the pending check into the existing
full-table test. A rebuild during a batch now takes the compacting path
when the table has no holes, since compaction then keeps every index.
The pure-Python backend got the same sizing, so `sys.getsizeof()` still
agrees between the two.
## Are there changes in behavior for the user?
No, apart from a smaller table after an update of keys that were already
present.
## Is it a substantial burden for the maintainers to support this?
No; it is a sizing rule in `md_reserve_batch()` plus one field in
`update_marks_t`.
## Related issue number
N/A
## Checklist
- [x] I think the code is well written
- [x] Unit tests for the changes exist
- [x] Documentation reflects the changes
- [ ] If you provide code modification, please add yourself to
`CONTRIBUTORS.txt` N/A
- [x] Add a new news fragment into the `CHANGES/` folder
- [x] `make doc-spelling` passes and any new technical words are added
to `docs/spelling_wordlist.txt`
Drafted with Claude Code (Claude Opus 5.5); reviewed by @asvetlov.
<details>
<summary>Agent run details (optional, for reviewers)</summary>
Callgrind, `PYTHONHASHSEED=0`, per call with a 100-item argument (N=120
minus N=20 runs), base fb828e3, CPython 3.14.7 and 3.14.7t:
| case | GIL md / ci | FT md / ci |
| --- | --- | --- |
| update(dict), keys present | -16.6% / -21.4% | -15.6% / -21.2% |
| update(list), keys present | -15.2% / -19.9% | -13.1% / -18.1% |
| update(multidict), keys present | -17.8% / -25.0% | -15.3% / -23.1% |
| merge(dict), keys present | -23.6% / -28.1% | -24.8% / -29.4% |
| update/merge into an empty multidict | +0.2..+0.4% | +0.3% |
| update/merge of 100 new keys into 100 | +0.1..+0.6% | +0.5..+0.6% |
| update with duplicate keys | +0.4..+0.9% | +0.6..+0.7% |
The adding paths pay about 1 instruction per add for the folded reserve
check.
Tests: full suite passes on the C extension (3.14.7 GIL, 3.14.7t FT,
3.10.17, 3.14.7t debug build) and on pure Python
(`MULTIDICT_NO_EXTENSIONS=1 pytest --no-c-extensions` from a clean tree
copy). `test_update_of_present_keys_keeps_the_table` fails on the base.
`tools/check_inlining.py` passes on 3.14.7 and 3.14.7t. `make
doc-spelling` passes.
</details>
0 commit comments