Fix #11968: Large enums are slow#5091
Closed
phpstan-bot wants to merge 1 commit intophpstan:2.1.xfrom
Closed
Conversation
- New integration test testBug11968 in AnalyserIntegrationTest - Test data file with 500-case UnitEnum with match expression and method calls - Covers the pattern from the issue: large enum with match($this) returning strings - Ensures analysis of large enums completes in reasonable time without errors Closes phpstan/phpstan#11968
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
Issue #11968 reported that large enums (~7500 cases) cause slow PHPStan analysis. The fix was already implemented in PR #3695 which optimized
IntersectionType::getEnumCases()by replacing expensivemd5(describe())key computation with simple string concatenation (className . '::' . caseName), achieving a 5x speedup. This PR adds a regression test to prevent the issue from resurfacing.Changes
testBug11968()method intests/PHPStan/Analyser/AnalyserIntegrationTest.phptests/PHPStan/Analyser/data/bug-11968.phpwith:UnitEnumimplementing an interfacematch($this)expression mapping all 500 cases to string valuesinstanceofnarrowing andin_arraywith the enum typeRoot cause
The original performance issue was caused by
IntersectionType::getEnumCases()usingmd5($enumCase->describe(VerbosityLevel::typeOnly()))as the array key for each enum case when computing the intersection of enum cases. For large enums, thedescribe()call andmd5()computation were expensive. The fix (PR #3695) replaced this with a simple$enumCase->getClassName() . '::' . $enumCase->getEnumCaseName()concatenation.Test
The regression test runs full analysis on a PHP file containing a 500-case UnitEnum with a match expression. It verifies that analysis completes without errors and in reasonable time. This follows the same pattern as existing enum performance tests (bug-10772, bug-10979, bug-11263).
Fixes phpstan/phpstan#11968