Make WordPress Core

Opened 18 months ago

Closed 16 months ago

Last modified 15 months ago

#57077 closed enhancement (fixed)

Performance issues in `_add_block_template_info`

Reported by: spacedmonkey's profile 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:


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)

Screenshot 2022-11-11 at 16.15.05.png (137.0 KB) - added by spacedmonkey 18 months ago.
Screenshot 2022-11-11 at 16.53.00.png (48.5 KB) - added by spacedmonkey 18 months ago.
Screenshot 2022-11-11 at 16.52.55.png (51.1 KB) - added by spacedmonkey 18 months ago.
Screenshot 2022-11-14 at 22.56.07.png (19.2 KB) - added by spacedmonkey 18 months ago.
Result from -

Download all attachments as: .zip

Change History (27)

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

18 months ago

  • Keywords has-patch added

#2 @spacedmonkey
18 months 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.

#4 @spacedmonkey
18 months ago

I have two solutions to this issue.

  1. Is simple but fix this problem. But doesn't solve the root problem.

  1. Solves the problem at deeper level.

#5 @spacedmonkey
18 months 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.

#7 @desrosj
18 months 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.

@spacedmonkey commented on PR #3624:

18 months ago

@peterwilsoncc A simple workaround is to call, clean theme json caches, on add_theme_support and remove_theme_supports.

@spacedmonkey commented on PR #3624:

18 months ago

@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:

18 months ago

@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:

18 months ago

Closing as this PR is a none starter.

#13 @spacedmonkey
18 months 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:

17 months ago

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:

17 months ago

@spacedmonkey Looks great, only pending @SergeyBiryukov's question.

@felixarntz Actioned the feedback.

#16 @spacedmonkey
17 months ago

  • Owner set to spacedmonkey
  • Resolution set to fixed
  • Status changed from new to closed

In 54998:

Themes: Improve performance of _add_block_template_part_area_info and _add_block_template_info functions.

Improve performance of _add_block_template_part_area_info and _add_block_template_info function by passing the with_supports parameter to the WP_Theme_JSON_Resolver::get_theme_data method. This results in hitting an existing cache and in many less calls to get_option.

Props spacedmonkey, flixos90, SergeyBiryukov, peterwilsoncc, desrosj.
Fixes #57077.

#17 @spacedmonkey
17 months ago

  • Resolution fixed deleted
  • Status changed from closed to reopened

#18 @spacedmonkey
17 months 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:

17 months ago


#20 @flixos90
16 months ago

Retroactively, an additional quick performance benchmark for this change, using

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 @flixos90
16 months ago

Conducted an additional experiment to try optimizing further, but not really worth it:

Sharing it just for visibility.

#22 @flixos90
16 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.

#23 @milana_cap
15 months ago

  • Keywords add-to-field-guide added
Note: See TracTickets for help on using tickets.