#57077 closed enhancement (fixed)
Performance issues in `_add_block_template_info`
Reported by: | spacedmonkey | Owned by: | |
---|---|---|---|
Milestone: | 6.2 | Priority: | normal |
Severity: | normal | Version: | 5.9 |
Component: | Themes | Keywords: | has-patch add-to-field-guide |
Focuses: | performance | Cc: |
Description
The function is _add_block_template_info
is called many times in an block theme. It has a number of performance issues, as it called WP_Theme_JSON_Resolver::get_theme_data
. See screenshots attached.
Attachments (4)
Change History (27)
This ticket was mentioned in PR #3609 on WordPress/wordpress-develop by @spacedmonkey.
2 years ago
#1
- Keywords has-patch added
#2
@
2 years ago
- Milestone changed from Awaiting Review to 6.1.1
Adding 6.1.1 as it is a simple fix and has serious benefit. 10% of page load for one page.
This ticket was mentioned in PR #3624 on WordPress/wordpress-develop by @spacedmonkey.
2 years ago
#3
Trac ticket: https://core.trac.wordpress.org/ticket/57077
#4
@
2 years ago
I have two solutions to this issue.
- Is simple but fix this problem. But doesn't solve the root problem.
https://github.com/WordPress/wordpress-develop/pull/3609
- Solves the problem at deeper level. https://github.com/WordPress/wordpress-develop/pull/3624
#5
@
2 years ago
In #3624, we see a 1/4 or 1/5 of the page load time disappear. Why does this happen?
Getting theme data, happens many times, including on getting template parts in block themes. This means that the theme_json_resolver class is called 100s of times. Without a cache, the same values are called 100s of times. In theme and supports data is generated just once and cached in a class property. As theme supports, also includes many calls to get_options, to get thumbnail sizes, it also means many less calls to database / caches.
I think this is an important change and one that needs to go into core ASAP, as this performance benefits are clear and will effect UX.
@spacedmonkey commented on PR #3609:
2 years ago
#6
Closing in favour of https://github.com/WordPress/wordpress-develop/pull/3624
#7
@
2 years ago
- Milestone changed from 6.1.1 to 6.1.2
Unfortunately, this was too late for 6.1.1 with the release slated in a few hours. But let's look at this for 6.1.2.
@peterwilsoncc commented on PR #3624:
2 years ago
#8
The test failures look legit https://github.com/WordPress/wordpress-develop/actions/runs/3470057716/jobs/5797787084#step:17:233
@spacedmonkey commented on PR #3624:
2 years ago
#9
@peterwilsoncc A simple workaround is to call, clean theme json caches, on add_theme_support and remove_theme_supports.
@spacedmonkey commented on PR #3624:
2 years ago
#10
@felixarntz @peterwilsoncc Spent some time on this PR. Caching block settings is harder than you might think. Block settings contain lots of calls to get_theme_supports
. So if a theme support is added / removed, the cached value is incorrect. As developer can and do, remove / add theme supports. To work around this, I have added two new actions and then clean caches for theme json with callback.
I don't love this, but I don't see anyway around this.
@spacedmonkey commented on PR #3624:
2 years ago
#11
@felixarntz I agree, I think this PR is a little bit of a none starter for now. I think we may have to refactor this logic so it is easier to invalidate.
@spacedmonkey commented on PR #3624:
2 years ago
#12
Closing as this PR is a none starter.
#13
@
2 years ago
There is a PR in progress to fix this issue. See #46112. I will work on a core patch once merged.
@SergeyBiryukov commented on PR #3609:
2 years ago
#14
Though I'm not super familiar with this code, the suggested change looks like an easy win.
I was wondering about _add_block_template_part_area_info()
. It has a similar line, does it needs the same change?
@spacedmonkey commented on PR #3609:
2 years ago
#15
@spacedmonkey Looks great, only pending @SergeyBiryukov's question.
@felixarntz Actioned the feedback.
#16
@
2 years ago
- Owner set to spacedmonkey
- Resolution set to fixed
- Status changed from new to closed
In 54998:
#18
@
2 years ago
- Owner spacedmonkey deleted
- Status changed from reopened to assigned
Reopening as this change needs to be backported to 6.1.2.
@spacedmonkey commented on PR #3609:
2 years ago
#19
Committed.
#20
@
22 months ago
Retroactively, an additional quick performance benchmark for this change, using https://gist.github.com/felixarntz/de5c697a1a16c2b892634b70216eb6c7:
With Twenty Twenty-Three home page:
_add_block_template_info()
: 0.37ms compared to 3.55ms (90.6% faster)_add_block_template_part_area_info()
: 3.94ms compared to 4.46ms (11.7% faster)- Total WP load time: 197.83ms compared to 203.07ms (2.6% faster)
With Twenty Twenty-Three single post with Gutenberg demo content:
_add_block_template_info()
: 0.18ms compared to 1.78ms (89.9% faster)_add_block_template_part_area_info()
: 3.87ms compared to 4.45ms (13.0% faster)- Total WP load time: 191.75ms compared to 198.65ms (3.5% faster)
All data based on median server timings from 20 requests each. This is a very nice little win through a simple change!
It also shows that _add_block_template_part_area_info()
overall is still pretty slow; maybe we can see if that can be improved further in another ticket.
#21
@
22 months ago
Conducted an additional experiment to try optimizing further, but not really worth it: https://github.com/WordPress/wordpress-develop/pull/3975
Sharing it just for visibility.
#22
@
22 months ago
- Milestone changed from 6.1.2 to 6.2
- Resolution set to fixed
- Status changed from assigned to closed
Since this enhancement was already committed to trunk
and we're nearing 6.2 Beta, we should probably close this.
There is no plan for a 6.1.2 release yet, and since this is an enhancement it is probably not suitable for that release anyway.
Trac ticket: https://core.trac.wordpress.org/ticket/57077