Make WordPress Core

Opened 13 months ago

Closed 8 months ago

Last modified 8 months ago

#58277 closed enhancement (fixed)

Change the position of notoptions lookup in get_option

Reported by: spacedmonkey's profile spacedmonkey Owned by: spacedmonkey's profile spacedmonkey
Milestone: 6.4 Priority: normal
Severity: normal Version:
Component: Options, Meta APIs Keywords: has-patch needs-dev-note
Focuses: performance Cc:


Added in [4855] and #3726 the notoptions cache was added to get_option. This cache was designed to save database queries, do option name that are none not to exist in the database / cache.

However, the ordering this functions present a performance problem. In get_option the look for if a option does not exist, is done before if it does. It is extremely uncommon for a get_option, for a option name that does not exist. Most times it is called, it for a option that does exist. This means the call to notoptions cache is called pointless.

Moving this check, before the database lookup would improve performance.

Attachments (2)

Screenshot 2023-05-09 at 14.50.36.png (16.6 KB) - added by spacedmonkey 13 months ago.
Trunk - TT1
Screenshot 2023-05-09 at 14.50.28.png (16.3 KB) - added by spacedmonkey 13 months ago.
PR - TT1

Download all attachments as: .zip

Change History (17)

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

13 months ago

  • Keywords has-patch added

#2 @spacedmonkey
13 months ago

  • Keywords has-patch removed

500 runs on TT1

Trunk - TT1 PR - TT1 Trunk - TT1 - Memcached PR - TT1 - Memcached
Response Time (median) 73.16 73.1 73.62 72.49
wp-load-alloptions-query (median) 0.7 0.69 0.68 0.68
wp-before-template (median) 28.21 28.28 28.29 28.04
wp-before-template-db-queries (median) 2.76 2.78 2.76 2.75
wp-template (median) 37.73 37.64 38.11 37.62
wp-total (median) 65.91 66.1 66.63 65.87
wp-template-db-queries (median) 6.29 6.34 6.39 6.37

500 runs on TT3

Trunk - TT3 PR - TT3 Trunk - TT3 - Memcached PR - TT3 - Memcached
Response Time (median) 109.18 108.92 108.14 107.78
wp-before-template (median) 58.54 58.76 55.08 54.61
wp-template (median) 44.77 44.6 47.25 47.47
wp-total (median) 103.52 103.45 102.76 102.47

#3 follow-up: @spacedmonkey
13 months ago

In a block theme, this saves 100s of calls to cache.

PR - 1302 vs Trunk - 1949.

#4 in reply to: ↑ 3 @azaozz
13 months ago

Replying to spacedmonkey:

In a block theme, this saves 100s of calls to cache.

Yep, this change makes sense imho. I'm a bit unsure on how to best test the result. Seems it would have to compare getting existing and non-existing options (that would trigger DB connections). Perhaps something like get an existing option 1000 times, and 5 different non-existing options 3 times each, etc.

In any case reducing the number of cache lookups is a good thing, even if the speed improvements are minimal.

#5 @spacedmonkey
9 months ago

  • Milestone changed from Awaiting Review to 6.4
  • Owner set to spacedmonkey
  • Status changed from new to assigned

This ticket was mentioned in Slack in #core-performance by spacedmonkey. View the logs.

9 months ago

#7 @spacedmonkey
9 months ago

  • Owner spacedmonkey deleted

#8 @spacedmonkey
9 months ago

  • Keywords has-patch needs-unit-tests needs-refresh added

This ticket was mentioned in Slack in #core-performance by spacedmonkey. View the logs.

9 months ago

#10 @spacedmonkey
8 months ago

  • Owner set to flixos90
  • Status changed from assigned to reviewing

Added unit test.

@spacedmonkey commented on PR #4413:

8 months ago

@costdev @felixarntz Feedback actioned. Ready for review.

#12 @spacedmonkey
8 months ago

  • Owner changed from flixos90 to spacedmonkey
  • Status changed from reviewing to accepted

#13 @spacedmonkey
8 months ago

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

In 56595:

Options, Meta APIs: Optimize get_option by relocating notoptions cache lookup.

In the get_option function, a cache lookup for the notoptions key is performed, which stores an array of keys for options known not to exist. This optimization prevents repeated database queries when certain options are requested. However, the cache lookup for notoptions was conducted before checking if the requested option exists in the cache. Given that it's more likely that the option does exist, this commit reorders the checks to first verify the option's existence in the cache before confirming its absence. This adjustment reduces redundant queries and also eliminates an unnecessary cache lookup, improving overall performance.

Props spacedmonkey, costdev, flixos90, azaozz.
Fixes #58277.

#15 @spacedmonkey
8 months ago

  • Keywords needs-dev-note added; needs-unit-tests needs-refresh removed
Note: See TracTickets for help on using tickets.