Share guarded dict/set copies and avoid redundant dict work

Merged
JA
21d ago
+1805-457
  • Use guarded copy_table() for dict(existing_dict), dict.copy() and set storage copies, preserving cached hashes and insertion order. copy_as(kind, vm) selects the result kind and releases its factory on failure. dict() requests a plain kind; .copy() preserves Counter/defaultdict kinds and factory ownership. A shared kind-reference check keeps factories visible to GC.
  • Share the scan with dict-pair and set-update snapshots. Preflight nonempty storage, release partial results on failure, poll deadlines across physical slots and stop after the last live entry. Empty sources skip retained tombstones; dense scans omit tombstone bookkeeping and inline the fill callback.
  • Probe dict.setdefault() once and skip dict self-merges. Keyword overrides still apply; Counter self-updates still add counts. Guard get() and setdefault() arguments through hashing/equality callbacks.
  • Share set/frozenset operand preparation and algebra. frozenset.copy() returns a counted reference to the same object and preserves its cached hash.

Cached-hash dict copies can differ from CPython's sparse-table rebuilds, which may call key __eq__.
See docs/limitations/builtins.md and docs/limitations/copy.md.

Benchmarks

4e42b628 versus previous PR head 2765b9c0, locally on macOS arm64, Rust 1.99.0 release, fat LTO, one codegen unit:

WorkloadElapsed time change
1,000 self-updates of a 128-entry dict-97.8%
3,840 setdefault misses with heap-backed tuple keys-14.0%
500 sparse set copies-80.7%
500 updates from the sparse set-77.3%

The sparse source retains 2,048 slots, with only its first entry surviving.
These are paired process-median estimates from 12 independent pairs, 10 warmups and five samples; setup excluded.
Controls use 32 independent pairs, 30 warmups and seven samples.
Dense set copies remain near baseline; integer get() improves 3.9%, and heap-string lookups remain near baseline.
Empty-copy point estimates stay within roughly 1%.
Counter.most_common() on 128 entries is 1.1% slower (95% interval +0.08% to +1.54%).

The copy_as refactor, 36cd8f2c versus 1e96ded7, was measured separately across 17 workloads using 24 process pairs, 10 warmups and seven samples, with 48-pair confirmation controls and 30 warmups.
Plain/dense copies, Counter construction and lookup controls remain near baseline.
Empty Counter copies cost +1.44% (95% interval +0.80% to +1.90%) and empty defaultdict copies +1.18% (+0.75% to +1.68%), roughly one nanosecond per copy.

Validation

206 focused interpreter tests, 107 worker tests and 1,516 Monty/CPython fixture checks pass for the refactor.
Fixtures use CPython 3.14.10 with memory-model-checks,ref-count-return.
Both new factory-ownership regressions also pass with memory-model checks; removing either safeguard makes its regression fail.
Workspace Clippy/import checks and Rust formatting pass.
The preceding merge passed all 42 optimized benchmark smoke cases.
Regressions cover sparse deadlines, partial-copy cleanup, callback reentrancy, cursor/dump stability, argument ownership, factory cleanup and GC tracking.


Summary by cubic

Dictionary and set copies now reuse cached entry hashes, so dict(d), d.copy(), defaultdict(factory, d), empty Counter updates, and frozenset.copy() no longer rehash or compare keys. Copies reuse a shared guarded copier that preflights against the memory budget, polls deadlines, and releases partial results on timeout; empty copies charge zero bytes. frozenset.copy() returns a counted reference to the same immutable object, preserving its cached hash.

Keyword counts in Counter and dict constructors are applied after the copy and released on failure, and self-updates no longer double-hash. {**d} and copy.copy(d) still re-hash keys, matching the documented limitation.