Make WordPress Core

Changeset 55749


Ignore:
Timestamp:
05/11/2023 12:25:51 PM (14 months ago)
Author:
spacedmonkey
Message:

Comments: Always lazily load comment meta.

In [34270] introduced lazy loading of comment meta. However, this was only in the context of WP_Query. Other parts of the codebase, like WP_Comment_Query did not lazily load comment meta. In this change, calls to update_meta_cache are now replaced with wp_lazyload_comment_meta, that instead of priming comment meta caches, just adds them to the queue to be primed it ever called. This results in far less database queries, as there a number of places where comment meta is being primed unnecessarily and never used. Adding everything to the comment meta queue, also means that if comment meta is used, that is all loaded in a single database / cache call.

Follow on from [55671], [55747].

Props spacedmonkey, peterwilsoncc, flixos90, mukesh27.
Fixes #57801.

Location:
trunk
Files:
6 edited

Legend:

Unmodified
Added
Removed
  • trunk/src/wp-admin/includes/class-wp-comments-list-table.php

    r55732 r55749  
    166166
    167167        if ( is_array( $_comments ) ) {
    168             update_comment_cache( $_comments );
    169 
    170168            $this->items       = array_slice( $_comments, 0, $comments_per_page );
    171169            $this->extra_items = array_slice( $_comments, $comments_per_page );
  • trunk/src/wp-includes/class-wp-comment-query.php

    r55607 r55749  
    482482        $comment_ids = array_map( 'intval', $comment_ids );
    483483
     484        if ( $this->query_vars['update_comment_meta_cache'] ) {
     485            wp_lazyload_comment_meta( $comment_ids );
     486        }
     487
    484488        if ( 'ids' === $this->query_vars['fields'] ) {
    485489            $this->comments = $comment_ids;
     
    487491        }
    488492
    489         _prime_comment_caches( $comment_ids, $this->query_vars['update_comment_meta_cache'] );
     493        _prime_comment_caches( $comment_ids, false );
    490494
    491495        // Fetch full comment objects from the primed cache.
  • trunk/src/wp-includes/class-wp-query.php

    r55703 r55749  
    28142814                wp_cache_add( $cache_key, $comment_ids, 'comment-queries' );
    28152815            }
    2816             _prime_comment_caches( $comment_ids, false );
     2816            _prime_comment_caches( $comment_ids );
    28172817
    28182818            // Convert to WP_Comment.
     
    33733373                wp_cache_add( $comment_cache_key, $comment_ids, 'comment-queries' );
    33743374            }
    3375             _prime_comment_caches( $comment_ids, false );
     3375            _prime_comment_caches( $comment_ids );
    33763376
    33773377            // Convert to WP_Comment.
     
    34883488        }
    34893489
    3490         // If comments have been fetched as part of the query, make sure comment meta lazy-loading is set up.
    3491         if ( ! empty( $this->comments ) ) {
    3492             wp_queue_comments_for_comment_meta_lazyload( $this->comments );
    3493         }
    3494 
    34953490        if ( ! $q['suppress_filters'] ) {
    34963491            /**
  • trunk/src/wp-includes/comment-template.php

    r55660 r55749  
    14411441
    14421442    $comment_args = array(
    1443         'orderby'                   => 'comment_date_gmt',
    1444         'order'                     => 'ASC',
    1445         'status'                    => 'approve',
    1446         'post_id'                   => $post->ID,
    1447         'no_found_rows'             => false,
    1448         'update_comment_meta_cache' => false, // We lazy-load comment meta for performance.
     1443        'orderby'       => 'comment_date_gmt',
     1444        'order'         => 'ASC',
     1445        'status'        => 'approve',
     1446        'post_id'       => $post->ID,
     1447        'no_found_rows' => false,
    14491448    );
    14501449
     
    23802379        $parsed_args['reverse_top_level'] = ( 'desc' === get_option( 'comment_order' ) );
    23812380    }
    2382 
    2383     wp_queue_comments_for_comment_meta_lazyload( $_comments );
    23842381
    23852382    if ( empty( $parsed_args['walker'] ) ) {
  • trunk/src/wp-includes/comment.php

    r55732 r55749  
    486486
    487487/**
     488 * Queue comment meta for lazy-loading.
     489 *
     490 * @since 6.3.0
     491 *
     492 * @param array $comment_ids List of comment IDs.
     493 */
     494function wp_lazyload_comment_meta( array $comment_ids ) {
     495    if ( empty( $comment_ids ) ) {
     496        return;
     497    }
     498    $lazyloader = wp_metadata_lazyloader();
     499    $lazyloader->queue_objects( 'comment', $comment_ids );
     500}
     501
     502/**
    488503 * Updates comment meta field based on comment ID.
    489504 *
     
    515530 *
    516531 * @since 4.5.0
     532 * @since 6.3.0 Use wp_lazyload_comment_meta() for lazy-loading of comment meta.
     533 *
     534 * @see wp_lazyload_comment_meta()
    517535 *
    518536 * @param WP_Comment[] $comments Array of comment objects.
     
    529547    }
    530548
    531     if ( $comment_ids ) {
    532         $lazyloader = wp_metadata_lazyloader();
    533         $lazyloader->queue_objects( 'comment', $comment_ids );
    534     }
     549    wp_lazyload_comment_meta( $comment_ids );
    535550}
    536551
     
    33323347 * @since 4.4.0
    33333348 * @since 6.1.0 This function is no longer marked as "private".
     3349 * @since 6.3.0 Use wp_lazyload_comment_meta() for lazy-loading of comment meta.
    33343350 *
    33353351 * @see update_comment_cache()
     
    33463362        $fresh_comments = $wpdb->get_results( sprintf( "SELECT $wpdb->comments.* FROM $wpdb->comments WHERE comment_ID IN (%s)", implode( ',', array_map( 'intval', $non_cached_ids ) ) ) );
    33473363
    3348         update_comment_cache( $fresh_comments, $update_meta_cache );
     3364        update_comment_cache( $fresh_comments, false );
     3365    }
     3366
     3367    if ( $update_meta_cache ) {
     3368        wp_lazyload_comment_meta( $comment_ids );
    33493369    }
    33503370}
  • trunk/tests/phpunit/tests/comment/metaCache.php

    r55745 r55749  
    1212     * @covers ::update_comment_meta
    1313     */
    14     public function test_update_comment_meta_cache_should_default_to_true() {
     14    public function test_update_comment_meta_cache_should_default_to_lazy_loading() {
    1515        $p           = self::factory()->post->create( array( 'post_status' => 'publish' ) );
    1616        $comment_ids = self::factory()->comment->create_post_comments( $p, 3 );
     
    2323        clean_comment_cache( $comment_ids );
    2424
    25         $q = new WP_Comment_Query(
     25        $num_queries = get_num_queries();
     26        $q           = new WP_Comment_Query(
    2627            array(
    2728                'post_ID' => $p,
     
    2930        );
    3031
     32        $this->assertSame( 2, get_num_queries() - $num_queries, 'Querying comments is expected to make two queries' );
     33
    3134        $num_queries = get_num_queries();
    3235        foreach ( $comment_ids as $cid ) {
     
    3437        }
    3538
    36         $this->assertSame( $num_queries, get_num_queries() );
    37     }
    38 
    39     /**
    40      * @ticket 16894
    41      *
    42      * @covers ::update_comment_meta
    43      */
    44     public function test_update_comment_meta_cache_true() {
     39        $this->assertSame( 1, get_num_queries() - $num_queries, 'Querying comments is expected to make two queries' );
     40    }
     41
     42    /**
     43     * @ticket 57801
     44     *
     45     * @covers ::wp_lazyload_comment_meta
     46     */
     47    public function test_update_comment_meta_cache_should_default_to_lazy_loading_fields_id() {
    4548        $p           = self::factory()->post->create( array( 'post_status' => 'publish' ) );
    4649        $comment_ids = self::factory()->comment->create_post_comments( $p, 3 );
     
    5356        clean_comment_cache( $comment_ids );
    5457
    55         $q = new WP_Comment_Query(
     58        $num_queries = get_num_queries();
     59        $q           = new WP_Comment_Query(
     60            array(
     61                'post_ID' => $p,
     62                'fields'  => 'ids',
     63            )
     64        );
     65
     66        $this->assertSame( 1, get_num_queries() - $num_queries, 'Querying comments is expected to make two queries' );
     67
     68        $num_queries = get_num_queries();
     69        foreach ( $comment_ids as $cid ) {
     70            get_comment_meta( $cid, 'foo', 'bar' );
     71        }
     72
     73        $this->assertSame( 1, get_num_queries() - $num_queries, 'Comment meta is expected to be lazy loaded' );
     74    }
     75
     76    /**
     77     * @ticket 16894
     78     *
     79     * @covers ::update_comment_meta
     80     */
     81    public function test_update_comment_meta_cache_true() {
     82        $p           = self::factory()->post->create( array( 'post_status' => 'publish' ) );
     83        $comment_ids = self::factory()->comment->create_post_comments( $p, 3 );
     84
     85        foreach ( $comment_ids as $cid ) {
     86            update_comment_meta( $cid, 'foo', 'bar' );
     87        }
     88
     89        // Clear comment cache, just in case.
     90        clean_comment_cache( $comment_ids );
     91
     92        $num_queries = get_num_queries();
     93        $q           = new WP_Comment_Query(
    5694            array(
    5795                'post_ID'                   => $p,
     
    5997            )
    6098        );
     99        $this->assertSame( 2, get_num_queries() - $num_queries, 'Comments should be queries and primed in two database queries' );
    61100
    62101        $num_queries = get_num_queries();
     
    65104        }
    66105
    67         $this->assertSame( $num_queries, get_num_queries() );
     106        $this->assertSame( 1, get_num_queries() - $num_queries, 'Comment meta should be loaded in one database query' );
     107    }
     108
     109    /**
     110     * @ticket 57801
     111     *
     112     * @covers ::update_comment_meta
     113     */
     114    public function test_update_comment_meta_cache_true_multiple() {
     115        $posts           = self::factory()->post->create_many( 3 );
     116        $all_comment_ids = array();
     117        foreach ( $posts as $p ) {
     118            $comment_ids = self::factory()->comment->create_post_comments( $p, 3 );
     119
     120            foreach ( $comment_ids as $cid ) {
     121                update_comment_meta( $cid, 'foo', 'bar' );
     122                $all_comment_ids[] = $cid;
     123            }
     124
     125            $num_queries = get_num_queries();
     126            $q           = new WP_Comment_Query(
     127                array(
     128                    'post_ID'                   => $p,
     129                    'update_comment_meta_cache' => true,
     130                )
     131            );
     132            $this->assertSame( 1, get_num_queries() - $num_queries, 'Comment query should only add one query' );
     133        }
     134
     135        $filter = new MockAction();
     136        add_filter( 'update_comment_metadata_cache', array( $filter, 'filter' ), 10, 2 );
     137        $num_queries = get_num_queries();
     138        get_comment_meta( $comment_ids[0], 'foo', 'bar' );
     139
     140        $this->assertSame( 1, get_num_queries() - $num_queries, 'Comment meta should be loaded in one database query' );
     141        $args              = $filter->get_args();
     142        $first             = reset( $args );
     143        $prime_comment_ids = end( $first );
     144        $this->assertSameSets( $prime_comment_ids, $all_comment_ids, 'All comment meta should be loaded all at once' );
    68145    }
    69146
     
    93170        }
    94171
    95         $this->assertSame( $num_queries + 3, get_num_queries() );
     172        $this->assertSame( 3, get_num_queries() - $num_queries );
    96173    }
    97174
     
    121198                $num_queries = get_num_queries();
    122199                get_comment_meta( $comment_ids[0], 'sauce' );
    123                 $this->assertSame( $num_queries + 1, get_num_queries() );
     200                $this->assertSame( 1, get_num_queries() - $num_queries );
    124201
    125202                // Second and third requests should be in cache.
    126203                get_comment_meta( $comment_ids[1], 'sauce' );
    127204                get_comment_meta( $comment_ids[2], 'sauce' );
    128                 $this->assertSame( $num_queries + 1, get_num_queries() );
     205                $this->assertSame( 1, get_num_queries() - $num_queries );
    129206            }
    130207        }
     
    135212     *
    136213     * @covers ::get_comment_meta
     214     * @covers ::wp_lazyload_comment_meta
    137215     */
    138216    public function test_comment_meta_should_be_lazy_loaded_in_comment_feed_queries() {
     
    183261     *
    184262     * @covers ::get_comment_meta
     263     * @covers ::wp_lazyload_comment_meta
    185264     */
    186265    public function test_comment_meta_should_be_lazy_loaded_in_single_post_comment_feed_queries() {
Note: See TracChangeset for help on using the changeset viewer.