Remove additional call to `WP_Theme_JSON::_construct`
Trac ticket: Trac 61112 Corresponding Guttenberg PR - https://github.com/WordPress/gutenberg/pull/61262
This was found while analysing https://core.trac.wordpress.org/ticket/59600, but it relates mostly https://core.trac.wordpress.org/ticket/57789
In this PR we are trying to avoid second call to new WP_Theme_JSON( $theme_json_data ); as the data can be obtained from WP_Theme_JSON_data class using just a line above by introducing a new public method. At the micro level call to WP_Theme_JSON constructor is expensive.
Interestingly, I can see this change also improved static calls. I wasn't expecting that as in my opinion static lines are only executed once, however, this has challenged my view about static calls and might be we need to be mindful of the performance even while using static.
The execution time of the wp_get_theme_data_custom_templates function on the home page of TT4 was evaluated. Each page had two calls to the function. The second call was static cached so it was significantly lower than the time for the first call.
function wp_get_theme_data_custom_templates() {
// Capture the start time
$startTime = microtime(true);
$data = WP_Theme_JSON_Resolver::get_theme_data( array(), array( 'with_supports' => false ) )->get_custom_templates();
// Capture the end time
$endTime = microtime(true);
// Calculate the script execution time
$executionTime = $endTime - $startTime;
echo "<pre>Execution Time of `wp_get_theme_data_custom_templates`: " . $executionTime . " seconds.</pre>";
return $data;
}
The performance improvement was as noted below: Before PR
| Description | Execution Time (seconds) |
|---|---|
| Run 1 | |
Execution Time of wp_get_theme_data_custom_templates |
0.0033679008483887 |
Execution Time of wp_get_theme_data_custom_templates |
0.00087404251098633 |
| Run 2 | |
Execution Time of wp_get_theme_data_custom_templates |
0.003180980682373 |
Execution Time of wp_get_theme_data_custom_templates |
0.00077509880065918 |
| Run 3 | |
Execution Time of wp_get_theme_data_custom_templates |
0.0039951801300049 |
Execution Time of wp_get_theme_data_custom_templates |
0.00078392028808594 |
| Run 4 | |
Execution Time of wp_get_theme_data_custom_templates |
0.0032379627227783 |
Execution Time of wp_get_theme_data_custom_templates |
0.00077700614929199 |
| Run 5 | |
Execution Time of wp_get_theme_data_custom_templates |
0.003154993057251 |
Execution Time of wp_get_theme_data_custom_templates |
0.00075793266296387 |
| Run 6 | |
Execution Time of wp_get_theme_data_custom_templates |
0.0032570362091064 |
Execution Time of wp_get_theme_data_custom_templates |
0.00075292587280273 |
Mean of first call: 0.003366 Mean of static call: 0.000787
After PR
| Description | Execution Time (seconds) |
|---|---|
| Run 1 | |
Execution Time of wp_get_theme_data_custom_templates |
0.0030369758605957 |
Execution Time of wp_get_theme_data_custom_templates |
0.0004730224609375 |
| Run 2 | |
Execution Time of wp_get_theme_data_custom_templates |
0.0031139850616455 |
Execution Time of wp_get_theme_data_custom_templates |
0.00047683715820312 |
| Run 3 | |
Execution Time of wp_get_theme_data_custom_templates |
0.002918004989624 |
Execution Time of wp_get_theme_data_custom_templates |
0.00047898292541504 |
| Run 4 | |
Execution Time of wp_get_theme_data_custom_templates |
0.0026509761810303 |
Execution Time of wp_get_theme_data_custom_templates |
0.00044488906860352 |
| Run 5 | |
Execution Time of wp_get_theme_data_custom_templates |
0.0027339458465576 |
Execution Time of wp_get_theme_data_custom_templates |
0.00045418739318848 |
| Run 6 | |
Execution Time of wp_get_theme_data_custom_templates |
0.0029489994049072 |
Execution Time of wp_get_theme_data_custom_templates |
0.00049400329589844 |
Mean of first call: 0.002900 Mean of static call: 0.000470
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.
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, swissspidy, audrasjb, oandregal.
To understand the WordPress project's expectations around crediting contributors, please review the Contributor Attribution page in the Core Handbook.
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.
@joemcgill With the latest changes we are getting a consistent 2.5% improvement without breaking anything.
| Performance Metric | Trunk | PR | Improvement (%) |
|---|---|---|---|
| Response Time (median) | 387.75 | 379.09 | 2.23% |
| wp-before-template (median) | 151.2 | 145.18 | 3.98% |
| wp-before-template-db-queries (median) | 0 | 0 | 0 |
Oh, by the way. It'd be nice if this change would have been done first in the Gutenberg codebase. The value that provides is immense to all of us, including easier synchronization and quick feedback from Gutenberg users if we've missed something. I have nothing against merging it in wordpress-develop first, though I'd kindly ask that the authors port the same PR to Gutenberg. Until we figure out something better, this is what we collectively have and need to care for.
Oh, by the way. It'd be nice if this change would have been done first in the Gutenberg codebase. The value that provides is immense to all of us, including easier synchronization and quick feedback from Gutenberg users if we've missed something. I have nothing against merging it in wordpress-develop first, though I'd kindly ask that the authors port the same PR to Gutenberg. Until we figure out something better, this is what we collectively have and need to care for.
Thank you @oandregal for your feedback. We clearly understand your feedback about Gutenberg first approach, hence we have already created a PR for Gutenberg - https://github.com/WordPress/gutenberg/pull/61262 . Once it's tested and approved on Gutenberg we can then plan to merge it for the core.
https://github.com/WordPress/gutenberg/pull/61262
Thank you so much 🙏 I approved the Gutenberg PR.
By reviewing the Gutenberg PR I noticed that the core code is missing a check: see https://github.com/WordPress/wordpress-develop/pull/6271#discussion_r1589940324 and https://github.com/WordPress/gutenberg/pull/61262/files#r1589940214. I don't know why they have differed but that check is important and we should consolidate both codebases.
Merged in https://core.trac.wordpress.org/changeset/58185.