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

Use static closures for minor performance improvement whenever instance access is not needed #729

Merged
merged 8 commits into from
Jun 13, 2023

Conversation

westonruter
Copy link
Member

@westonruter westonruter commented May 15, 2023

Summary

Fixes #728

Relevant technical choices

  • Added the SlevomatCodingStandard.Functions.StaticClosure sniff to the PHPCS ruleset.
  • Ran PHPCBF to add static where relevant.

Checklist

  • PR has either [Focus] or Infrastructure label.
  • PR has a [Type] label.
  • PR has a milestone or the no milestone label.

@westonruter westonruter added [Type] Bug An existing feature is broken Infrastructure Issues for the overall performance plugin infrastructure labels May 15, 2023
@westonruter westonruter added this to the 2.4.0 milestone May 15, 2023
Copy link
Member

@felixarntz felixarntz left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@westonruter LGTM, just one non-blocking question.

phpcs.xml.dist Outdated Show resolved Hide resolved
@felixarntz felixarntz added [Type] Enhancement A suggestion for improvement of an existing feature and removed [Type] Bug An existing feature is broken labels May 15, 2023
@felixarntz felixarntz changed the title Fix: Static closures should be employed whenever $this is not used Use static closures should whenever $this is not used May 15, 2023
@felixarntz
Copy link
Member

@westonruter I've updated the label since this is more of an enhancement than a bug (nothing broken without this), plus changed the title slightly since that is used to auto-generate the eventual changelog for the release that this will be part of.

@westonruter westonruter requested a review from manuelRod as a code owner May 15, 2023 20:59
@felixarntz felixarntz changed the title Use static closures should whenever $this is not used Use static closures whenever $this is not used May 15, 2023
Copy link
Member

@aristath aristath left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM 👍
I had a small nitpick and added a comment, but definitely not a blocker

tests/admin/load-tests.php Outdated Show resolved Hide resolved
composer.json Outdated
@@ -15,6 +15,7 @@
"dealerdirect/phpcodesniffer-composer-installer": "^0.7",
"phpcompatibility/php-compatibility": "^9.3",
"phpunit/phpunit": "^4|^5|^6|^7|^8|^9",
"slevomat/coding-standard": "^8.0",
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This coding standard requires php 7.2. Will would make this plugin break in 7.2- no.

https://github.com/slevomat/coding-standard/blob/f69e2524e8770efb9b3e5ac4a0ebc0d54eb446d7/composer.json#L19

Copy link
Member

@swissspidy swissspidy May 16, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It would not break the plugin, because the composer autoloader is only used in tests.

It would just break tests running on PHP 5.6. However, right now tests are not running on this version on CI, so... 🤷

Edit: looks like there is #399 for updating tests.

Copy link
Member Author

@westonruter westonruter May 16, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please continue discussion at #730 (comment)

As I understand, there are three possible ways to about this:

  1. Have a separate composer file for PHP 7.2+
  2. In GHA workflow, just-in-time composer require this package before doing composer install before running PHPCS
  3. In GHA workflow, just-in-time composer remove this package before doing composer install prior to running PHPUnit in PHP<7.2.

I think the third option is ideal, since all normal development environments would be running PHP 7.2+. It's only the exception case, specifically on GHA, where PHP 5.6 is needed. This would allow a developer to have all packages installed normally.

The first option doesn't seem ideal since there would be a separate composer file to maintain, and Dependabot wouldn't automatically be aware of it.

This comment was marked as duplicate.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We may not want this sniff after all. See WordPress/wordpress-develop#4457 (comment)

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Noting that we could still make a separate decision from core here, there are several plugins that have stricter requirements than core to enforce better coding practices. I don't feel strongly about this one, but I feel like if we can find a way that we can integrate it that remains compatible with PHP 5.6, I'd be happy to include it.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I am not against using this rule set, but php 5.6 compat is the key here.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can the 5.6 compat be handled as part of #399? Without CI actually running anything in 5.6, it makes it a bit difficult to verify the compat is working, or at least it seems a bit premature.

Copy link
Member

@felixarntz felixarntz left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Marking this as needing changes due to #730 (comment) which applies here too.

Co-authored-by: Ari Stathopoulos <[email protected]>
@westonruter westonruter requested a review from felixarntz May 16, 2023 16:21
Copy link
Member

@mukeshpanchal27 mukeshpanchal27 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks, @westonruter. I left non-blocking feedback other than looks good to me.

tests/server-timing/perflab-server-timing-tests.php Outdated Show resolved Hide resolved
tests/server-timing/perflab-server-timing-tests.php Outdated Show resolved Hide resolved
@westonruter
Copy link
Member Author

westonruter commented Jun 1, 2023

Marking this as needing changes due to #730 (comment) which applies here too.

@felixarntz Considering your #730 (comment) I think this can move forward.

@felixarntz
Copy link
Member

@westonruter Can you refresh this PR please so we can hopefully merge it by tomorrow for the 2.4.0 release?

… add/static-closures

* 'trunk' of https://github.com/WordPress/performance:
  Wrap link around the entire third step instruction.
  Implement an admin notice and admin pointer to prompt admins using the SQLite module to migrate to the standalone plugin.
  Enhance interoperability between the SQLite module and the standalone plugin.
  Make Server-Timing header output more robust
  Reuse __return_null
  Replace deprecated RegExp assertions
  Require phpstan/phpstan-deprecation-rules
  Fix covers tags and a couple typos
  Require phpstan/phpstan-phpunit
  Add missing default value for message param in assertImageNotHasSource phpdoc
  Add missing return value null for void
  Fix composer script name in lint-staged
  Add missing parameter in phpdocc for perflab_disable_object_cache_dropin filter
  Add phpstan/extension-installer to allow-plugins
  Add phpstan config to branches.paths in php-lint workflow
  Run composer update
  Add phpstan config
  Add PHPStan to php-lint workflow
  Add package scripts and lint-staged for phpstan
  Require phpstan packages
@westonruter
Copy link
Member Author

@westonruter Can you refresh this PR please so we can hopefully merge it by tomorrow for the 2.4.0 release?

@felixarntz Done ✅

Copy link
Member

@felixarntz felixarntz left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks @westonruter!

@felixarntz felixarntz changed the title Use static closures whenever $this is not used Use static closures for minor performance improvement whenever instance access is not needed Jun 13, 2023
@felixarntz felixarntz merged commit 2d2f5f2 into trunk Jun 13, 2023
@felixarntz felixarntz deleted the add/static-closures branch June 13, 2023 16:56
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Infrastructure Issues for the overall performance plugin infrastructure [Type] Enhancement A suggestion for improvement of an existing feature
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Static closures should be employed whenever $this is not used
6 participants