← Back to Pull requests

Share guarded dict/set copies and avoid redundant dict work #1268

Closed
unknown closed this pull request without merging on Nov 14, 2026 17:25 UTC
View on GitHub
unknown unknown commented on Nov 07, 2026 10:28 UTC
Author

  • 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:

    | Workload | Elapsed 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.

    <!-- 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), 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.

    <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. -->

    Diff: 572 lines (+429 / -143) across 7 files Reviewer: jaheba Review duration: 10496m
    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
    0.50
    Model 2 · Review Depth (DistilBERT)
    Depth Score: 0.17 · 1 substantive comment(s)
    0.17
    Model 3 · Review Timing & Attention
    Session adequacy: 100% · Reviewer familiarity: 35%
    60%
    Bedrock Claude 3 · Risk Narration
    (disconnected)
    Groq
    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
    Reviewers
    Assignees
    Unassigned
    Labels
    risk: low confidence: 60.5% reviewed
    Vouch Risk Breakdown
    AI-powered review risk analysis
    Change Risk
    Change Risk · Model 1 (XGBoost)
    Predicted defect probability (0.00–1.00) based on diff churn, modified sensitive files, and author risk history.
    Score: 49.8% (MEDIUM) · Higher = larger blast radius
    49.8%
    Review Confidence
    Review Confidence · Models 2 & 3
    Scrutiny score (0.00–1.00) combining DistilBERT semantic review depth with reviewer familiarity and session dwell time.
    Score: 60.5% · Lower = rushed or shallow review
    60.5%
    Residual Risk
    Residual Risk · Core Equation
    Residual = Change × (1 − Confidence)
    Unmitigated risk surviving review. ≥ 0.65 triggers an automated re-queue to protect production.
    Score: 19.7% · Safe to merge
    19.7%
    Repository