Make WordPress Core

Opened 8 months ago

Closed 5 months ago

Last modified 5 months ago

#61679 closed defect (bug) (fixed)

Reimplement caching in `wp_add_global_styles_for_blocks()`

Reported by: joemcgill's profile joemcgill Owned by: joemcgill's profile joemcgill
Milestone: 6.7 Priority: normal
Severity: normal Version:
Component: Editor Keywords: has-patch has-unit-tests commit
Focuses: performance Cc:

Description

This is a follow-up ticket to #59595 to update and reimplement the caching strategy that was reverted in [58710], while leaving that ticket open to refocus on the performance of the WP_Theme_JSON::compute_style_properties method specifically.

Background

While trying to reduce the performance impact of WP_Theme_JSON::compute_style_properties in #59595, we discovered that the reason this was being called so frequently was due to it's use in WP_Theme_JSON::get_styles_for_block() which is called for each block in wp_add_global_styles_for_blocks(). See this comment.

This user-facing bug with the site editor was discovered late in the 6.6 cycle, which led to the caching being reverted.

This new ticket is to separate discussion about the reimplementation of that cache from additional performance optimizations being worked on specific to the original WP_Theme_JSON::compute_style_properties method.

Change History (10)

This ticket was mentioned in Slack in #core-performance by joemcgill. View the logs.


8 months ago

This ticket was mentioned in PR #6879 on WordPress/wordpress-develop by @joemcgill.


8 months ago
#2

  • Keywords has-unit-tests added

This uses a consistent cache key with the cache busting hash saved to the cache value rather than as part of the key. This allows us to remove the expiry time for the cache and only invalidate when the hash changes.

This also swaps the use of a site transient to the use of a regular transient so that the option will be autoloaded since there is no expiration time.

Update July 12, 2024
This has been updated to also fix a bug that was reported in https://core.trac.wordpress.org/ticket/59595#comment:71 with testing instructions.

Trac ticket: https://core.trac.wordpress.org/ticket/61679
See: https://core.trac.wordpress.org/ticket/59595

@joemcgill commented on PR #6879:


8 months ago
#3

Thanks for the review, @costdev. This is ready for another look.

@joemcgill commented on PR #6879:


8 months ago
#4

I spent some time trying to write a new PHPUnit test that will confirm that the block meta will change whenever the wp_global_styles post is updated but discovered that the query in WP_Theme_JSON_Resolver::get_user_data_from_wp_global_styles( wp_get_theme() ) does not return the same post that is created if you first call WP_Theme_JSON_Resolver::get_user_data_from_wp_global_styles( wp_get_theme(), true ) to create the post. This seems like an unrelated bug so will report it upstream, but wanted to mention in case anyone had any insight into why this might be expected behavior.

Gutenberg issue opened, along with a unit test that demonstrates the behavior.

This ticket was mentioned in Slack in #core-performance by mukeshpanchal27. View the logs.


5 months ago

@joemcgill commented on PR #6879:


5 months ago
#6

I've refreshed this PR and added an additional unit test to confirm that the cache gets invalidated whenever the global-styles post is updated in the editor.

#7 @joemcgill
5 months ago

  • Keywords commit added

@joemcgill commented on PR #6879:


5 months ago
#8

I've run a set of benchmarks on this locally, which compares 40 runs to the homepage of Twenty Twenty Four.

URL trunk This PR Difference Percentage Difference
Response Time (p10) 70.75 69.21 -1.54 -2.22%
Response Time (p25) 71.51 69.77 -1.74 -2.50%
Response Time (p50) 72.75 70.65 -2.10 -2.97%
Response Time (p75) 74.45 72.05 -2.40 -3.33%
Response Time (p90) 76.19 76.28 0.09 0.12%
wp-before-template (p10) 31.48 31.21 -0.27 -0.86%
wp-before-template (p25) 31.83 31.54 -0.29 -0.92%
wp-before-template (p50) 32.25 32.17 -0.08 -0.25%
wp-before-template (p75) 33.25 33.13 -0.12 -0.36%
wp-before-template (p90) 33.8 34.42 0.62 1.80%
wp-template (p10) 36.39 35.08 -1.31 -3.73%
wp-template (p25) 36.91 35.43 -1.48 -4.18%
wp-template (p50) 37.83 35.96 -1.87 -5.20%
wp-template (p75) 39.14 37.75 -1.39 -3.55%
wp-template (p90) 40.15 39.07 -1.08 -2.69%
wp-total (p10) 68.47 66.85 -1.62 -2.42%
wp-total (p25) 69.49 67.45 -2.04 -3.02%
wp-total (p50) 70.48 68.3 -2.18 -3.19%
wp-total (p75) 71.56 69.64 -1.92 -2.68%
wp-total (p90) 73.91 74.3 0.39 0.53%

#9 @joemcgill
5 months ago

  • Resolution set to fixed
  • Status changed from assigned to closed

In 59256:

Editor: Cache global styles for blocks.

This caches the generated CSS from block nodes in merged Theme JSON data to avoid repeated costly operations required to compute style properties for blocks. The generated CSS is saved to a transient that expires every hour.

This is a follow-up that reimplements [58334], which was previously reverted in [58710].

Props thekt12, spacedmonkey, pereirinha, mukesh27, isabel_brison, oandregal, andrewserong, ramonjd, joemcgill, costdev, aaronrobertshaw, peterwilsoncc.
Fixes #61679. See #59595.

Note: See TracTickets for help on using tickets.