You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
[ty] Add caching for pattern match narrowing (#25613)
## 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.)
0 commit comments