-
Notifications
You must be signed in to change notification settings - Fork 107
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
Improve template loading and rendering #746
Comments
@joemcgill @kt-12 should we update the title here to say "Solutions to pursue for WP 6.5" instead of 6.4 now? |
Yes, I think so. |
@joemcgill Could we add https://core.trac.wordpress.org/ticket/59595 in the list? |
@mukeshpanchal27 yes, I think that makes sense. I'll add it to the misc section |
@joemcgill I started analysing WordPress/gutenberg#45601 But the original ticket(related work) seems to be fixed. I see a PR for this but there is no Trac Ticket for this can you check I might be missing something here |
@kt-12 I don't think there was ever a Trac ticket for that issue. The GB issue came out of profiling that showed the function, |
For Core-59315 the overall cost of file lookups for PHP template files are minimal and not worth trying to refactor. Closing as a |
Similarly, Core-59314 is a |
Issue - WordPress/gutenberg#45601
|
@kt-12 I've added https://core.trac.wordpress.org/ticket/59532 to the "misc improvements" section of this ticket, since it's closely related to the work you're planning to pick up on https://core.trac.wordpress.org/ticket/60120. |
@joemcgill Should we mark https://core.trac.wordpress.org/ticket/58196 as done, given that we have a static variable approach in place? Also, should we create a ticket to study |
Yup! Should have done when I closed that ticket. Thanks for spotting, @kt-12! |
@joemcgill Can we add it here as it is related to the theme_json effort? https://core.trac.wordpress.org/ticket/61112#ticket |
@kt-12 I think that makes sense. I've updated the description. |
At the conclusion of WordPress 6.6, we have accomplished almost everything we originally set out to fix with this epic. The only remaining issues that were not completed were:
In the former, we were able to resolve one of the biggest related performance issues, by caching Block Theme patterns in https://core.trac.wordpress.org/changeset/58025. However, doing something similar for block theme templates and template part files, did not show much improvement. Instead, the remaining opportunities for improving template loading and rendering that we have identified all seem to lead back to the same expensive processes that are a part of the WP_Theme_JSON system, where theme.json data is merged together from several different "origins" (core, theme, blocks, and user data) all of which can also be filtered by plugins. The complexities with this system make caching and invalidation a much larger challenge, which would be better handled as separate performance efforts. For that reason, I've crossed out those Trac tickets and am marking this overview issue as completed. |
Based on a performance analysis taken of WordPress 6.2, the WP Performance Team determined that there was significant opportunity to improve the way that WordPress loads files and executes logic associated with the template rendering process for both classic themes and block themes.
Problem Exploration
Classic Theme Context
Conceptually, when WordPress handles a request for a specific page, it will render the HTML to send back in the response by parsing the request to see which type of page should be rendered, and then build the page by locating and processing the various template parts that make up the page. This includes executing any procedural code, including hook callbacks, present in these template files.
For example, when rendering a post page in the Twenty Twenty-One theme, locate_template is first called by
get_query_template
, which loads thesingle.php
file, which then loads the header file, which loads a header template-part, which loads two more template-parts, etc. This process is repeated to render the content, and then the footer. The result (in this case) is 4 calls tolocate_template
, requiring the need to perform file operations, database queries, and procedural logic, just to render the page.Notably,
wp_head
—which is called in the header template of most themes in order to render markup inside the<head>
tag of the page—is responsible for a bulk of the execution time as of WP 6.3 (an apparent regression from previous versions).Due to the dynamic nature of WordPress, this whole process is repeated for every request, even though the results of this process rarely change between requests. On high-traffic sites, this overhead is often avoided by putting a full page cache in front of the WordPress application. However, a more granular way of caching and reusing specific template parts or even a whole page closer to the application does not currently exist.
Block Theme Context
For block themes, similar problems exist as described for classic themes. However, the template loading process is slightly different. Instead of locating and executing procedural code in PHP template files,
get_query_template
will first search for classic theme templates, before callinglocate_block_template
to see if a block template file exists and if any overrides are present as a custom post type in the database inresolve_block_template
.Once a block template is located, the content is stored in the
$_wp_current_template_content
global variable, where it is parsed into rendered block markup, and post processing is applied all within theget_the_block_template_html
function. This leads to two potential problems:get_the_block_template_html
, the same default filters that are added to hooks likethe_content
can get run twice, causing unnecessary double processing which adds performance overhead and even sometimes leads to bugs (see: https://core.trac.wordpress.org/ticket/55996).Projects for WP 6.4/6.5
Add caching for template file lookups
To avoid repeated
file_exists
operations for the same templates that rarely change, we could cache the result ofget_query_template
and load them directly on subsequent requests.Note: This effort should initially be time-boxed to include a discovery of whether PHPs internal stat cache renders the impact of these changes insignificant. See related conversation in: https://core.trac.wordpress.org/ticket/40731.
Related tickets:
59600 – Block themes: Use a cache for block templates filesMerge consideration of block-templates with classic template hierarchy
For block themes, WordPress will first attempt to locate the appropriate PHP template in the theme for the request before considering a block template. However, any block template that is found at an equal or higher priority takes precedence over the PHP templates. This means that block themes perform several unnecessary
file_exists
checks to look for a PHP template, when a block template is the desired result.Related tickets:
Reduce or eliminate cost of template part block instance variations
The function
build_template_part_block_instance_variations
is called when the template part block is registered, and is responsible for ~10% of the total server time for rendering a page in a block theme in WP 6.3 (profiling analysis). This is primarily due toget_block_templates()
, which is called internally by this function. That function performs two tasks that make up a bulk of the time: performing an expensive query and getting block template files (via_get_block_template_files()
which eventually results in an expensive call toWP_Theme_JSON_Resolver::get_theme_data
.Related tickets:
Make caching for global style sheets persistent
The function
wp_get_global_stylesheet
is responsible for ~10% of server execution time (WP 6.3 profiling data) in classic themes. While it is cached in some scenarios, it may not be a persistent cache, which creates a large server response time regression since WP 6.1 when the function was introduced.Related tickets:
57789 - Make theme.json related caches persistentwp_enqueue_global_styles()
for classic themesMiscellaneous improvements to template loading
While the above solutions focus on specific solutions, additional optimizations that reduce or eliminate unnecessary file operations are also worth pursuing.
Related tickets:
_get_block_templates_paths
WP_Theme_JSON
object from raw theme_jsonThe text was updated successfully, but these errors were encountered: