Commit cc5bab6
committed
subtypes: fast path for Union/Union subtype check
Enums are exploded into Union of Literal when narrowed.
Conditional branches on enum values can result in multiple distinct narrowing
of the same enum which are later subject to subtype checks (most notably via
`is_same_type`, when exiting frame context in the binder). Such checks would
have quadratic complexity: `O(N*M)` where `N` and `M` are the number of entries
in each narrowed enum variable, and led to drastic slowdown if any of the enums
involved has a large number of valuees.
Implemement a linear-time fast path where literals are quickly filtered, with
a fallback to the slow path for more complex values.
In our codebase there is one method with a chain of a dozen if statements
operating on instances of an enum with a hundreds of values. Prior to the
regression it was typechecked in less than 1s. After the regression it takes
over 13min to typecheck. This patch fully fixes the regression for us.
Fixes #138211 parent 2514610 commit cc5bab6
1 file changed
+37
-5
lines changed| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
57 | 57 | | |
58 | 58 | | |
59 | 59 | | |
| 60 | + | |
60 | 61 | | |
61 | 62 | | |
62 | 63 | | |
| |||
877 | 878 | | |
878 | 879 | | |
879 | 880 | | |
880 | | - | |
| 881 | + | |
| 882 | + | |
| 883 | + | |
| 884 | + | |
| 885 | + | |
| 886 | + | |
| 887 | + | |
| 888 | + | |
| 889 | + | |
| 890 | + | |
| 891 | + | |
| 892 | + | |
| 893 | + | |
| 894 | + | |
| 895 | + | |
| 896 | + | |
| 897 | + | |
| 898 | + | |
| 899 | + | |
| 900 | + | |
| 901 | + | |
| 902 | + | |
| 903 | + | |
| 904 | + | |
| 905 | + | |
| 906 | + | |
881 | 907 | | |
882 | | - | |
883 | 908 | | |
884 | 909 | | |
| 910 | + | |
| 911 | + | |
885 | 912 | | |
886 | 913 | | |
887 | | - | |
| 914 | + | |
888 | 915 | | |
889 | | - | |
890 | | - | |
| 916 | + | |
| 917 | + | |
| 918 | + | |
| 919 | + | |
| 920 | + | |
| 921 | + | |
891 | 922 | | |
892 | 923 | | |
| 924 | + | |
893 | 925 | | |
894 | 926 | | |
895 | 927 | | |
| |||
0 commit comments