From fda9000c7bafb65b2b9a04fc22113c0073e9c229 Mon Sep 17 00:00:00 2001 From: spacedmonkey Date: Thu, 11 May 2023 12:27:22 +0000 Subject: [PATCH] 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. Built from https://develop.svn.wordpress.org/trunk@55749 git-svn-id: http://core.svn.wordpress.org/trunk@55261 1a063a9b-81f0-0310-95a4-ce76da25c4cd --- .../includes/class-wp-comments-list-table.php | 2 -- wp-includes/class-wp-comment-query.php | 6 +++- wp-includes/class-wp-query.php | 9 ++---- wp-includes/comment-template.php | 13 ++++---- wp-includes/comment.php | 30 +++++++++++++++---- wp-includes/version.php | 2 +- 6 files changed, 38 insertions(+), 24 deletions(-) diff --git a/wp-admin/includes/class-wp-comments-list-table.php b/wp-admin/includes/class-wp-comments-list-table.php index 8ffdddad18..176b16718a 100644 --- a/wp-admin/includes/class-wp-comments-list-table.php +++ b/wp-admin/includes/class-wp-comments-list-table.php @@ -165,8 +165,6 @@ class WP_Comments_List_Table extends WP_List_Table { $_comments = get_comments( $args ); if ( is_array( $_comments ) ) { - update_comment_cache( $_comments ); - $this->items = array_slice( $_comments, 0, $comments_per_page ); $this->extra_items = array_slice( $_comments, $comments_per_page ); diff --git a/wp-includes/class-wp-comment-query.php b/wp-includes/class-wp-comment-query.php index 920907906f..cfdf05dbfd 100644 --- a/wp-includes/class-wp-comment-query.php +++ b/wp-includes/class-wp-comment-query.php @@ -481,12 +481,16 @@ class WP_Comment_Query { $comment_ids = array_map( 'intval', $comment_ids ); + if ( $this->query_vars['update_comment_meta_cache'] ) { + wp_lazyload_comment_meta( $comment_ids ); + } + if ( 'ids' === $this->query_vars['fields'] ) { $this->comments = $comment_ids; return $this->comments; } - _prime_comment_caches( $comment_ids, $this->query_vars['update_comment_meta_cache'] ); + _prime_comment_caches( $comment_ids, false ); // Fetch full comment objects from the primed cache. $_comments = array(); diff --git a/wp-includes/class-wp-query.php b/wp-includes/class-wp-query.php index 29380e7df3..c1f43cf271 100644 --- a/wp-includes/class-wp-query.php +++ b/wp-includes/class-wp-query.php @@ -2813,7 +2813,7 @@ class WP_Query { $comment_ids = $wpdb->get_col( $comments_request ); wp_cache_add( $cache_key, $comment_ids, 'comment-queries' ); } - _prime_comment_caches( $comment_ids, false ); + _prime_comment_caches( $comment_ids ); // Convert to WP_Comment. /** @var WP_Comment[] */ @@ -3372,7 +3372,7 @@ class WP_Query { $comment_ids = $wpdb->get_col( $comments_request ); wp_cache_add( $comment_cache_key, $comment_ids, 'comment-queries' ); } - _prime_comment_caches( $comment_ids, false ); + _prime_comment_caches( $comment_ids ); // Convert to WP_Comment. /** @var WP_Comment[] */ @@ -3487,11 +3487,6 @@ class WP_Query { } } - // If comments have been fetched as part of the query, make sure comment meta lazy-loading is set up. - if ( ! empty( $this->comments ) ) { - wp_queue_comments_for_comment_meta_lazyload( $this->comments ); - } - if ( ! $q['suppress_filters'] ) { /** * Filters the array of retrieved posts after they've been fetched and diff --git a/wp-includes/comment-template.php b/wp-includes/comment-template.php index bff69f858b..514f65acbf 100644 --- a/wp-includes/comment-template.php +++ b/wp-includes/comment-template.php @@ -1440,12 +1440,11 @@ function comments_template( $file = '/comments.php', $separate_comments = false $comment_author_url = esc_url( $commenter['comment_author_url'] ); $comment_args = array( - 'orderby' => 'comment_date_gmt', - 'order' => 'ASC', - 'status' => 'approve', - 'post_id' => $post->ID, - 'no_found_rows' => false, - 'update_comment_meta_cache' => false, // We lazy-load comment meta for performance. + 'orderby' => 'comment_date_gmt', + 'order' => 'ASC', + 'status' => 'approve', + 'post_id' => $post->ID, + 'no_found_rows' => false, ); if ( get_option( 'thread_comments' ) ) { @@ -2380,8 +2379,6 @@ function wp_list_comments( $args = array(), $comments = null ) { $parsed_args['reverse_top_level'] = ( 'desc' === get_option( 'comment_order' ) ); } - wp_queue_comments_for_comment_meta_lazyload( $_comments ); - if ( empty( $parsed_args['walker'] ) ) { $walker = new Walker_Comment(); } else { diff --git a/wp-includes/comment.php b/wp-includes/comment.php index 45a326ced5..0d400bef4c 100644 --- a/wp-includes/comment.php +++ b/wp-includes/comment.php @@ -484,6 +484,21 @@ function get_comment_meta( $comment_id, $key = '', $single = false ) { return get_metadata( 'comment', $comment_id, $key, $single ); } +/** + * Queue comment meta for lazy-loading. + * + * @since 6.3.0 + * + * @param array $comment_ids List of comment IDs. + */ +function wp_lazyload_comment_meta( array $comment_ids ) { + if ( empty( $comment_ids ) ) { + return; + } + $lazyloader = wp_metadata_lazyloader(); + $lazyloader->queue_objects( 'comment', $comment_ids ); +} + /** * Updates comment meta field based on comment ID. * @@ -514,6 +529,9 @@ function update_comment_meta( $comment_id, $meta_key, $meta_value, $prev_value = * Queues comments for metadata lazy-loading. * * @since 4.5.0 + * @since 6.3.0 Use wp_lazyload_comment_meta() for lazy-loading of comment meta. + * + * @see wp_lazyload_comment_meta() * * @param WP_Comment[] $comments Array of comment objects. */ @@ -528,10 +546,7 @@ function wp_queue_comments_for_comment_meta_lazyload( $comments ) { } } - if ( $comment_ids ) { - $lazyloader = wp_metadata_lazyloader(); - $lazyloader->queue_objects( 'comment', $comment_ids ); - } + wp_lazyload_comment_meta( $comment_ids ); } /** @@ -3331,6 +3346,7 @@ function update_comment_cache( $comments, $update_meta_cache = true ) { * * @since 4.4.0 * @since 6.1.0 This function is no longer marked as "private". + * @since 6.3.0 Use wp_lazyload_comment_meta() for lazy-loading of comment meta. * * @see update_comment_cache() * @global wpdb $wpdb WordPress database abstraction object. @@ -3345,7 +3361,11 @@ function _prime_comment_caches( $comment_ids, $update_meta_cache = true ) { if ( ! empty( $non_cached_ids ) ) { $fresh_comments = $wpdb->get_results( sprintf( "SELECT $wpdb->comments.* FROM $wpdb->comments WHERE comment_ID IN (%s)", implode( ',', array_map( 'intval', $non_cached_ids ) ) ) ); - update_comment_cache( $fresh_comments, $update_meta_cache ); + update_comment_cache( $fresh_comments, false ); + } + + if ( $update_meta_cache ) { + wp_lazyload_comment_meta( $comment_ids ); } } diff --git a/wp-includes/version.php b/wp-includes/version.php index 1ac68d28f8..a833fda54a 100644 --- a/wp-includes/version.php +++ b/wp-includes/version.php @@ -16,7 +16,7 @@ * * @global string $wp_version */ -$wp_version = '6.3-alpha-55748'; +$wp_version = '6.3-alpha-55749'; /** * Holds the WordPress DB revision, increments when changes are made to the WordPress DB schema.