Thanks to visit codestin.com
Credit goes to github.com

Skip to content

[ty] Store cycle-detector cache entries inline#26183

Merged
MichaReiser merged 4 commits into
mainfrom
micha/cycle-detector-inline-cache
Jun 22, 2026
Merged

[ty] Store cycle-detector cache entries inline#26183
MichaReiser merged 4 commits into
mainfrom
micha/cycle-detector-inline-cache

Conversation

@MichaReiser

@MichaReiser MichaReiser commented Jun 21, 2026

Copy link
Copy Markdown
Member

Summary

We always push at least one type into the CycleDetector cache, even if that was the only typed that needed visiting.

This PR adds an inline storage that avoids allocating for the most common cases.

Cutoff populated
1 74.32%
2 89.48%
3 92.66%
4 95.33%
8 98.07%
16 99.27%
32 99.89%

As you can see, most CycleDetector (90%) contain at most two cached elements. That's what this PR uses as inline cache. We keep using a FxHashMap for cases with more than two elements.

I also considered using a SmallVec, but cache can grow large

p50 p90 p95 p99 p99.9 Maximum
1 3 4 15 33 1423

Benchmarks

Codspeed reports about a 2-3% perf improvement for most projects

Test Plan

Testing: Passed the ty semantic test suite, Clippy, repository hooks, and wall-time benchmarks.

@astral-sh-bot astral-sh-bot Bot added the ty Multi-file analysis & type inference label Jun 21, 2026
@astral-sh-bot

astral-sh-bot Bot commented Jun 21, 2026

Copy link
Copy Markdown

Typing conformance results

No changes detected ✅

Current numbers
The percentage of diagnostics emitted that were expected errors held steady at 94.37%. The percentage of expected errors that received a diagnostic held steady at 89.00%. The number of fully passing files held steady at 94/134.

@astral-sh-bot

astral-sh-bot Bot commented Jun 21, 2026

Copy link
Copy Markdown

Memory usage report

Memory usage unchanged ✅

@astral-sh-bot

astral-sh-bot Bot commented Jun 21, 2026

Copy link
Copy Markdown

ecosystem-analyzer results

No diagnostic changes detected ✅

Full report with detailed diff (timing results)

@MichaReiser MichaReiser added the performance Potential performance improvement label Jun 21, 2026
@MichaReiser MichaReiser marked this pull request as ready for review June 21, 2026 11:25
@MichaReiser MichaReiser requested a review from a team as a code owner June 21, 2026 11:25
@astral-sh-bot astral-sh-bot Bot requested a review from dcreager June 21, 2026 11:25
@AlexWaygood

Copy link
Copy Markdown
Member

I also considered using a SmallVec, but cache can grow large

Wow, 1423 is larger than I would have guessed. I wonder if we could (separately) experiment with using a fancier cache that has some kind of LRU cache eviction. My guess would be that some small portion of those 1423 entries are repeatedly hit, while most are hit rarely, if at all

@MichaReiser

Copy link
Copy Markdown
Member Author

Wow, 1423 is larger than I would have guessed. I wonder if we could (separately) experiment with using a fancier cache that has some kind of LRU cache eviction. My guess would be that some small portion of those 1423 entries are repeatedly hit, while most are hit rarely, if at all

I'm not sure this is worth it. This adds another write to the cached paths and the cost is relatively low, because we don't store CycleDetectors.

@AlexWaygood

Copy link
Copy Markdown
Member

I still think experimenting could be interesting, but it's definitely out of scope here

@MichaReiser

Copy link
Copy Markdown
Member Author

Sure. We just see in Salsa that LRU cache has a high cost because it adds a write to every read path. But, the scales are probably different here.

Comment thread crates/ty_python_semantic/src/types/cyclic.rs
Comment thread crates/ty_python_semantic/src/types/cyclic.rs
Comment thread crates/ty_python_semantic/src/types/cyclic.rs Outdated
@MichaReiser MichaReiser force-pushed the micha/cycle-detector-inline-cache branch from f2b3ec3 to bb6d6d0 Compare June 22, 2026 06:40
@codspeed-hq

codspeed-hq Bot commented Jun 22, 2026

Copy link
Copy Markdown

Merging this PR will improve performance by 5.25%

⚡ 1 improved benchmark
✅ 70 untouched benchmarks
⏩ 64 skipped benchmarks1

Performance Changes

Mode Benchmark BASE HEAD Efficiency
Simulation ty_micro[large_union_narrowing] 209.3 ms 198.9 ms +5.25%

Tip

Curious why this is faster? Use the CodSpeed MCP and ask your agent.


Comparing micha/cycle-detector-inline-cache (bb6d6d0) with main (ae306a0)

Open in CodSpeed

Footnotes

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

@MichaReiser MichaReiser merged commit e94cc9a into main Jun 22, 2026
95 of 96 checks passed
@MichaReiser MichaReiser deleted the micha/cycle-detector-inline-cache branch June 22, 2026 07:13
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

performance Potential performance improvement ty Multi-file analysis & type inference

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants