Skip to content

Optimize UnionType for cases in which all innerTypes are a constant string #15008

Closed
phpstan/phpstan-src
#6116

Description

@staabm

Feature request

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?

No response

Activity

  1. SanderMuller commented on Jul 27, 2026

    @SanderMuller
    Contributor

    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.

  2. github-actions commented on Sep 4, 2026

    @github-actions

    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.

  3. locked as resolved and limited conversation to collaborators on Sep 4, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions