Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Generic/LowerCaseConstant: improve performance #119

Merged
merged 2 commits into from Dec 8, 2023

Commits on Dec 8, 2023

  1. Configuration menu
    Copy the full SHA
    e5c3212 View commit details
    Browse the repository at this point in the history
  2. Generic/LowerCaseConstant: improve performance

    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.
    jrfnl committed Dec 8, 2023
    Configuration menu
    Copy the full SHA
    aae1797 View commit details
    Browse the repository at this point in the history