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

Conversation

jrfnl
Copy link
Member

@jrfnl jrfnl commented Dec 4, 2023

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 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.

Suggested changelog entry

  • Performance improvement for the Generic.PHP.LowerCaseConstant sniff

@jrfnl jrfnl added this to the 3.8.0 milestone Dec 4, 2023
@jrfnl jrfnl force-pushed the feature/generic-lowercaseconstants-performance-fix branch from c9599f2 to a7d41c4 Compare December 5, 2023 23:54
@jrfnl jrfnl force-pushed the feature/generic-lowercaseconstants-performance-fix branch 2 times, most recently from bd4f3f0 to 2016a15 Compare December 7, 2023 16:06
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 jrfnl force-pushed the feature/generic-lowercaseconstants-performance-fix branch from 2016a15 to aae1797 Compare December 8, 2023 03:19
@jrfnl jrfnl merged commit 53c2838 into master Dec 8, 2023
63 checks passed
@jrfnl jrfnl deleted the feature/generic-lowercaseconstants-performance-fix branch December 8, 2023 03:25
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

1 participant