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
{{ message }}
Repository navigation
Optimize UnionType for cases in which all innerTypes are a constant string #15008
when constructing a UnionType we should check all inner types and remember whether it only contains of constant string types.
these constant string types could than be put into a trie datastructure so searching for another constant string type in isSuperTypeOf/isSubTypeOf can be a very fast operation (similar to how a high performance router component in a MVC framework would search a catalog of all system known routes against the current request uri)
the trie datastructure should be represented as a separate class. when possible the trie datastructure could also be used to represent big lists of enum cases to match against a EnumCaseObjectType.
maybe it would even work for big unions of constant integers (with a Trie? with a different datastructure?) ?
maybe we can even do it for any finite type
if one extra null or something like that is in the union it should not defeat the optimization
Did PHPStan help you today? Did it make you happy in any way?
One thing worth pinning down for the datastructure choice: isSuperTypeOf/isSubTypeOf between constant strings is exact-value membership, not prefix or pattern matching (ConstantStringType::isSuperTypeOf on another constant string is just value === value). A union-vs-union check fans that out per member, so the per-call cost today is O(|A|×|B|).
For exact membership an identity-keyed hash map gives O(1) per lookup and turns that into O(|A|+|B|); caching it on the union (your "remember when constructing" instinct) is what makes it amortized. It just wants a hash keyed by value rather than a trie: a trie earns its keep on prefix/pattern queries (what routers do with URIs) or on compressing shared-prefix strings in memory, and membership here is neither.
Handily, the keying already exists: TypeCombinator::finiteUnionMembers() builds exactly this map for the finite-union fast path in intersect(), keying constant scalars and enum cases by identity (s:, i:, b:, enum:Class::Case), with the edge cases already handled (class-string constants and floats excluded, bare enum cases only). That answers the int and enum-case questions with one structure. It's private to TypeCombinator today, so sharing it with isSuperTypeOf/isSubTypeOf would mean lifting it somewhere both can reach.
Activity
SanderMuller commentedon Jul 27, 2026
One thing worth pinning down for the datastructure choice:
isSuperTypeOf/isSubTypeOfbetween constant strings is exact-value membership, not prefix or pattern matching (ConstantStringType::isSuperTypeOfon another constant string is justvalue === value). A union-vs-union check fans that out per member, so the per-call cost today is O(|A|×|B|).For exact membership an identity-keyed hash map gives O(1) per lookup and turns that into O(|A|+|B|); caching it on the union (your "remember when constructing" instinct) is what makes it amortized. It just wants a hash keyed by value rather than a trie: a trie earns its keep on prefix/pattern queries (what routers do with URIs) or on compressing shared-prefix strings in memory, and membership here is neither.
Handily, the keying already exists:
TypeCombinator::finiteUnionMembers()builds exactly this map for the finite-union fast path inintersect(), keying constant scalars and enum cases by identity (s:,i:,b:,enum:Class::Case), with the edge cases already handled (class-string constants and floats excluded, bare enum cases only). That answers the int and enum-case questions with one structure. It's private toTypeCombinatortoday, so sharing it withisSuperTypeOf/isSubTypeOfwould mean lifting it somewhere both can reach.github-actions commentedon Sep 4, 2026
This thread has been automatically locked since there has not been any recent activity after it was closed. Please open a new issue for related bugs.