Repository navigation
Speed up key lookups and replacements in tables - #704
DicksonWu654 wants to merge 2 commits into
Conversation
Replacing an existing key scanned every key in the container to recover the stored Key object, so updating many keys of a large table was quadratic. Take the stored key from the body slot that _map already points to instead. Looking up a plain str key built and validated a temporary SingleKey just to probe _map. SingleKey hashes and compares like its string, so probe _map with the string directly. Containers that have ever held a custom Key implementation keep the previous normalization and comparison scan, since such keys may not accept a str operand. Missing key errors still report the normalized key. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
|
🛡️ Human Attestation required. A human needs to confirm this PR once before it can merge. Attest this PR — sign in with GitHub, pass the challenge, and confirm one of the statements. |
If we are to ignore this case, can it be further simplified, like using |
A container only stores SingleKeys (dotted keys are split before they are stored), so plain str queries can always probe _map directly, and replacing a key can always take the stored key from its body slot. This removes the _has_custom_keys flag and the comparison scan it guarded. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@frostming Yes. I've dropped that case in 05a8246, which removes the I kept |
Summary
Two costs in
Containergrow with the size of the table rather than with the key being accessed:Container.__setitem__on an existing key rannext(filter(lambda k: k == key, self._map))to recover the storedKeyobject, comparing the query against every key in the table. Updating every key of a table therefore takes quadratic time._mapalready points to the key's body slot, and that slot holds the stored key, so take it from there.item()and__contains__turned everystrquery into aSingleKey(which runs key-type detection and builds the quoted form) just to probe_map.SingleKeyhashes and compares like its string, so a plainstrcan probe_mapdirectly. On a miss,NonExistentKeyis still built from aSingleKey, so error messages are unchanged.A container stores only
SingleKeys, since dotted keys are split intoSingleKeys before they are stored. User-definedKeyimplementations are not given special handling.There is no new state, and no change to the public API or pickle format.
Benchmarks
Master is
55d0768. Each pair is two fresh processes pinned to one core withtaskset, with the master-first / branch-first order alternating. Each process warms up first (plus 2 s on PyPy) and reports its fastest call. The table shows the median of those times and the median of the per-pair ratios, with their range. The machine is a shared, busy 11-vCPU Linux box (WSL2), so the ranges are wide.name/versionof every package, uv.lockname/versionof every package, poetry.lockname/versionof every package, uv.lockThe two 3.12 rows for 2,000 edits, and the poetry.lock lookup row, average each process over at least 0.4 s instead of taking its fastest call.
[tool.poetry.dependencies]table with 2,000 entries, with a new version string assigned to every key in random order.uv.lock/poetry.lockfiles.Parse times, and edits to lock files whose tables have only a few keys, show no measurable change (the bottom rows).
Behaviour checks
ruff check,ruff format --checkandpyupgrade --py39-plusare clean.mypyreports the same errors as on master.tests/test_container_updates.pycover:str,strsubclasses andKeyqueries,unwrap()results, edit-then-dump output, and exact exception messages. It covered every TOML file intests/toml-testandtests/examplesplus real lock files. Earlier, roughly 17,000 generated documents and edit sequences on CPython 3.12 and 3.14 were also compared. No differences were found.Duplicate Check
_get_last_index_before_table/_insert_at). This PR only changes lookups and replacement of existing keys, so the two do not overlap.Agent Drafting Metadata
master. That re-check reran the full test suite on four interpreters, ran a separate differential check, and repeated the paired benchmarks above.🤖 Generated with Claude Code