Make WordPress Core


Ignore:
Timestamp:
05/11/2023 12:25:51 PM (17 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.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • 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}
Note: See TracChangeset for help on using the changeset viewer.