Skip to content

perf: avoid scanning the type cache on type mutation - #8628

Draft
moreal wants to merge 2 commits into
RustPython:mainfrom
moreal:perf/type-cache-invalidation
Draft

moreal wants to merge 2 commits into
RustPython:mainfrom
moreal:perf/type-cache-invalidation

Conversation

@moreal

@moreal moreal commented Aug 31, 2026 •

Copy link
Copy Markdown
Contributor

Summary

Type mutation previously invalidated method-cache entries by scanning all 4,096 slots for the type's old version tag. This made every cached type mutation pay for a full-cache scan.

This change invalidates the type by setting its version tag to zero and leaves the old cache entry in place. Type version tags are monotonic and never reused; once the counter is exhausted, new tag assignment remains disabled. Cache readers accept an entry only when its version and interned name match the type's current version under the existing sequence-lock protocol, so an entry for an invalidated version is unreachable.

Cached values are already marked as published and use QSBR-delayed allocation reclamation, which lets an in-flight reader that observed the old version finish its conditional incref safely. The existing whole-cache clear still nulls every borrowed pointer before cyclic GC breaks type reference cycles.

This also adds a microbenchmark for repeated cached lookup followed by type mutation, plus focused tests for version reachability and concurrent cache readers.

Performance

benches/microbenchmarks/type_cache_invalidation.py compares the benchmark-only commit (16413a9) with the invalidation change (53e2bb2). Re-measured on 2026-10-25 after rebasing onto main (0f74d1c): Apple M1 Max, rustc 1.99.0, cargo bench --bench microbenchmarks (criterion mean of 100 samples), mutation counts as in the harness:

Mutations Baseline This PR Change
100 195.81 µs 21.509 µs -89.0%
300 587.61 µs 66.977 µs -88.6%
500 943.94 µs 213.64 µs -77.4%
700 1.3472 ms 244.92 µs -81.8%
900 1.7180 ms 282.93 µs -83.5%

AI assistance

Codex (gpt-5.6-sol) assisted extensively with the implementation, tests, benchmark setup and analysis, automated validation, code review, and drafting this PR description.

@coderabbitai

coderabbitai Bot commented Aug 31, 2026 •

Copy link
Copy Markdown
Contributor

Important

Draft PR not reviewed

Draft PRs are not automatically reviewed by default.

  • Trigger a manual review

To automatically review draft PRs, update your CodeRabbit configuration:

reviews:
  auto_review:
    drafts: true
  • Autofix · Keep fixing CodeRabbit findings and required CI, and resolving merge conflicts

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

❤️ Share

Comment @coderabbitai help to get the list of available commands.

@github-actions github-actions Bot added the z-ca-2026 Tag to track Contribution Academy 2026 label Aug 31, 2026
@codspeed

codspeed Bot commented Aug 31, 2026 •

Copy link
Copy Markdown
Contributor

Merging this PR will degrade performance by 11.74%

❌ 1 regressed benchmark
✅ 61 untouched benchmarks
🆕 1 new benchmark
⏩ 4 skipped benchmarks1

Warning

Please fix the performance issues or acknowledge them on CodSpeed.

Performance Changes

Benchmark BASE HEAD Efficiency
❌ rustpython[comprehension_list.py] 802.5 µs 909.2 µs -11.74%
🆕 rustpython[type_cache_invalidation.py] N/A 4 ms N/A

Tip

Investigate this regression by commenting @codspeedbot fix this regression on this PR, or directly use the CodSpeed MCP with your agent.


Comparing moreal:perf/type-cache-invalidation (293a7e8) with main (0f74d1c)

Open in CodSpeed

Footnotes

  1. 4 benchmarks were skipped, so the baseline results were used instead. If they were deleted from the codebase, click here and archive them to remove them from the performance reports. ↩

Assisted-by: Codex:gpt-5.6-sol
@moreal
moreal force-pushed the perf/type-cache-invalidation branch from 1480f43 to 53e2bb2 Compare October 25, 2026 11:01
Assisted-by: Codex:gpt-5.6-sol
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

z-ca-2026 Tag to track Contribution Academy 2026

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant