Generic/LowerCaseConstant: improve performance #119
Merged
+175
−17
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.
Description
Generic/LowerCaseConstant: add tests with functions which don't create scope
Generic/LowerCaseConstant: improve performance
I noticed a PHPCS run on a particular code base being quite slow. Running the Performance report from PR suizlabs/PHP_CodeSniffer#3810 / #60 showed the
Generic.PHP.LowerCaseConstant
sniff being the slowest sniff taking 27 seconds on a total sniff run time of 87 seconds (with a few hundred sniffs being run).A closer look at the sniff pointed to the "skip type declarations for typed properties" part of the sniff as the culprit - in combination with the code base in question having a lot of array properties containing large arrays with mostly
true/false/null
values.Every single one of those
true/false/null
values would trigger a condition check and then a check whether the token was in the property value, causing lots of token walking for those long arrays.This PR should fix that by changing the logic for skipping over the property type declaration.
Instead of checking for each
true/false/null
whether it is a property type (or value), the sniff now listens to all property modifier keywords and skips over the type declaration from there, meaning thattrue/false/null
found within property values will no longer need to do a conditions check/"am I a value or a type?" check.For this particular code base, with this change, the sniff run time goes down from 27 seconds to 0.102 seconds.
Includes additional tests for the "property type skipping" code to verify it works correctly, but also that it won't cause issues with too much/the wrong things being skipped.
Suggested changelog entry