Generic/LowerCaseConstant: improve performance#119
Merged
Conversation
c9599f2 to
a7d41c4
Compare
bd4f3f0 to
2016a15
Compare
I noticed a PHPCS run on a particular code base being quite slow. Running the Performance report from PR 3810 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 that `true/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. > Note: an additional performance boost could be gained by not recording metrics and bowing out early for any `true/false/null` which are already lowercase, but that would change the functionality of the sniff.
2016a15 to
aae1797
Compare
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.
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.LowerCaseConstantsniff 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/nullvalues.Every single one of those
true/false/nullvalues 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/nullwhether 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/nullfound 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