Make WordPress Core

Opened 23 months ago

Closed 18 months ago

Last modified 2 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:

Description

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 23 months ago.
Trunk - TT1
Screenshot 2023-05-09 at 14.50.28.png (16.3 KB) - added by spacedmonkey 23 months ago.
PR - TT1

Download all attachments as: .zip

Change History (18)

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


23 months ago
#1

  • Keywords has-patch added

#2 @spacedmonkey
23 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
23 months ago

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

PR - 1302 vs Trunk - 1949.

#4 in reply to: ↑ 3 @azaozz
23 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
19 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.


19 months ago

#7 @spacedmonkey
19 months ago

  • Owner spacedmonkey deleted

#8 @spacedmonkey
19 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.


19 months ago

#10 @spacedmonkey
19 months ago

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

Added unit test.

@spacedmonkey commented on PR #4413:


19 months ago
#11

@costdev @felixarntz Feedback actioned. Ready for review.

#12 @spacedmonkey
18 months ago

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

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

  • Keywords needs-dev-note added; needs-unit-tests needs-refresh removed

#16 @peterwilsoncc
2 months ago

In 59631:

Options/Meta APIs: Optimize cache hits for non-existent options.

Optimize the order of checking the various options caches in get_option() to prevent hitting external caches each time it is called for a known non-existent option.

The caches are checked in the following order when getting an option:

  1. Check the alloptions cache first to prioritize existing loaded options.
  2. Check the notoptions cache before a cache lookup or DB hit.
  3. Check the options cache prior to a DB hit.

Follow up to [56595].

Props adamsilverstein, flixos90, ivankristianto, joemcgill, rmccue, siliconforks, spacedmonkey.
Fixes #62692.
See #58277.

Note: See TracTickets for help on using tickets.