Make WordPress Core


Ignore:
Timestamp:
02/17/2016 10:57:33 PM (9 years ago)
Author:
boonebgorges
Message:

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.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/src/wp-includes/post.php

    r36498 r36566  
    59495949
    59505950/**
     5951 * Queue posts for lazyloading of term meta.
     5952 *
     5953 * @since 4.5.0
     5954 *
     5955 * @param array $posts Array of WP_Post objects.
     5956 */
     5957function wp_queue_posts_for_term_meta_lazyload( $posts ) {
     5958    $post_type_taxonomies = $term_ids = array();
     5959    foreach ( $posts as $post ) {
     5960        if ( ! ( $post instanceof WP_Post ) ) {
     5961            continue;
     5962        }
     5963
     5964        if ( ! isset( $post_type_taxonomies[ $post->post_type ] ) ) {
     5965            $post_type_taxonomies[ $post->post_type ] = get_object_taxonomies( $post->post_type );
     5966        }
     5967
     5968        foreach ( $post_type_taxonomies[ $post->post_type ] as $taxonomy ) {
     5969            // Term cache should already be primed by `update_post_term_cache()`.
     5970            $terms = get_object_term_cache( $post->ID, $taxonomy );
     5971            if ( false !== $terms ) {
     5972                foreach ( $terms as $term ) {
     5973                    if ( ! isset( $term_ids[ $term->term_id ] ) ) {
     5974                        $term_ids[] = $term->term_id;
     5975                    }
     5976                }
     5977            }
     5978        }
     5979    }
     5980
     5981    if ( $term_ids ) {
     5982        $lazyloader = wp_metadata_lazyloader();
     5983        $lazyloader->queue_objects( 'term', $term_ids );
     5984    }
     5985}
     5986
     5987/**
    59515988 * Update the custom taxonomies' term counts when a post's status is changed.
    59525989 *
Note: See TracChangeset for help on using the changeset viewer.