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

Skip to content

[ty] Use SmallVec for CycleDetector::seen#26181

Merged
MichaReiser merged 2 commits into
mainfrom
micha/cycle-detector-smallvec
Jun 21, 2026
Merged

[ty] Use SmallVec for CycleDetector::seen#26181
MichaReiser merged 2 commits into
mainfrom
micha/cycle-detector-smallvec

Conversation

@MichaReiser

@MichaReiser MichaReiser commented Jun 21, 2026

Copy link
Copy Markdown
Member

Store the CycleDetector::seen in a SmallVec instead of a FxIndexSet. We always push a type into seen, even if this ends being the only type that we visited. Today, this always results in an allocation.

This PR uses a SmallVec for seen instead.

Cutoff seen
1 81.20%
2 94.97%
3 98.64%
4 99.61%
8 99.994%
16 99.99996%
32 99.99996%

As you can see, almost all CycleDetectors store at most 2-3 elements, in which case a simple vector contains is much faster.

I was worried about what if seen grows very large. However, running ty over all ecosystem projects: maximum 52 in mitmproxy for six detectors; another eight reached 51. The next-highest project maximum was only 14.

So there are a few large seen tables but they're still bound where a O(n) scan is reasonable. On top, our current implementation is already O(n), and a larger seen also results in a much deeper stack because seen tracks stack depth. That's why I think a Vec (or small vec) is perfectly fine here. Especially because it does perform better than my small set implementation #26171

Performance

See Codspeed report. This also helps reduce allocations by a lot (up to 5.6% for some projects)

@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 ✅

Flaky changes detected. This PR summary excludes flaky changes; see the HTML report for details.

Full report with detailed diff (timing results)

@MichaReiser MichaReiser reopened this Jun 21, 2026
@codspeed-hq

codspeed-hq Bot commented Jun 21, 2026

Copy link
Copy Markdown

Merging this PR will improve performance by 5.36%

⚡ 6 improved benchmarks
✅ 65 untouched benchmarks
⏩ 64 skipped benchmarks1

Performance Changes

Mode Benchmark BASE HEAD Efficiency
Simulation ty_micro[large_union_narrowing] 237.7 ms 218.9 ms +8.59%
Simulation ty_micro[typevar_mapping_accumulation] 984.7 ms 933.2 ms +5.51%
Simulation ty_micro[large_isinstance_narrowing] 150.4 ms 143.6 ms +4.73%
WallTime pydantic 10.9 s 10.4 s +4.6%
WallTime colour_science 79.8 s 76.4 s +4.52%
WallTime freqtrade 12.8 s 12.2 s +4.24%

Tip

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


Comparing micha/cycle-detector-smallvec (e9e3bcf) with main (44f6d18)

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

Copy link
Copy Markdown
Member Author

How frustrating that this is faster 😆

@MichaReiser MichaReiser changed the title [ty] Use SmallVec for cycle detection [ty] Use SmallVec for CycleDetector::seen Jun 21, 2026
@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:08
@MichaReiser MichaReiser requested review from a team as code owners June 21, 2026 11:08
@astral-sh-bot astral-sh-bot Bot requested a review from sharkdp June 21, 2026 11:08

@AlexWaygood AlexWaygood left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ugh, our CycleDetector was already an overabstraction with too many generic parameters 😆 but this is awesome, thanks

@MichaReiser MichaReiser merged commit 505cb15 into main Jun 21, 2026
75 of 80 checks passed
@MichaReiser MichaReiser deleted the micha/cycle-detector-smallvec branch June 21, 2026 18:07
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