mirror of
https://github.com/gosticks/wordpress-develop.git
synced 2026-06-28 14:20:15 +00:00
More performance improvements to metadata lazyloading.
Comment and term meta lazyloading for `WP_Query` loops, introduced in 4.4, depended on filter callback methods belonging to `WP_Query` objects. This meant storing `WP_Query` objects in the `$wp_filter` global (via `add_filter()`), requiring that PHP retain the objects in memory, even when the local variables would typically be expunged during normal garbage collection. In cases where a large number of `WP_Query` objects were instantiated on a single pageload, and/or where the contents of the `WP_Query` objects were quite large, serious performance issues could result. We skirt this problem by moving metadata lazyloading out of `WP_Query`. The new `WP_Metadata_Lazyloader` class acts as a lazyload queue. Query instances register items whose metadata should be lazyloaded - such as post terms, or comments - and a `WP_Metadata_Lazyloader` method will intercept comment and term meta requests to perform the cache priming. Since `WP_Metadata_Lazyloader` instances are far smaller than `WP_Query` (containing only object IDs), and clean up after themselves far better than the previous `WP_Query` methods (bp only running their callbacks a single time for a given set of queued objects), the resource use is decreased dramatically. See [36525] for an earlier step in this direction. Props lpawlik, stevegrunwell, boonebgorges. Fixes #35816. git-svn-id: https://develop.svn.wordpress.org/trunk@36566 602fd350-edb4-49c9-b593-d223f7449a82
This commit is contained in:
@@ -3605,11 +3605,6 @@ class WP_Query {
|
||||
if ( $this->posts )
|
||||
$this->posts = array_map( 'get_post', $this->posts );
|
||||
|
||||
|
||||
if ( $q['update_post_term_cache'] ) {
|
||||
add_filter( 'get_term_metadata', array( $this, 'lazyload_term_meta' ), 10, 2 );
|
||||
}
|
||||
|
||||
if ( ! $q['suppress_filters'] ) {
|
||||
/**
|
||||
* Filter the raw post results array, prior to status checks.
|
||||
@@ -3738,7 +3733,7 @@ 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 ) ) {
|
||||
add_filter( 'get_comment_metadata', array( $this, 'lazyload_comment_meta' ), 10, 2 );
|
||||
wp_queue_comments_for_comment_meta_lazyload( $this->comments );
|
||||
}
|
||||
|
||||
if ( ! $q['suppress_filters'] ) {
|
||||
@@ -3770,6 +3765,10 @@ class WP_Query {
|
||||
$this->posts = array();
|
||||
}
|
||||
|
||||
if ( $q['update_post_term_cache'] ) {
|
||||
wp_queue_posts_for_term_meta_lazyload( $this->posts );
|
||||
}
|
||||
|
||||
return $this->posts;
|
||||
}
|
||||
|
||||
@@ -4834,106 +4833,32 @@ class WP_Query {
|
||||
}
|
||||
|
||||
/**
|
||||
* Lazy-loads termmeta for located posts.
|
||||
*
|
||||
* As a rule, term queries (`get_terms()` and `wp_get_object_terms()`) prime the metadata cache for matched
|
||||
* terms by default. However, this can cause a slight performance penalty, especially when that metadata is
|
||||
* not actually used. In the context of a `WP_Query` instance, we're able to avoid this potential penalty.
|
||||
* `update_object_term_cache()`, called from `update_post_caches()`, does not 'update_term_meta_cache'.
|
||||
* Instead, the first time `get_term_meta()` is called from within a `WP_Query` loop, the current method
|
||||
* detects the fact, and then primes the metadata cache for all terms attached to all posts in the loop,
|
||||
* with a single database query.
|
||||
*
|
||||
* This method is public so that it can be used as a filter callback. As a rule, there is no need to invoke it
|
||||
* directly, from either inside or outside the `WP_Query` object.
|
||||
* Lazyload term meta for posts in the loop.
|
||||
*
|
||||
* @since 4.4.0
|
||||
* @access public
|
||||
* @deprecated 4.5.0 See wp_queue_posts_for_term_meta_lazyload().
|
||||
*
|
||||
* @param mixed $check The `$check` param passed from the 'get_term_metadata' hook.
|
||||
* @param int $term_id ID of the term whose metadata is being cached.
|
||||
* @return mixed In order not to short-circuit `get_metadata()`. Generally, this is `null`, but it could be
|
||||
* another value if filtered by a plugin.
|
||||
* @param mixed $check
|
||||
* @param int $term_id
|
||||
* @return mixed
|
||||
*/
|
||||
public function lazyload_term_meta( $check, $term_id ) {
|
||||
// We can only lazyload if the entire post object is present.
|
||||
$posts = array();
|
||||
foreach ( $this->posts as $post ) {
|
||||
if ( $post instanceof WP_Post ) {
|
||||
$posts[] = $post;
|
||||
}
|
||||
}
|
||||
|
||||
if ( ! empty( $posts ) ) {
|
||||
// Fetch cached term_ids for each post. Keyed by term_id for faster lookup.
|
||||
$term_ids = array();
|
||||
foreach ( $posts as $post ) {
|
||||
$taxonomies = get_object_taxonomies( $post->post_type );
|
||||
foreach ( $taxonomies as $taxonomy ) {
|
||||
// Term cache should already be primed by 'update_post_term_cache'.
|
||||
$terms = get_object_term_cache( $post->ID, $taxonomy );
|
||||
if ( false !== $terms ) {
|
||||
foreach ( $terms as $term ) {
|
||||
if ( ! isset( $term_ids[ $term->term_id ] ) ) {
|
||||
$term_ids[ $term->term_id ] = 1;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/*
|
||||
* Only update the metadata cache for terms belonging to these posts if the term_id passed
|
||||
* to `get_term_meta()` matches one of those terms. This prevents a single call to
|
||||
* `get_term_meta()` from priming metadata for all `WP_Query` objects.
|
||||
*/
|
||||
if ( isset( $term_ids[ $term_id ] ) ) {
|
||||
update_termmeta_cache( array_keys( $term_ids ) );
|
||||
remove_filter( 'get_term_metadata', array( $this, 'lazyload_term_meta' ), 10, 2 );
|
||||
}
|
||||
}
|
||||
|
||||
// If no terms were found, there's no need to run this again.
|
||||
if ( empty( $term_ids ) ) {
|
||||
remove_filter( 'get_term_metadata', array( $this, 'lazyload_term_meta' ), 10, 2 );
|
||||
}
|
||||
|
||||
_deprecated_function( __METHOD__, '4.5.0' );
|
||||
return $check;
|
||||
}
|
||||
|
||||
/**
|
||||
* Lazy-load comment meta when inside of a `WP_Query` loop.
|
||||
*
|
||||
* This method is public so that it can be used as a filter callback. As a rule, there is no need to invoke it
|
||||
* directly, from either inside or outside the `WP_Query` object.
|
||||
* Lazyload comment meta for comments in the loop.
|
||||
*
|
||||
* @since 4.4.0
|
||||
* @deprecated 4.5.0 See wp_queue_comments_for_comment_meta_lazyload().
|
||||
*
|
||||
* @param mixed $check The `$check` param passed from the 'get_comment_metadata' hook.
|
||||
* @param int $comment_id ID of the comment whose metadata is being cached.
|
||||
* @return mixed The original value of `$check`, to not affect 'get_comment_metadata'.
|
||||
* @param mixed $check
|
||||
* @param int $comment_id
|
||||
* @return mixed
|
||||
*/
|
||||
public function lazyload_comment_meta( $check, $comment_id ) {
|
||||
// Don't use `wp_list_pluck()` to avoid by-reference manipulation.
|
||||
$comment_ids = array();
|
||||
if ( is_array( $this->comments ) ) {
|
||||
foreach ( $this->comments as $comment ) {
|
||||
$comment_ids[] = $comment->comment_ID;
|
||||
}
|
||||
}
|
||||
|
||||
/*
|
||||
* Only update the metadata cache for comments belonging to these posts if the comment_id passed
|
||||
* to `get_comment_meta()` matches one of those comments. This prevents a single call to
|
||||
* `get_comment_meta()` from priming metadata for all `WP_Query` objects.
|
||||
*/
|
||||
if ( in_array( $comment_id, $comment_ids ) ) {
|
||||
update_meta_cache( 'comment', $comment_ids );
|
||||
remove_filter( 'get_comment_metadata', array( $this, 'lazyload_comment_meta' ), 10, 2 );
|
||||
} elseif ( empty( $comment_ids ) ) {
|
||||
remove_filter( 'get_comment_metadata', array( $this, 'lazyload_comment_meta' ), 10, 2 );
|
||||
}
|
||||
|
||||
_deprecated_function( __METHOD__, '4.5.0' );
|
||||
return $check;
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user