perf: avoid scanning the type cache on type mutation by moreal · Pull Request #8628 · RustPython/RustPython · GitHub
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

perf: avoid scanning the type cache on type mutation#8628
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 with the invalidation change:

Mutations Baseline This PR Change
100 160.17 µs 17.605 µs -89.0%
300 484.82 µs 55.050 µs -88.7%
500 824.61 µs 108.27 µs -86.9%
700 1.1639 ms 159.14 µs -86.3%
900 1.4977 ms 206.99 µs -86.2%

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

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

codspeed-hq Bot commented Aug 31, 2026

Copy link
Copy Markdown
Contributor

Merging this PR will not alter performance

✅ 66 untouched benchmarks
🆕 1 new benchmark

Performance Changes

Benchmark BASE HEAD Efficiency
🆕 rustpython[type_cache_invalidation.py] N/A 3.8 ms N/A

Comparing moreal:perf/type-cache-invalidation (0d1e846) with main (287dcd9)

Open in CodSpeed

Assisted-by: Codex:gpt-5.6-sol
Assisted-by: Codex:gpt-5.6-sol
@moreal
moreal force-pushed the perf/type-cache-invalidation branch from f2b1f39 to 0d1e846 Compare September 2, 2026 17:22
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