wordpress-develop icon indicating copy to clipboard operation
wordpress-develop copied to clipboard

Object caching `WP_Theme_JSON::compute_style_properties`

Open kt-12 opened this issue 1 year ago • 2 comments

Trac ticket: 59595

Incorporating feedback from - https://github.com/WordPress/wordpress-develop/pull/5567, I did the first version https://github.com/WordPress/wordpress-develop/pull/5567

theme_json based caching based on initial feeback ( commit )

  • The commit included using, theme_json for caching. However theme_json being non-persistent had no benefit. Instead, the cache key generation added additional overhead. This is because compute_style_properties( is called 85 times on the home page out of which 83 calls had a unique signature, so it was as good as having no cache at all + cache key generation overhead. The same -ve effect was found for the non-cached site. Overall the code resulted in a 6% degrade.
Screenshot 2024-04-15 at 14 44 44

No cache - All score below in (ms)

Trunk Response Time (median) - 399.62 After PR - 424.81 -- Note this cost is mainly due to cache key creation code run separately 85 times

Meme cache with theme_json non persistent cache

Trunk Response Time (median - 375.47 After PR with cache - 402.58

Switching to transient-based caching ( this commit )

The way around this was to use transient function-based caching. The idea was transient would override the -ve effect of overhead due to cache key creation on non-persistent sites and it would give the benefit of a persistent cache for other sites.

Testing the above concept this commit -

No cache

Trunk - 400 PR (transient cache) - 471.61 ( mainly because get_transient_site is costly) + cache_key code.

Meme cache

Trunk - 374 PR - 376 (benefit negated by wp_json_encode and md5 )

Improved transient caching through static caching (current state)

Meme cache

Trunk - 374 PR - 356 (~ 6% improvement )

No cache

Trunk - 400 PR (transient cache) - 384 ( ~4% improvement )


This Pull Request is for code review only. Please keep all other discussion in the Trac ticket. Do not merge this Pull Request. See GitHub Pull Requests for Code Review in the Core Handbook for more details.

kt-12 avatar Apr 15 '24 12:04 kt-12

Test using WordPress Playground

The changes in this pull request can previewed and tested using a WordPress Playground instance.

WordPress Playground is an experimental project that creates a full WordPress instance entirely within the browser.

Some things to be aware of

  • The Plugin and Theme Directories cannot be accessed within Playground.
  • All changes will be lost when closing a tab with a Playground instance.
  • All changes will be lost when refreshing the page.
  • A fresh instance is created each time the link below is clicked.
  • Every time this pull request is updated, a new ZIP file containing all changes is created. If changes are not reflected in the Playground instance, it's possible that the most recent build failed, or has not completed. Check the list of workflow runs to be sure.

For more details about these limitations and more, check out the Limitations page in the WordPress Playground documentation.

Test this pull request with WordPress Playground.

github-actions[bot] avatar Apr 15 '24 12:04 github-actions[bot]

The following accounts have interacted with this PR and/or linked issues. I will continue to update these lists as activity occurs. You can also manually ask me to refresh this list by adding the props-bot label.

Core Committers: Use this line as a base for the props when committing in SVN:

Props thekt12, joemcgill, spacedmonkey.

To understand the WordPress project's expectations around crediting contributors, please review the Contributor Attribution page in the Core Handbook.

github-actions[bot] avatar Apr 16 '24 14:04 github-actions[bot]

@kt-12 I spent some time looking over the unit tests and add a couple additional tests that I think could be useful in d0b243c. I could not think of a good way of ensuring that the cache was actually used in order to avoid unnecessary calls to get_styles_for_block() since we're not able to modify the WP_Theme_JSON_Resolver class hard coded in wp_get_global_styles_custom_css() nor the WP_Theme_JSON object that is returned by WP_Theme_JSON_Resolver::get_merged_data().

joemcgill avatar May 24 '24 17:05 joemcgill

This is looking good. Just doing some testing of the PR and I see a large improvement in the wp_add_global_styles_for_blocks() function. Some of the improvement is offset by the fact that the transient requires another DB query, but in persistent caches, those will also be avoided.

Trunk

image

This PR image

Some of the Unit test failures seem legitimate, so let's figure out those issues and this should be almost ready to go in.

joemcgill avatar May 29 '24 01:05 joemcgill

@kt-12 I went ahead and resolved conflicts and updated this branch with the fixes proposed in my other PR so will close that now. Looks like this has fixed the test failures we were seeing on this branch. Let's do some final checks, but I think this is about ready to commit.

joemcgill avatar Jun 03 '24 20:06 joemcgill

With the latest updates this is still profiling really well and showing a large improvement for wp_add_global_styles_for_blocks().

Trunk image

PR image

That said, those improvements are not currently reflected in the benchmarks performed by the Performance Workflow. I suspect it's because the extra query being performed to get the transient is offsetting the savings introduced in the function, but that's just a guess. Given the size of the potential improvement, it still may be worth committing so we can continue to do further testing during the beta period.

joemcgill avatar Jun 03 '24 22:06 joemcgill

Merged in 58334.

joemcgill avatar Jun 04 '24 14:06 joemcgill