Share guarded dict/set copies and avoid redundant dict work #1268
All Vouch review confidence checks passed
Residual risk 0.20 = Change Risk (0.50) × (1 − Review Confidence (0.60)).
Review depth and reviewer attention state are adequate.
Model 1 · Change Risk (XGBoost)
Score: 0.50 · 572 lines diff across 7 files
Model 2 · Review Depth (DistilBERT)
Depth Score: 0.17 · 1 substantive comment(s)
Model 3 · Review Timing & Attention
Session adequacy: 100% · Reviewer familiarity: 35%
Bedrock Claude 3 · Risk Narration
(disconnected)
Groq Risk Narration
AWS Bedrock is currently disconnected. Generate high-confidence PR triage narration on-demand using Groq.
Model 1 · Change Risk Attribution
SHAP Weights
Diff Size
diff_size
0.380
Shallow Review Depth
shallow_review_depth
0.289
Real PR Conversation Timeline
1 comments
"Review active on #1268"
general
weight: 0.35
jaheba
copy_table()fordict(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.dict.setdefault()once and skip dict self-merges. Keyword overrides still apply; Counter self-updates still add counts. Guardget()andsetdefault()arguments through hashing/equality callbacks.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.mdanddocs/limitations/copy.md.Benchmarks
4e42b628versus previous PR head2765b9c0, locally on macOS arm64, Rust 1.99.0 release, fat LTO, one codegen unit:| Workload | Elapsed time change |
| --- | ---: |
| 1,000 self-updates of a 128-entry dict | -97.8% |
| 3,840
setdefaultmisses 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_asrefactor,36cd8f2cversus1e96ded7, 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.
<!-- This is an auto-generated description by cubic. -->
---
Summary by cubic
Dictionary and set copies now reuse cached entry hashes, so
dict(d),d.copy(),defaultdict(factory, d), emptyCounterupdates, andfrozenset.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
Counterand dict constructors are applied after the copy and released on failure, and self-updates no longer double-hash.{**d}andcopy.copy(d)still re-hash keys, matching the documented limitation.<sup>Written for commit 3d39460aa7e8cb4b81582a67ab91d6370a080203. Summary will update on new commits.</sup>
<a href="https://cubic.dev/pr/pydantic/monty/pull/1268?utm_source=github" target="_blank" rel="noopener noreferrer" data-no-image-dialog="true"><picture><source media="(prefers-color-scheme: dark)" srcset="https://www.cubic.dev/buttons/review-in-cubic-dark.svg"><source media="(prefers-color-scheme: light)" srcset="https://www.cubic.dev/buttons/review-in-cubic-light.svg"><img alt="View guided diff" src="https://www.cubic.dev/buttons/review-in-cubic-light.svg"></picture></a>
<!-- End of auto-generated description by cubic. -->