Repository navigation
[perf] Check return literals before override guard in two return-type rules - #8710
Merged
Merged
Conversation
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.
NumericReturnTypeFromStrictScalarReturnsRectorandBoolReturnTypeFromBooleanConstReturnsRectorsubscribe toClassMethod. Both ranClassMethodReturnTypeOverrideGuard::shouldSkipClassMethod()- parent/override reflection - before checking whether the returns are actually int/float (or true/false) literals. A method whose single return is not such a literal (the common case) paid for the override lookup only to then fail the literal check and return null.The node-only literal check now runs first, so a method returning a non-literal expression bails before the reflection. The check reads only AST (
Int_/Float_/UnaryMinus,ConstFetch+ true/false), so the outcome is unchanged - the fail path returned null either way.Behavior unchanged - all 8 fixtures across both rules pass.
Bench included (
bench/return_type_literal_filter_bench.php), drivingrefactor()over 151 methods that return a non-literal expression. CPU per call, two runs each:~2.5x faster on that hot path. The remaining cost is
findReturnsScoped(), which runs regardless. Runphp bench/return_type_literal_filter_bench.phpon each branch to reproduce.Siblings checked and left as-is:
StringReturnTypeFromStrictScalarReturnsRectoralready runs its literal check before the guard;NumericReturnTypeFromStrictReturnsRectorhas no node-only filter for the common case (its work is type inference).