Skip to:
Content

bbPress.org


Ignore:
Timestamp:
06/14/2017 06:45:49 PM (9 years ago)
Author:
johnjamesjacoby
Message:

Performance: Last pass at 2.6 performance tuning.

  • Keep a local cache of non-options that aren't in the database, to avoid multiple database misses for options we know aren't in the database after wp_load_alloptions() is called
  • Stop getting all favorite IDs and subscription IDs when checking if a user has favorited or subscribed to something, because these queries are expensive joins that are difficult to cache & invalidate
  • Consolidate forum/topic favorites & subscriptions logic back into central routers, to make it easier to handle taxonomy term subscriptions in the future, and remove nested filter calls that make the call-stack confusing
  • Informally deprecate some forum & topic specific fav/sub functions
  • Rename one of the engagements remove functions to better match existing naming pattern
  • Typo fixes & general code quality improvements
  • Bump slow tests threshold up to 500 for now, and we can bring back down later (my 12" MacBook runs this pretty slowly, so I'd like to play with this more)
  • Unit tests are all passing, and more quickly, maybe surprisingly

This should result in around 20 fewer database hits on non-persistent-cache sites, on average. When viewing single topics & replies, this will result in more than 25 fewer database hits.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/src/includes/core/options.php

    r6514 r6544  
    2323
    2424    // Filter & return
    25     return apply_filters( 'bbp_get_default_options', array(
     25    return (array) apply_filters( 'bbp_get_default_options', array(
    2626
    2727        /** DB Version ********************************************************/
     
    8484
    8585        '_bbp_user_slug'            => 'users',         // User profile slug
     86        '_bbp_user_engs_slug'       => 'engagements',   // User engagements slug
    8687        '_bbp_user_favs_slug'       => 'favorites',     // User favorites slug
    8788        '_bbp_user_subs_slug'       => 'subscriptions', // User subscriptions slug
     
    112113
    113114        '_bbp_enable_akismet'       => 1,           // Users from all sites can post
    114        
     115
    115116        /** Converter *********************************************************/
    116117
     
    193194    // Add filters to each bbPress option
    194195    foreach ( array_keys( bbp_get_default_options() ) as $key ) {
    195         add_filter( 'pre_option_' . $key, 'bbp_pre_get_option' );
     196        add_filter( 'pre_option_'     . $key, 'bbp_filter_pre_get_option', 10, 2 );
     197        add_filter( 'default_option_' . $key, 'bbp_filter_default_option', 10, 3 );
    196198    }
    197199
     
    201203
    202204/**
    203  * Filter default options and allow them to be overloaded from inside the
    204  * $bbp->options array.
     205 * Filter pre options and maybe overloaded from the $bbp->options array.
     206 *
     207 * This function should not be called directly.
    205208 *
    206209 * @since 2.0.0 bbPress (r3451)
    207  *
    208  * @param bool $value Optional. Default value false
     210 * @access private
     211 *
     212 * @param bool   $value  Default value false
     213 * @param string $option Name of the option
     214 *
    209215 * @return mixed false if not overloaded, mixed if set
    210216 */
    211 function bbp_pre_get_option( $value = '' ) {
    212 
    213     // Remove the filter prefix
    214     $option = str_replace( 'pre_option_', '', current_filter() );
     217function bbp_filter_pre_get_option( $value = false, $option = '' ) {
    215218
    216219    // Check the options global for preset value
     
    223226}
    224227
     228/**
     229 * Filter default_options set them from inside the $bbp->options array.
     230 *
     231 * This function should not be called directly.
     232 *
     233 * @since 2.6.0 bbPress (r3451)
     234 * @access private
     235 *
     236 * @param bool $value Optional. Default value false
     237 * @return mixed false if not overloaded, mixed if set
     238 */
     239function bbp_filter_default_option( $default = false, $option = '', $passed_default = false ) {
     240    $options = bbp_get_default_options();
     241
     242    // Maybe use the default value
     243    if ( isset( $options[ $option ] ) ) {
     244
     245        // Try to use the passed default and fallback to assumed default
     246        $default = ( true === $passed_default )
     247            ? $default
     248            : $options[ $option ];
     249    }
     250
     251    // Always return a value, even if false
     252    return $default;
     253}
     254
     255/**
     256 * Loads & caches bbPress options if a persistent cache is not being used.
     257 *
     258 * @since 2.6.0
     259 */
     260function bbp_pre_load_options() {
     261
     262    // Bail if using object cache or installing
     263    if ( wp_using_ext_object_cache() || wp_installing() ) {
     264        return;
     265    }
     266
     267    // Bail if strategy is overloaded to false|null
     268    $strategy = apply_filters( 'bbp_pre_load_options', 'notoptions' );
     269    if ( empty( $strategy ) ) {
     270        return;
     271    }
     272
     273    // Get variables
     274    $bbp         = bbpress();
     275    $bbp_options = bbp_get_default_options();
     276    $all_options = wp_load_alloptions();
     277    $not_options = (array) wp_cache_get( 'notoptions', 'options' );
     278
     279    // Loop through all bbPress options to maybe cache their non-existence
     280    foreach ( $bbp_options as $option => $value ) {
     281
     282        // Skip if already saved to database
     283        if ( isset( $all_options[ $option ] ) ) {
     284            continue;
     285
     286        // Skip if overloaded
     287        } elseif ( isset( $bbp->options[ $option ] ) ) {
     288            continue;
     289
     290        // Skip if already in cache
     291        } elseif ( wp_cache_get( $option, 'options' ) !== false ) {
     292            continue;
     293
     294        // Needs caching to avoid database hit
     295        } else {
     296
     297            // Store internally, for easier identification later
     298            $bbp->not_options[ $option ] = $value;
     299
     300            // Cache to notoptions
     301            if ( 'notoptions' === $strategy ) {
     302                $not_options[ $option ] = true;
     303                wp_cache_set( 'notoptions', $not_options, 'options' );
     304
     305            // Cache to option
     306            } elseif ( 'option' === $strategy ) {
     307                wp_cache_set( $option, $value, 'options' );
     308            }
     309        }
     310    }
     311}
     312
    225313/** Active? *******************************************************************/
    226314
     
    230318 * @since 2.0.0 bbPress (r2658)
    231319 *
    232  * @param $default bool Optional.Default value true
     320 * @param bool $default Optional.Default value true
    233321 * @uses get_option() To get the favorites option
    234322 * @return bool Is favorites enabled or not
     
    245333 * @since 2.0.0 bbPress (r2658)
    246334 *
    247  * @param $default bool Optional.Default value true
     335 * @param bool $default Optional.Default value true
    248336 * @uses get_option() To get the subscriptions option
    249337 * @return bool Is subscription enabled or not
     
    260348 * @since 2.6.0 bbPress (r6320)
    261349 *
    262  * @param $default bool Optional.Default value true
     350 * @param bool $default Optional.Default value true
    263351 * @uses get_option() To get the engagements option
    264352 * @return bool Is engagements enabled or not
     
    275363 * @since 2.6.0 bbPress (r6441)
    276364 *
    277  * @param $default bool Optional. Default value false
     365 * @param bool $default Optional. Default value false
    278366 * @uses get_option() To get the global content edit option
    279367 * @return bool Is content editing allowed?
     
    290378 * @since 2.6.0 bbPress (r6441)
    291379 *
    292  * @param $default bool Optional. Default value false
     380 * @param bool $default Optional. Default value false
    293381 * @uses get_option() To get the content throttle  option
    294382 * @return bool Is content throttling allowed?
     
    305393 * @since 2.2.0 bbPress (r4097)
    306394 *
    307  * @param $default bool Optional. Default value true
     395 * @param bool $default Optional. Default value true
    308396 * @uses get_option() To get the allow tags
    309397 * @return bool Are tags allowed?
     
    336424 * @since 2.4.0 bbPress (r4970)
    337425 *
    338  * @param $default bool Optional. Default value true
     426 * @param bool $default Optional. Default value true
    339427 * @uses get_option() To get the forum-wide search setting
    340428 * @return bool Is forum-wide searching allowed?
     
    351439 * @since 2.4.0 bbPress (r4964)
    352440 *
    353  * @param $default bool Optional. Default value false
     441 * @param bool $default Optional. Default value false
    354442 * @uses get_option() To get the threaded replies setting
    355443 * @return bool Are threaded replies allowed?
     
    383471 * @since 2.0.0 bbPress (r3412)
    384472 *
    385  * @param $default bool Optional. Default value true
     473 * @param bool $default Optional. Default value true
    386474 * @uses get_option() To get the allow revisions
    387475 * @return bool Are revisions allowed?
     
    398486 * @since 2.0.0 bbPress (r2659)
    399487 *
    400  * @param $default bool Optional. Default value
     488 * @param bool $default Optional. Default value
    401489 * @uses get_option() To get the allow anonymous option
    402490 * @return bool Is anonymous posting allowed?
     
    413501 * @since 2.0.0 bbPress (r3378)
    414502 *
    415  * @param $default bool Optional. Default value false
     503 * @param bool $default Optional. Default value false
    416504 * @uses get_option() To get the global access option
    417505 * @return bool Is global access allowed?
     
    428516 * @since 2.2.0 bbPress (r4294)
    429517 *
    430  * @param $default string Optional. Default value empty
     518 * @param string $default Optional. Default value empty
    431519 * @uses get_option() To get the default forums role option
    432520 * @return string The default forums user role
     
    443531 * @since 2.0.0 bbPress (r3386)
    444532 *
    445  * @param $default bool Optional. Default value true
     533 * @param bool $default Optional. Default value true
    446534 * @uses get_option() To get the WP editor option
    447535 * @return bool Use WP editor?
     
    458546 * @since 2.1.0 bbPress (r3752)
    459547 *
    460  * @param $default bool Optional. Default value true
     548 * @param bool $default Optional. Default value true
    461549 * @uses get_option() To get the oEmbed option
    462550 * @return bool Use oEmbed?
     
    473561 * @since 2.1.0 bbPress (r3829)
    474562 *
    475  * @param $default string Optional. Default value 'default'
     563 * @param string $default Optional. Default value 'default'
    476564 * @uses get_option() To get the theme-package option
    477565 * @return string ID of the theme-package
     
    488576 * @since 2.0.0 bbPress (r3246)
    489577 *
    490  * @param $default bool Optional. Default value 80
     578 * @param bool $default Optional. Default value 80
    491579 */
    492580function bbp_title_max_length( $default = 80 ) {
     
    498586     * @since 2.0.0 bbPress (r3246)
    499587     *
    500      * @param $default bool Optional. Default value 80
     588     * @param bool $default Optional. Default value 80
    501589     * @uses get_option() To get the maximum title length
    502590     * @return int Is anonymous posting allowed?
     
    513601 * @since 2.1.0 bbPress (r3575)
    514602 *
    515  * @param $default int Optional. Default value
     603 * @param int $default Optional. Default value
    516604 */
    517605function bbp_group_forums_root_id( $default = 0 ) {
     
    523611     * @since 2.1.0 bbPress (r3575)
    524612     *
    525      * @param $default bool Optional. Default value 0
     613     * @param bool $default Optional. Default value 0
    526614     * @uses get_option() To get the root group forum ID
    527615     * @return int The post ID for the root forum
     
    538626 * @since 2.1.0 bbPress (r3575)
    539627 *
    540  * @param $default bool Optional. Default value true
     628 * @param bool $default Optional. Default value true
    541629 * @uses get_option() To get the group forums option
    542630 * @return bool Is group forums enabled or not
     
    553641 * @since 2.1.0 bbPress (r3575)
    554642 *
    555  * @param $default bool Optional. Default value true
     643 * @param bool $default Optional. Default value true
    556644 * @uses get_option() To get the Akismet option
    557645 * @return bool Is Akismet enabled or not
     
    573661 * @since 2.4.0 bbPress (r4932)
    574662 *
    575  * @param $default bool Optional. Default value false
     663 * @param bool $default Optional. Default value false
    576664 * @uses get_option() To get the admin integration setting
    577665 * @return bool To deeply integrate settings, or not
     
    605693 * @since 2.1.0 bbPress (r3759)
    606694 *
     695 * @param string $default Optional. Default value 'forums'
     696 * @uses get_option() To get the slug
    607697 * @return string
    608698 */
     
    618708 * @since 2.1.0 bbPress (r3759)
    619709 *
     710 * @param bool $default Optional. Default value true
     711 * @uses get_option() To get the setting
    620712 * @return bool
    621713 */
     
    631723 * @since 2.4.0 bbPress (r4932)
    632724 *
     725 * @param string $default Optional. Default value 'forums'
     726 * @uses get_option() To get the setting
    633727 * @return string
    634728 */
     
    644738 * @since 2.1.0 bbPress (r3759)
    645739 *
     740 * @param string $default Optional. Default value 'forums'
     741 * @uses get_option() To get the slug
    646742 * @return string
    647743 */
    648744function bbp_maybe_get_root_slug() {
    649     $retval = '';
    650 
    651     if ( bbp_get_root_slug() && bbp_include_root_slug() ) {
    652         $retval = trailingslashit( bbp_get_root_slug() );
    653     }
     745    $slug   = bbp_get_root_slug();
     746    $retval = ( ! empty( $slug ) && bbp_include_root_slug() )
     747        ? trailingslashit( $slug )
     748        : '';
    654749
    655750    // Filter & return
     
    662757 * @since 2.1.0 bbPress (r3759)
    663758 *
     759 * @param string $default Optional. Default value 'forum'
     760 * @uses get_option() To get the slug
    664761 * @return string
    665762 */
     
    675772 * @since 2.1.0 bbPress (r3759)
    676773 *
     774 * @param string $default Optional. Default value 'topics'
     775 * @uses get_option() To get the slug
    677776 * @return string
    678777 */
     
    688787 * @since 2.4.0 bbPress (r4925)
    689788 *
     789 * @param string $default Optional. Default value 'replies'
     790 * @uses get_option() To get the slug
    690791 * @return string
    691792 */
     
    701802 * @since 2.1.0 bbPress (r3759)
    702803 *
     804 * @param string $default Optional. Default value 'topic'
     805 * @uses get_option() To get the slug
    703806 * @return string
    704807 */
     
    714817 * @since 2.1.0 bbPress (r3759)
    715818 *
     819 * @param string $default Optional. Default value 'topic-tag'
     820 * @uses get_option() To get the slug
    716821 * @return string
    717822 */
     
    727832 * @since 2.1.0 bbPress (r3759)
    728833 *
     834 * @param string $default Optional. Default value 'reply'
     835 * @uses get_option() To get the slug
    729836 * @return string
    730837 */
     
    740847 * @since 2.1.0 bbPress (r3759)
    741848 *
     849 * @param string $default Optional. Default value 'users'
     850 * @uses get_option() To get the slug
    742851 * @return string
    743852 */
     
    753862 * @since 2.2.0 bbPress (r4187)
    754863 *
     864 * @param string $default Optional. Default value 'favorites'
     865 * @uses get_option() To get the slug
    755866 * @return string
    756867 */
     
    766877 * @since 2.2.0 bbPress (r4187)
    767878 *
     879 * @param string $default Optional. Default value 'subscriptions'
     880 * @uses get_option() To get the slug
    768881 * @return string
    769882 */
     
    779892 * @since 2.6.0 bbPress (r6320)
    780893 *
     894 * @param string $default Optional. Default value 'engagements'
     895 * @uses get_option() To get the slug
    781896 * @return string
    782897 */
     
    784899
    785900    // Filter & return
    786     return apply_filters( 'bbp_get_user_engagements_slug', get_option( '_bbp_user_engagements_slug', $default ) );
     901    return apply_filters( 'bbp_get_user_engagements_slug', get_option( '_bbp_user_engs_slug', $default ) );
    787902}
    788903
     
    792907 * @since 2.1.0 bbPress (r3759)
    793908 *
     909 * @param string $default Optional. Default value 'view'
     910 * @uses get_option() To get the slug
    794911 * @return string
    795912 */
     
    805922 * @since 2.3.0 bbPress (r4579)
    806923 *
     924 * @param string $default Optional. Default value 'search'
     925 * @uses get_option() To get the slug
    807926 * @return string
    808927 */
     
    820939 * @since 2.1.0 bbPress (r3790)
    821940 *
    822  * @param $default string Optional. Default empty string
     941 * @param string $default Optional. Default empty string
    823942 * @uses get_option() To get the old bb-config.php location
    824943 * @return string The location of the bb-config.php file, if any
Note: See TracChangeset for help on using the changeset viewer.