Make WordPress Core

Opened 12 months ago

Closed 12 months ago

Last modified 11 months ago

#58376 closed enhancement (fixed)

Improve performance of the `_wp_array_get` function

Reported by: aristath's profile aristath Owned by: sergeybiryukov's profile 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)

Screenshot 2023-05-23 at 13.08.02.png (85.6 KB) - added by spacedmonkey 12 months ago.

Download all attachments as: .zip

Change History (23)

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


12 months ago
#1

  • Keywords has-patch added

Trac ticket: https://core.trac.wordpress.org/ticket/58376

Props @SergeyBiryukov @afercia @jrfnl

#2 @spacedmonkey
12 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:


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


12 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!

#6 @SergeyBiryukov
12 months ago

  • Owner set to SergeyBiryukov
  • Status changed from new to accepted

#7 follow-up: @samiamnot
12 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 @SergeyBiryukov
12 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.

#9 @SergeyBiryukov
12 months ago

  • Resolution set to fixed
  • Status changed from accepted to closed

In 55851:

General: Improve performance of the _wp_array_get() function.

When using a block theme, _wp_array_get() is the most called function on the front end of a site.

This commit makes a few minor performance optimizations, which add up to a noticeable improvement.

Follow-up to [49135], [49143], [49580].

Props aristath, jrf, afercia, costdev, swissspidy, flixos90, spacedmonkey, mukesh27, samiamnot, SergeyBiryukov.
Fixes #58376.

@SergeyBiryukov commented on PR #4471:


12 months ago
#10

Thanks for the PR! Merged in r55851.

#11 follow-up: @costdev
12 months ago

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%.

Note: Before editing this comment, I previously mentioned that I was logged in as an admin. I tried without being logged in and can verify that this made no difference to the number of calls.

Last edited 12 months ago by costdev (previous) (diff)

#12 in reply to: ↑ 11 @samiamnot
12 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 @costdev
12 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 @spacedmonkey
12 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 @samiamnot
12 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 @costdev
12 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?

Version 0, edited 12 months ago by costdev (next)

#17 @spacedmonkey
12 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: @samiamnot
12 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 @aristath
12 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 @spacedmonkey
12 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.

#21 @flixos90
12 months ago

@spacedmonkey @aristath I opened #58532 as a more specific follow up ticket. Would be great if you could take a look and share your thoughts or maybe start a PR.

#22 @milana_cap
11 months ago

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