Skip to content

[ty] Add caching for pattern match narrowing#25613

Merged
charliermarsh merged 2 commits into
mainfrom
charlie/cache-pattern-match-narrowing
Jun 4, 2026
Merged

[ty] Add caching for pattern match narrowing#25613
charliermarsh merged 2 commits into
mainfrom
charlie/cache-pattern-match-narrowing

Conversation

@charliermarsh

@charliermarsh charliermarsh commented Jun 3, 2026

Copy link
Copy Markdown
Member

Summary

When analyzing a match statement, we currently rebuild the narrowed subject for every case by collecting all preceding unguarded patterns into a union and intersecting the subject with its negation. As pattern types become richer, this can repeatedly distribute the same intersections and make a chain of adjacent cases exponentially expensive. For example, without this change, #25493 starts to surface significant slowdowns in select projects.

As a concrete example, this small match blows up with a 9x regression after #25493, without the caching introduced here: https://github.com/kornia/kornia/blob/7c2fee7216599a5e6ef149d4ab2fe33dd70f18c3/kornia/io/io.py#L132-L156. Without caching, every branch has to recompute the prefix in subject & ~(pattern_1 | pattern_2 | ... | pattern_k-1); with caching, we turn it into:

T_0 = subject
T_i = T_i-1 & ~pattern_i

(Prior to #25493, the sequence patterns generally contributed Never here, making it inexpensive.)

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

astral-sh-bot Bot commented Jun 3, 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 92.13%. The percentage of expected errors that received a diagnostic held steady at 87.18%. The number of fully passing files held steady at 92/134.

@astral-sh-bot

astral-sh-bot Bot commented Jun 3, 2026

Copy link
Copy Markdown

Memory usage report

Memory usage unchanged ✅

@astral-sh-bot

astral-sh-bot Bot commented Jun 3, 2026

Copy link
Copy Markdown

ecosystem-analyzer results

No diagnostic changes detected ✅

Full report with detailed diff (timing results)

@codspeed-hq

codspeed-hq Bot commented Jun 3, 2026

Copy link
Copy Markdown

Merging this PR will improve performance by 14.83%

⚠️ Different runtime environments detected

Some benchmarks with significant performance changes were compared across different runtime environments,
which may affect the accuracy of the results.

Open the report in CodSpeed to investigate

⚡ 5 improved benchmarks
❌ 1 regressed benchmark
✅ 59 untouched benchmarks
⏩ 60 skipped benchmarks1

Warning

Please fix the performance issues or acknowledge them on CodSpeed.

Performance Changes

Mode Benchmark BASE HEAD Efficiency
Memory ty_micro[literal_match_fallthrough_guarded_any] 15.1 MB 15.9 MB -5.54%
Simulation ty_micro[large_union_narrowing] 560.7 ms 347.3 ms +61.47%
Simulation ty_micro[literal_match_fallthrough] 81.8 ms 68.5 ms +19.44%
Memory ty_micro[large_union_narrowing] 18.5 MB 16.6 MB +11.91%
Simulation ty_micro[literal_match_fallthrough_guarded_any] 275.5 ms 256.8 ms +7.29%
Simulation ty_micro[many_enum_members_2] 96.5 ms 92.1 ms +4.8%

Tip

Investigate this regression with the CodSpeed MCP and your agent.


Comparing charlie/cache-pattern-match-narrowing (ca71699) with main (27058fc)

Open in CodSpeed

Footnotes

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

@charliermarsh charliermarsh force-pushed the charlie/cache-pattern-match-narrowing branch from d61232c to 6e11f43 Compare June 3, 2026 22:01
@charliermarsh charliermarsh changed the base branch from main to charlie/benchmark-adjacent-exact-sequence-patterns June 3, 2026 22:03
@charliermarsh charliermarsh force-pushed the charlie/cache-pattern-match-narrowing branch from 6e11f43 to e28faa1 Compare June 3, 2026 22:35
@charliermarsh charliermarsh force-pushed the charlie/benchmark-adjacent-exact-sequence-patterns branch 2 times, most recently from 3c911ae to 12eb9a2 Compare June 3, 2026 23:19
@charliermarsh charliermarsh force-pushed the charlie/cache-pattern-match-narrowing branch 2 times, most recently from f7512ba to 35a5ea1 Compare June 3, 2026 23:52
@charliermarsh charliermarsh changed the base branch from charlie/benchmark-adjacent-exact-sequence-patterns to main June 3, 2026 23:52
@charliermarsh charliermarsh force-pushed the charlie/cache-pattern-match-narrowing branch from 35a5ea1 to e436717 Compare June 3, 2026 23:55
@charliermarsh charliermarsh added the performance Potential performance improvement label Jun 4, 2026
@charliermarsh charliermarsh marked this pull request as ready for review June 4, 2026 00:03
@astral-sh-bot astral-sh-bot Bot requested a review from dhruvmanila June 4, 2026 00:03
@charliermarsh charliermarsh marked this pull request as draft June 4, 2026 10:42
@charliermarsh charliermarsh marked this pull request as ready for review June 4, 2026 10:49
Comment thread crates/ty_python_semantic/src/reachability.rs

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

Sweet, it looks like the multithreaded benchmark impact is negligible now!

@charliermarsh charliermarsh merged commit 7c6dcd9 into main Jun 4, 2026
58 of 59 checks passed
@charliermarsh charliermarsh deleted the charlie/cache-pattern-match-narrowing branch June 4, 2026 12:57
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.

3 participants