Make WordPress Core


Ignore:
Timestamp:
09/15/2023 04:13:52 PM (10 months ago)
Author:
spacedmonkey
Message:

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.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/src/wp-includes/option.php

    r56508 r56595  
    162162
    163163    if ( ! wp_installing() ) {
    164         // Prevent non-existent options from triggering multiple queries.
    165         $notoptions = wp_cache_get( 'notoptions', 'options' );
    166 
    167         // Prevent non-existent `notoptions` key from triggering multiple key lookups.
    168         if ( ! is_array( $notoptions ) ) {
    169             $notoptions = array();
    170             wp_cache_set( 'notoptions', $notoptions, 'options' );
    171         }
    172 
    173         if ( isset( $notoptions[ $option ] ) ) {
    174             /**
    175              * Filters the default value for an option.
    176              *
    177              * The dynamic portion of the hook name, `$option`, refers to the option name.
    178              *
    179              * @since 3.4.0
    180              * @since 4.4.0 The `$option` parameter was added.
    181              * @since 4.7.0 The `$passed_default` parameter was added to distinguish between a `false` value and the default parameter value.
    182              *
    183              * @param mixed  $default_value  The default value to return if the option does not exist
    184              *                               in the database.
    185              * @param string $option         Option name.
    186              * @param bool   $passed_default Was `get_option()` passed a default value?
    187              */
    188             return apply_filters( "default_option_{$option}", $default_value, $option, $passed_default );
    189         }
    190 
    191164        $alloptions = wp_load_alloptions();
    192165
     
    197170
    198171            if ( false === $value ) {
     172                // Prevent non-existent options from triggering multiple queries.
     173                $notoptions = wp_cache_get( 'notoptions', 'options' );
     174
     175                // Prevent non-existent `notoptions` key from triggering multiple key lookups.
     176                if ( ! is_array( $notoptions ) ) {
     177                    $notoptions = array();
     178                    wp_cache_set( 'notoptions', $notoptions, 'options' );
     179                } elseif ( isset( $notoptions[ $option ] ) ) {
     180                    /**
     181                     * Filters the default value for an option.
     182                     *
     183                     * The dynamic portion of the hook name, `$option`, refers to the option name.
     184                     *
     185                     * @since 3.4.0
     186                     * @since 4.4.0 The `$option` parameter was added.
     187                     * @since 4.7.0 The `$passed_default` parameter was added to distinguish between a `false` value and the default parameter value.
     188                     *
     189                     * @param mixed  $default_value  The default value to return if the option does not exist
     190                     *                               in the database.
     191                     * @param string $option         Option name.
     192                     * @param bool   $passed_default Was `get_option()` passed a default value?
     193                     */
     194                    return apply_filters( "default_option_{$option}", $default_value, $option, $passed_default );
     195                }
     196
    199197                $row = $wpdb->get_row( $wpdb->prepare( "SELECT option_value FROM $wpdb->options WHERE option_name = %s LIMIT 1", $option ) );
    200198
     
    204202                    wp_cache_add( $option, $value, 'options' );
    205203                } else { // Option does not exist, so we must cache its non-existence.
    206                     if ( ! is_array( $notoptions ) ) {
    207                         $notoptions = array();
    208                     }
    209 
    210204                    $notoptions[ $option ] = true;
    211205                    wp_cache_set( 'notoptions', $notoptions, 'options' );
Note: See TracChangeset for help on using the changeset viewer.