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

Performance improvements on append_to_selector method #47833

Merged
merged 1 commit into from
Feb 10, 2023

Conversation

spacedmonkey
Copy link
Member

What?

The append_to_selector method is called up to 1200 times per page load.

This does an explode / implode, even if the string does not need it. Do a simple check to see if the string contains a comma before doing the explode.

Screenshot from 2023-02-07 13-15-43

Why?

How?

Testing Instructions

Testing Instructions for Keyboard

Screenshots or screencast

@github-actions
Copy link

github-actions bot commented Feb 7, 2023

Flaky tests detected in bee781c.
Some tests passed with failed attempts. The failures may not be related to this commit but are still reported for visibility. See the documentation for more information.

🔍 Workflow run URL: https://github.com/WordPress/gutenberg/actions/runs/4114291451
📝 Reported issues:

Copy link
Member

@Mamaduka Mamaduka left a comment

Choose a reason for hiding this comment

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

The fix looks good to me. Thank you, @spacedmonkey!

Do you have a performance graph after this change is applied?

@felixarntz
Copy link
Member

felixarntz commented Feb 9, 2023

@spacedmonkey @Mamaduka @aristath I gave this a test in WordPress core trunk (adding the same change directly in there), with TT3 theme. I used the Performance Lab Server-Timing API from WordPress/performance#553, registering a metric for the WP_Theme_JSON::append_to_selector() method that measures its execution. I then used the median based on 50 requests.

  • In my setup, the method is called 1179 times per page load.
  • 1106 of those 1179 times (93.8%) don't need the implode + explode, so great catch there.
  • Without this fix, the overall time that method consumes is 8.5ms; with this fix it's 8.14ms. That's an increase of 4.3%, but overall it's less than a millisecond, so a very small benefit.
  • Either way, >8ms for a single function is a whole lot (about ~7% of total WP execution time in my testing). So maybe there's even more we can improve.

I looked into the problem a bit further, particularly in how this method is being called.

  • 1023 of those 1179 times (86.7%) the method is called with $position set to left.
  • The method is pretty much always called with different values, so caching won't help here indeed.
  • However, the main problem here is that this method is called so often and runs those checks all the time. This is really unnecessary as we already know it when calling the method; this is one of the reasons why using parameters to control what a function does is a bad practice.

I think this change here looks good to merge for now. But I also think we should (in a separate PR) explore implementing this highly utilized method in a cleaner way. Instead of append_to_selector( $selector, $to_append, $position ), we should have the following much simpler utility methods:

  • append_to_selector( $selector, $to_append )
  • prepend_to_selector( $selector, $to_prepend )
  • append_to_selectors( $selectors, $to_append )
  • prepend_to_selectors( $selectors, $to_prepend )

With that, these's no if conditions anywhere, and only what is actually needed for each of the 4 scenarios is run. In all places where append_to_selector is called today, we'll know right away which of those 4 methods to call instead, so migrating to this logic should be trivial.

@spacedmonkey spacedmonkey merged commit bb61b4c into trunk Feb 10, 2023
@spacedmonkey spacedmonkey deleted the fix/append_to_selector branch February 10, 2023 17:26
@github-actions github-actions bot added this to the Gutenberg 15.2 milestone Feb 10, 2023
@DaisyOlsen DaisyOlsen added [Type] Performance Related to performance efforts [Feature] Themes Questions or issues with incorporating or styling blocks in a theme. labels Feb 14, 2023
@t-hamano
Copy link
Contributor

t-hamano commented Mar 5, 2023

I came across this PR while researching #48706.
The issue is that in theme.json, when a style is applied to a heading element in a particular block, the correct selector is not generated. If I reverted this PR change, it fixes the issue reported in #48706.

@@ -785,6 +785,9 @@ protected static function sanitize( $input, $valid_block_names, $valid_element_n
* @return string The new selector.
*/
protected static function append_to_selector( $selector, $to_append, $position = 'right' ) {
if ( ! str_contains( ',', $selector ) ) {
Copy link
Contributor

Choose a reason for hiding this comment

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

Perhaps the order of the arguments is reversed?
See: https://www.php.net/manual/en/function.str-contains.php

@felixarntz
Copy link
Member

Related: https://core.trac.wordpress.org/ticket/58193 and WordPress/wordpress-develop#4380

I'll work on porting this over to Gutenberg.

@oandregal
Copy link
Member

Backport at WordPress/wordpress-develop#4412

@ramonjd ramonjd removed the Needs PHP backport Needs PHP backport to Core label Jun 2, 2023
pento pushed a commit to WordPress/wordpress-develop that referenced this pull request Jun 12, 2023
Improve `append_to_selector` method in `WP_Theme_JSON` by checking to see if string contains a common before imploding / exploding string, which improves performance. 

Originally developed and tested in [WordPress/gutenberg#47833 Gutenberg PR 47833].

Props spacedmonkey, flixos90, mukesh27, joemcgill, wildworks, oandregal, mamaduka.
Fixes #58231.

git-svn-id: https://develop.svn.wordpress.org/trunk@55907 602fd350-edb4-49c9-b593-d223f7449a82
@spacedmonkey
Copy link
Member Author

markjaquith pushed a commit to WordPress/WordPress that referenced this pull request Jun 12, 2023
Improve `append_to_selector` method in `WP_Theme_JSON` by checking to see if string contains a common before imploding / exploding string, which improves performance. 

Originally developed and tested in [WordPress/gutenberg#47833 Gutenberg PR 47833].

Props spacedmonkey, flixos90, mukesh27, joemcgill, wildworks, oandregal, mamaduka.
Fixes #58231.
Built from https://develop.svn.wordpress.org/trunk@55907


git-svn-id: http://core.svn.wordpress.org/trunk@55419 1a063a9b-81f0-0310-95a4-ce76da25c4cd
github-actions bot pushed a commit to gilzow/wordpress-performance that referenced this pull request Jun 12, 2023
Improve `append_to_selector` method in `WP_Theme_JSON` by checking to see if string contains a common before imploding / exploding string, which improves performance. 

Originally developed and tested in [WordPress/gutenberg#47833 Gutenberg PR 47833].

Props spacedmonkey, flixos90, mukesh27, joemcgill, wildworks, oandregal, mamaduka.
Fixes #58231.
Built from https://develop.svn.wordpress.org/trunk@55907


git-svn-id: https://core.svn.wordpress.org/trunk@55419 1a063a9b-81f0-0310-95a4-ce76da25c4cd
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
[Feature] Themes Questions or issues with incorporating or styling blocks in a theme. [Type] Performance Related to performance efforts
Projects
None yet
Development

Successfully merging this pull request may close these issues.

8 participants