#58376 closed enhancement (fixed)
Improve performance of the `_wp_array_get` function
Reported by: | aristath | Owned by: | SergeyBiryukov |
---|---|---|---|
Milestone: | 6.3 | Priority: | high |
Severity: | normal | Version: | |
Component: | General | Keywords: | has-patch add-to-field-guide |
Focuses: | performance | Cc: |
Description
The _wp_array_get
function gets called ~23k times on each page-load when using a block theme. In fact, it's the most-called function on the frontend of a site.
There is room for improvement here, and any tweaks we make can be beneficial.
Attachments (1)
Change History (23)
This ticket was mentioned in PR #4471 on WordPress/wordpress-develop by @aristath.
17 months ago
#1
- Keywords has-patch added
#2
@
17 months ago
- Milestone changed from Awaiting Review to 6.3
- Priority changed from normal to high
This is well worth doing, as this function is call 12k+ times on many page loads.
Moving into 6.3.
@spacedmonkey commented on PR #4471:
17 months ago
#3
Brenchmarks.
Trunk
Response Time (median),109.43
wp-load-alloptions-query (median),0.61
wp-before-template (median),52.5
wp-before-template-db-queries (median),4.6
wp-template (median),50.45
wp-total (median),104.07
wp-template-db-queries (median),2.55
PR - e2f5b7562af632fcdefd0cfc92a7aa11e72158d6
Response Time (median),108.4
wp-load-alloptions-query (median),0.62
wp-before-template (median),51.75
wp-before-template-db-queries (median),4.54
wp-template (median),50.01
wp-total (median),102.57
wp-template-db-queries (median),2.51
#4
@
17 months ago
I also conducted two benchmarks, one for TT3 (block theme) and one for TT1 (classic theme). Results look positive indeed, it's a small enhancement, but it shows consistently, improving overall WordPress execution time by ~1%.
- Block theme
wp-total
improved from 158.34ms to 156.49ms (1.17%) - Classic theme
wp-total
improved from 58.42ms to 57.55ms (1.49%)
Here are the full results: https://docs.google.com/spreadsheets/d/1nn12aoFpgqF6F8o20hLExhnanL_-dShAkLjaItKsxcI/edit
@flixos90 commented on PR #4471:
17 months ago
#5
Shared another benchmark for block theme and classic theme in https://core.trac.wordpress.org/ticket/58376#comment:4 - this looks like a nice little enhancement indeed!
#7
follow-up:
↓ 8
@
17 months ago
While speeding up _wp_array_get
is nice, maybe we should be asking how to cut ~23k calls down to something manageable. Why is it called that many times per screen load?
#8
in reply to:
↑ 7
@
17 months ago
Replying to samiamnot:
While speeding up
_wp_array_get
is nice, maybe we should be asking how to cut ~23k calls down to something manageable. Why is it called that many times per screen load?
That was one of my first thoughts here too, perhaps we could optimize that as well as part of the performance focus.
@SergeyBiryukov commented on PR #4471:
17 months ago
#10
Thanks for the PR! Merged in r55851.
#11
follow-up:
↓ 12
@
17 months ago
Here are some results from a quick check loading the frontend while logged in as an admin (TT3 active, no plugins active).
Total calls to _wp_array_get()
: 23873
Top 5:
- 9849 calls -
WP_Theme_JSON::merge()
- 3559 calls -
WP_Theme_JSON::get_property_value()
- 3283 calls -
WP_Theme_JSON::get_default_slugs()
- 1340 calls -
WP_Theme_JSON::get_metadata_boolean()
- 1129 calls -
block_has_support()
- These top 5 account for 19160 calls, or ~80%.
#12
in reply to:
↑ 11
@
17 months ago
Thank you @costdev . Fascinating. Can I assume that a legacy theme like TwentyFifteen or TwentySeventeen would have only a fraction of the calls to _wp_array_get()
? @flixos90's numbers above seem to show classic themes being quicker overall, and I wonder how much is impacted by _wp_array_get()
calls. Do we know what the second most frequently called function is for both classic and block themes?
Replying to costdev:
Here are some results from a quick check loading the frontend - TT3 active, no plugins active.
Total calls to
_wp_array_get()
: 23873
Top 5:
- 9849 calls -
WP_Theme_JSON::merge()
- 3559 calls -
WP_Theme_JSON::get_property_value()
- 3283 calls -
WP_Theme_JSON::get_default_slugs()
- 1340 calls -
WP_Theme_JSON::get_metadata_boolean()
- 1129 calls -
block_has_support()
- These top 5 account for 19160 calls, or ~80%.
#13
@
17 months ago
Same results for Twenty Fifteen and Twenty Seventeen
Total calls to _wp_array_get()
: 4418
Top 5:
- 928 calls -
WP_Theme_JSON::merge()
- 736 calls -
::wp_migrate_old_typography_shape()
- 703 calls -
::block_has_support()
- 588 calls -
::wp_register_typography_support()
- 390 calls -
::wp_register_colors_support()
- These top 5 account for 3345 calls, or ~75%.
#14
@
17 months ago
@costdev Are you testing with WP_DEBUG
enabled? If so, those numbers will be wrong. WP_DEBUG
disabled some of the core caches and results in these numbers being higher than in production.
#15
@
17 months ago
@costdev,
Thanks. It is interesting that there are 928 calls to WP_Theme_JSON::merge()
when those themes do not even have a themes.json
file as far as I know. Why ::wp_migrate_old_typography_shape()
is called so may times seems strange to me.
#16
@
17 months ago
@spacedmonkey Ah true! I disabled WP_DEBUG
and output the results directly to the screen, this showed 9599 calls for Twenty-Twenty Three, and 3746 for Twenty Seventeen. What numbers are you getting?
#17
@
16 months ago
@costdev The number depends on the blocks on the page. There is no exact number. There are many more calls in block themes.
#18
follow-up:
↓ 19
@
16 months ago
Regardless of the actual number of calls the count of calls to _wp_array_get
seems excessive. Is there a way to reduce it?
#19
in reply to:
↑ 18
@
16 months ago
Replying to samiamnot:
Regardless of the actual number of calls the count of calls to
_wp_array_get
seems excessive. Is there a way to reduce it?
Yes, there are ways to reduce the number of calls to that function. I currently have a PR in Gutenberg to do just that - see https://github.com/WordPress/gutenberg/pull/51116 for more info.
It's still awaiting a review, but I'm hoping we can get this merged soon.
#20
@
16 months ago
This function is called in a lot of places. In the most in.
block_has_support - 2091
wp_should_skip_block_supports_serialization - 731
Love to fix these places.
Trac ticket: https://core.trac.wordpress.org/ticket/58376
Props @SergeyBiryukov @afercia @jrfnl