<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1//EN"
"http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head><meta http-equiv="content-type" content="text/html; charset=utf-8" />
<title>[36566] trunk: More performance improvements to metadata lazyloading.</title>
</head>
<body>
<style type="text/css"><!--
#msg dl.meta { border: 1px #006 solid; background: #369; padding: 6px; color: #fff; }
#msg dl.meta dt { float: left; width: 6em; font-weight: bold; }
#msg dt:after { content:':';}
#msg dl, #msg dt, #msg ul, #msg li, #header, #footer, #logmsg { font-family: verdana,arial,helvetica,sans-serif; font-size: 10pt; }
#msg dl a { font-weight: bold}
#msg dl a:link { color:#fc3; }
#msg dl a:active { color:#ff0; }
#msg dl a:visited { color:#cc6; }
h3 { font-family: verdana,arial,helvetica,sans-serif; font-size: 10pt; font-weight: bold; }
#msg pre { overflow: auto; background: #ffc; border: 1px #fa0 solid; padding: 6px; }
#logmsg { background: #ffc; border: 1px #fa0 solid; padding: 1em 1em 0 1em; }
#logmsg p, #logmsg pre, #logmsg blockquote { margin: 0 0 1em 0; }
#logmsg p, #logmsg li, #logmsg dt, #logmsg dd { line-height: 14pt; }
#logmsg h1, #logmsg h2, #logmsg h3, #logmsg h4, #logmsg h5, #logmsg h6 { margin: .5em 0; }
#logmsg h1:first-child, #logmsg h2:first-child, #logmsg h3:first-child, #logmsg h4:first-child, #logmsg h5:first-child, #logmsg h6:first-child { margin-top: 0; }
#logmsg ul, #logmsg ol { padding: 0; list-style-position: inside; margin: 0 0 0 1em; }
#logmsg ul { text-indent: -1em; padding-left: 1em; }#logmsg ol { text-indent: -1.5em; padding-left: 1.5em; }
#logmsg > ul, #logmsg > ol { margin: 0 0 1em 0; }
#logmsg pre { background: #eee; padding: 1em; }
#logmsg blockquote { border: 1px solid #fa0; border-left-width: 10px; padding: 1em 1em 0 1em; background: white;}
#logmsg dl { margin: 0; }
#logmsg dt { font-weight: bold; }
#logmsg dd { margin: 0; padding: 0 0 0.5em 0; }
#logmsg dd:before { content:'\00bb';}
#logmsg table { border-spacing: 0px; border-collapse: collapse; border-top: 4px solid #fa0; border-bottom: 1px solid #fa0; background: #fff; }
#logmsg table th { text-align: left; font-weight: normal; padding: 0.2em 0.5em; border-top: 1px dotted #fa0; }
#logmsg table td { text-align: right; border-top: 1px dotted #fa0; padding: 0.2em 0.5em; }
#logmsg table thead th { text-align: center; border-bottom: 1px solid #fa0; }
#logmsg table th.Corner { text-align: left; }
#logmsg hr { border: none 0; border-top: 2px dashed #fa0; height: 1px; }
#header, #footer { color: #fff; background: #636; border: 1px #300 solid; padding: 6px; }
#patch { width: 100%; }
#patch h4 {font-family: verdana,arial,helvetica,sans-serif;font-size:10pt;padding:8px;background:#369;color:#fff;margin:0;}
#patch .propset h4, #patch .binary h4 {margin:0;}
#patch pre {padding:0;line-height:1.2em;margin:0;}
#patch .diff {width:100%;background:#eee;padding: 0 0 10px 0;overflow:auto;}
#patch .propset .diff, #patch .binary .diff {padding:10px 0;}
#patch span {display:block;padding:0 10px;}
#patch .modfile, #patch .addfile, #patch .delfile, #patch .propset, #patch .binary, #patch .copfile {border:1px solid #ccc;margin:10px 0;}
#patch ins {background:#dfd;text-decoration:none;display:block;padding:0 10px;}
#patch del {background:#fdd;text-decoration:none;display:block;padding:0 10px;}
#patch .lines, .info {color:#888;background:#fff;}
--></style>
<div id="msg">
<dl class="meta" style="font-size: 105%">
<dt style="float: left; width: 6em; font-weight: bold">Revision</dt> <dd><a style="font-weight: bold" href="https://core.trac.wordpress.org/changeset/36566">36566</a><script type="application/ld+json">{"@context":"http://schema.org","@type":"EmailMessage","description":"Review this Commit","action":{"@type":"ViewAction","url":"https://core.trac.wordpress.org/changeset/36566","name":"Review Commit"}}</script></dd>
<dt style="float: left; width: 6em; font-weight: bold">Author</dt> <dd>boonebgorges</dd>
<dt style="float: left; width: 6em; font-weight: bold">Date</dt> <dd>2016-02-17 22:57:33 +0000 (Wed, 17 Feb 2016)</dd>
</dl>
<pre style='padding-left: 1em; margin: 2em 0; border-left: 2px solid #ccc; line-height: 1.25; font-size: 105%; font-family: sans-serif'>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 <a href="https://core.trac.wordpress.org/changeset/36525">[36525]</a> for an earlier step in this direction.
Props lpawlik, stevegrunwell, boonebgorges.
Fixes <a href="https://core.trac.wordpress.org/ticket/35816">#35816</a>.</pre>
<h3>Modified Paths</h3>
<ul>
<li><a href="#trunksrcwpincludescommenttemplatephp">trunk/src/wp-includes/comment-template.php</a></li>
<li><a href="#trunksrcwpincludescommentphp">trunk/src/wp-includes/comment.php</a></li>
<li><a href="#trunksrcwpincludesmetaphp">trunk/src/wp-includes/meta.php</a></li>
<li><a href="#trunksrcwpincludespostphp">trunk/src/wp-includes/post.php</a></li>
<li><a href="#trunksrcwpincludesqueryphp">trunk/src/wp-includes/query.php</a></li>
<li><a href="#trunksrcwpsettingsphp">trunk/src/wp-settings.php</a></li>
<li><a href="#trunktestsphpunitteststermmetaphp">trunk/tests/phpunit/tests/term/meta.php</a></li>
</ul>
<h3>Added Paths</h3>
<ul>
<li><a href="#trunksrcwpincludesclasswpmetadatalazyloaderphp">trunk/src/wp-includes/class-wp-metadata-lazyloader.php</a></li>
</ul>
</div>
<div id="patch">
<h3>Diff</h3>
<a id="trunksrcwpincludesclasswpmetadatalazyloaderphp"></a>
<div class="addfile"><h4 style="background-color: #eee; color: inherit; margin: 1em 0; padding: 1.3em; font-size: 115%">Added: trunk/src/wp-includes/class-wp-metadata-lazyloader.php</h4>
<pre class="diff"><span>
<span class="info" style="display: block; padding: 0 10px; color: #888">--- trunk/src/wp-includes/class-wp-metadata-lazyloader.php (rev 0)
+++ trunk/src/wp-includes/class-wp-metadata-lazyloader.php 2016-02-17 22:57:33 UTC (rev 36566)
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -0,0 +1,164 @@
</span><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+<?php
+
+/**
+ * Lazyloader for object metadata.
+ *
+ * When loading many objects of a given type, such as posts in a WP_Query loop, it often makes
+ * sense to prime various metadata caches at the beginning of the loop. This means fetching all
+ * relevant metadata with a single database query, a technique that has the potential to improve
+ * performance dramatically in some cases.
+ *
+ * In cases where the given metadata may not even be used in the loop, we can improve performance
+ * even more by only priming the metadata cache for affected items the first time a piece of metadata
+ * is requested - ie, by lazyloading it. So, for example, comment meta may not be loaded into the
+ * cache in the comments section of a post until the first time get_comment_meta() is called in the
+ * context of the comment loop.
+ *
+ * WP uses the WP_Metadata_Lazyloader class to queue objects for metadata cache priming. The class
+ * then detects the relevant get_*_meta() function call, and queries the metadata of all queued objects.
+ *
+ * Do not access this class directly. Use the wp_metadata_lazyloader() function.
+ *
+ * @since 4.5.0
+ */
+class WP_Metadata_Lazyloader {
+ /**
+ * Pending objects queue.
+ *
+ * @since 4.5.0
+ * @var array
+ */
+ protected $pending_objects;
+
+ /**
+ * Settings for supported object types.
+ *
+ * @since 4.5.0
+ * @var array
+ */
+ protected $settings = array();
+
+ /**
+ * Constructor.
+ *
+ * @since 4.5.0
+ */
+ public function __construct() {
+ $this->settings = array(
+ 'term' => array(
+ 'filter' => 'get_term_metadata',
+ 'callback' => array( $this, 'lazyload_term_meta' ),
+ ),
+ 'comment' => array(
+ 'filter' => 'get_comment_metadata',
+ 'callback' => array( $this, 'lazyload_comment_meta' ),
+ ),
+ );
+ }
+
+ /**
+ * Add objects to the metadata lazyload queue.
+ *
+ * @since 4.5.0
+ *
+ * @param string $object_type Type of object whose meta is to be lazyloaded. Accepts 'term' or 'comment'.
+ * @param array $object_ids Array of object IDs.
+ * @return bool|WP_Error True on success, WP_Error on failure.
+ */
+ public function queue_objects( $object_type, $object_ids ) {
+ if ( ! isset( $this->settings[ $object_type ] ) ) {
+ return new WP_Error( 'invalid_object_type', __( 'Invalid object type' ) );
+ }
+
+ $type_settings = $this->settings[ $object_type ];
+
+ if ( ! isset( $this->pending_objects[ $object_type ] ) ) {
+ $this->pending_objects[ $object_type ] = array();
+ }
+
+ foreach ( $object_ids as $object_id ) {
+ // Keyed by ID for faster lookup.
+ if ( ! isset( $this->pending_objects[ $object_type ][ $object_id ] ) ) {
+ $this->pending_objects[ $object_type ][ $object_id ] = 1;
+ }
+ }
+
+ add_filter( $type_settings['filter'], $type_settings['callback'] );
+
+ /**
+ * Fires after objects are added to the metadata lazyload queue.
+ *
+ * @since 4.5.0
+ *
+ * @param array $object_ids Object IDs.
+ * @param string $object_type Type of object being queued.
+ * @param WP_Metadata_Lazyloader $lazyloader The lazyloader object.
+ */
+ do_action( 'metadata_lazyloader_queued_objects', $object_ids, $object_type, $this );
+ }
+
+ /**
+ * Reset lazyload queue for a given object type.
+ *
+ * @since 4.5.0
+ *
+ * @param string $object_type Object type. Accepts 'comment' or 'term'.
+ * @return bool|WP_Error True on success, WP_Error on failure.
+ */
+ public function reset_queue( $object_type ) {
+ if ( ! isset( $this->settings[ $object_type ] ) ) {
+ return new WP_Error( 'invalid_object_type', __( 'Invalid object type' ) );
+ }
+
+ $type_settings = $this->settings[ $object_type ];
+
+ $this->pending_objects[ $object_type ] = array();
+ remove_filter( $type_settings['filter'], $type_settings['callback'] );
+ }
+
+ /**
+ * Lazyloads term meta for queued terms.
+ *
+ * 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.
+ *
+ * @since 4.5.0
+ * @access public
+ *
+ * @param mixed $check The `$check` param passed from the 'get_term_metadata' hook.
+ * @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.
+ */
+ public function lazyload_term_meta( $check ) {
+ if ( ! empty( $this->pending_objects['term'] ) ) {
+ update_termmeta_cache( array_keys( $this->pending_objects['term'] ) );
+
+ // No need to run again for this set of terms.
+ $this->reset_queue( 'term' );
+ }
+
+ return $check;
+ }
+
+ /**
+ * Lazyload comment meta for queued comments.
+ *
+ * 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.
+ *
+ * @since 4.5.0
+ *
+ * @param mixed $check The `$check` param passed from the 'get_comment_metadata' hook.
+ * @return mixed The original value of `$check`, so as not to short-circuit `get_comment_metadata()`.
+ */
+ public function lazyload_comment_meta( $check ) {
+ if ( ! empty( $this->pending_objects['comment'] ) ) {
+ update_meta_cache( 'comment', array_keys( $this->pending_objects['comment'] ) );
+
+ // No need to run again for this set of comments.
+ $this->reset_queue( 'comment' );
+ }
+
+ return $check;
+ }
+}
</ins></span></pre></div>
<a id="trunksrcwpincludescommenttemplatephp"></a>
<div class="modfile"><h4 style="background-color: #eee; color: inherit; margin: 1em 0; padding: 1.3em; font-size: 115%">Modified: trunk/src/wp-includes/comment-template.php</h4>
<pre class="diff"><span>
<span class="info" style="display: block; padding: 0 10px; color: #888">--- trunk/src/wp-includes/comment-template.php 2016-02-17 22:51:01 UTC (rev 36565)
+++ trunk/src/wp-includes/comment-template.php 2016-02-17 22:57:33 UTC (rev 36566)
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -1393,9 +1393,6 @@
</span><span class="cx" style="display: block; padding: 0 10px"> */
</span><span class="cx" style="display: block; padding: 0 10px"> $wp_query->comments = apply_filters( 'comments_array', $comments_flat, $post->ID );
</span><span class="cx" style="display: block; padding: 0 10px">
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">- // Set up lazy-loading for comment metadata.
- add_action( 'get_comment_metadata', array( $wp_query, 'lazyload_comment_meta' ), 10, 2 );
-
</del><span class="cx" style="display: block; padding: 0 10px"> $comments = &$wp_query->comments;
</span><span class="cx" style="display: block; padding: 0 10px"> $wp_query->comment_count = count($wp_query->comments);
</span><span class="cx" style="display: block; padding: 0 10px"> $wp_query->max_num_comment_pages = $comment_query->max_num_pages;
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -2030,6 +2027,8 @@
</span><span class="cx" style="display: block; padding: 0 10px"> if ( null === $r['reverse_top_level'] )
</span><span class="cx" style="display: block; padding: 0 10px"> $r['reverse_top_level'] = ( 'desc' == get_option('comment_order') );
</span><span class="cx" style="display: block; padding: 0 10px">
</span><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+ wp_queue_comments_for_comment_meta_lazyload( $_comments );
+
</ins><span class="cx" style="display: block; padding: 0 10px"> if ( empty( $r['walker'] ) ) {
</span><span class="cx" style="display: block; padding: 0 10px"> $walker = new Walker_Comment;
</span><span class="cx" style="display: block; padding: 0 10px"> } else {
</span></span></pre></div>
<a id="trunksrcwpincludescommentphp"></a>
<div class="modfile"><h4 style="background-color: #eee; color: inherit; margin: 1em 0; padding: 1.3em; font-size: 115%">Modified: trunk/src/wp-includes/comment.php</h4>
<pre class="diff"><span>
<span class="info" style="display: block; padding: 0 10px; color: #888">--- trunk/src/wp-includes/comment.php 2016-02-17 22:51:01 UTC (rev 36565)
+++ trunk/src/wp-includes/comment.php 2016-02-17 22:57:33 UTC (rev 36566)
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -469,6 +469,30 @@
</span><span class="cx" style="display: block; padding: 0 10px"> }
</span><span class="cx" style="display: block; padding: 0 10px">
</span><span class="cx" style="display: block; padding: 0 10px"> /**
</span><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+ * Queue comments for metadata lazyloading.
+ *
+ * @since 4.5.0
+ *
+ * @param array $comments Array of comment objects.
+ */
+function wp_queue_comments_for_comment_meta_lazyload( $comments ) {
+ // Don't use `wp_list_pluck()` to avoid by-reference manipulation.
+ $comment_ids = array();
+ if ( is_array( $comments ) ) {
+ foreach ( $comments as $comment ) {
+ if ( $comment instanceof WP_Comment ) {
+ $comment_ids[] = $comment->comment_ID;
+ }
+ }
+ }
+
+ if ( $comment_ids ) {
+ $lazyloader = wp_metadata_lazyloader();
+ $lazyloader->queue_objects( 'comment', $comment_ids );
+ }
+}
+
+/**
</ins><span class="cx" style="display: block; padding: 0 10px"> * Sets the cookies used to store an unauthenticated commentator's identity. Typically used
</span><span class="cx" style="display: block; padding: 0 10px"> * to recall previous comments by this commentator that are still held in moderation.
</span><span class="cx" style="display: block; padding: 0 10px"> *
</span></span></pre></div>
<a id="trunksrcwpincludesmetaphp"></a>
<div class="modfile"><h4 style="background-color: #eee; color: inherit; margin: 1em 0; padding: 1.3em; font-size: 115%">Modified: trunk/src/wp-includes/meta.php</h4>
<pre class="diff"><span>
<span class="info" style="display: block; padding: 0 10px; color: #888">--- trunk/src/wp-includes/meta.php 2016-02-17 22:51:01 UTC (rev 36565)
+++ trunk/src/wp-includes/meta.php 2016-02-17 22:57:33 UTC (rev 36566)
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -852,6 +852,23 @@
</span><span class="cx" style="display: block; padding: 0 10px"> }
</span><span class="cx" style="display: block; padding: 0 10px">
</span><span class="cx" style="display: block; padding: 0 10px"> /**
</span><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+ * Get the metadata lazyloading queue.
+ *
+ * @since 4.5.0
+ *
+ * @return WP_Metadata_Lazyloader $lazyloader Metadata lazyloader queue.
+ */
+function wp_metadata_lazyloader() {
+ static $wp_metadata_lazyloader;
+
+ if ( null === $wp_metadata_lazyloader ) {
+ $wp_metadata_lazyloader = new WP_Metadata_Lazyloader();
+ }
+
+ return $wp_metadata_lazyloader;
+}
+
+/**
</ins><span class="cx" style="display: block; padding: 0 10px"> * Given a meta query, generates SQL clauses to be appended to a main query.
</span><span class="cx" style="display: block; padding: 0 10px"> *
</span><span class="cx" style="display: block; padding: 0 10px"> * @since 3.2.0
</span></span></pre></div>
<a id="trunksrcwpincludespostphp"></a>
<div class="modfile"><h4 style="background-color: #eee; color: inherit; margin: 1em 0; padding: 1.3em; font-size: 115%">Modified: trunk/src/wp-includes/post.php</h4>
<pre class="diff"><span>
<span class="info" style="display: block; padding: 0 10px; color: #888">--- trunk/src/wp-includes/post.php 2016-02-17 22:51:01 UTC (rev 36565)
+++ trunk/src/wp-includes/post.php 2016-02-17 22:57:33 UTC (rev 36566)
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -5948,6 +5948,43 @@
</span><span class="cx" style="display: block; padding: 0 10px"> }
</span><span class="cx" style="display: block; padding: 0 10px">
</span><span class="cx" style="display: block; padding: 0 10px"> /**
</span><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+ * Queue posts for lazyloading of term meta.
+ *
+ * @since 4.5.0
+ *
+ * @param array $posts Array of WP_Post objects.
+ */
+function wp_queue_posts_for_term_meta_lazyload( $posts ) {
+ $post_type_taxonomies = $term_ids = array();
+ foreach ( $posts as $post ) {
+ if ( ! ( $post instanceof WP_Post ) ) {
+ continue;
+ }
+
+ if ( ! isset( $post_type_taxonomies[ $post->post_type ] ) ) {
+ $post_type_taxonomies[ $post->post_type ] = get_object_taxonomies( $post->post_type );
+ }
+
+ foreach ( $post_type_taxonomies[ $post->post_type ] 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;
+ }
+ }
+ }
+ }
+ }
+
+ if ( $term_ids ) {
+ $lazyloader = wp_metadata_lazyloader();
+ $lazyloader->queue_objects( 'term', $term_ids );
+ }
+}
+
+/**
</ins><span class="cx" style="display: block; padding: 0 10px"> * Update the custom taxonomies' term counts when a post's status is changed.
</span><span class="cx" style="display: block; padding: 0 10px"> *
</span><span class="cx" style="display: block; padding: 0 10px"> * For example, default posts term counts (for custom taxonomies) don't include
</span></span></pre></div>
<a id="trunksrcwpincludesqueryphp"></a>
<div class="modfile"><h4 style="background-color: #eee; color: inherit; margin: 1em 0; padding: 1.3em; font-size: 115%">Modified: trunk/src/wp-includes/query.php</h4>
<pre class="diff"><span>
<span class="info" style="display: block; padding: 0 10px; color: #888">--- trunk/src/wp-includes/query.php 2016-02-17 22:51:01 UTC (rev 36565)
+++ trunk/src/wp-includes/query.php 2016-02-17 22:57:33 UTC (rev 36566)
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -3605,11 +3605,6 @@
</span><span class="cx" style="display: block; padding: 0 10px"> if ( $this->posts )
</span><span class="cx" style="display: block; padding: 0 10px"> $this->posts = array_map( 'get_post', $this->posts );
</span><span class="cx" style="display: block; padding: 0 10px">
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">-
- if ( $q['update_post_term_cache'] ) {
- add_filter( 'get_term_metadata', array( $this, 'lazyload_term_meta' ), 10, 2 );
- }
-
</del><span class="cx" style="display: block; padding: 0 10px"> if ( ! $q['suppress_filters'] ) {
</span><span class="cx" style="display: block; padding: 0 10px"> /**
</span><span class="cx" style="display: block; padding: 0 10px"> * Filter the raw post results array, prior to status checks.
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -3738,7 +3733,7 @@
</span><span class="cx" style="display: block; padding: 0 10px">
</span><span class="cx" style="display: block; padding: 0 10px"> // If comments have been fetched as part of the query, make sure comment meta lazy-loading is set up.
</span><span class="cx" style="display: block; padding: 0 10px"> if ( ! empty( $this->comments ) ) {
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">- add_filter( 'get_comment_metadata', array( $this, 'lazyload_comment_meta' ), 10, 2 );
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+ wp_queue_comments_for_comment_meta_lazyload( $this->comments );
</ins><span class="cx" style="display: block; padding: 0 10px"> }
</span><span class="cx" style="display: block; padding: 0 10px">
</span><span class="cx" style="display: block; padding: 0 10px"> if ( ! $q['suppress_filters'] ) {
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -3770,6 +3765,10 @@
</span><span class="cx" style="display: block; padding: 0 10px"> $this->posts = array();
</span><span class="cx" style="display: block; padding: 0 10px"> }
</span><span class="cx" style="display: block; padding: 0 10px">
</span><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+ if ( $q['update_post_term_cache'] ) {
+ wp_queue_posts_for_term_meta_lazyload( $this->posts );
+ }
+
</ins><span class="cx" style="display: block; padding: 0 10px"> return $this->posts;
</span><span class="cx" style="display: block; padding: 0 10px"> }
</span><span class="cx" style="display: block; padding: 0 10px">
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -4834,106 +4833,32 @@
</span><span class="cx" style="display: block; padding: 0 10px"> }
</span><span class="cx" style="display: block; padding: 0 10px">
</span><span class="cx" style="display: block; padding: 0 10px"> /**
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">- * Lazy-loads termmeta for located posts.
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+ * Lazyload term meta for posts in the loop.
</ins><span class="cx" style="display: block; padding: 0 10px"> *
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">- * 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.
- *
</del><span class="cx" style="display: block; padding: 0 10px"> * @since 4.4.0
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">- * @access public
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+ * @deprecated 4.5.0 See wp_queue_posts_for_term_meta_lazyload().
</ins><span class="cx" style="display: block; padding: 0 10px"> *
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">- * @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.
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+ * @param mixed $check
+ * @param int $term_id
+ * @return mixed
</ins><span class="cx" style="display: block; padding: 0 10px"> */
</span><span class="cx" style="display: block; padding: 0 10px"> public function lazyload_term_meta( $check, $term_id ) {
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">- // 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 );
- }
-
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+ _deprecated_function( __METHOD__, '4.5.0' );
</ins><span class="cx" style="display: block; padding: 0 10px"> return $check;
</span><span class="cx" style="display: block; padding: 0 10px"> }
</span><span class="cx" style="display: block; padding: 0 10px">
</span><span class="cx" style="display: block; padding: 0 10px"> /**
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">- * Lazy-load comment meta when inside of a `WP_Query` loop.
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+ * Lazyload comment meta for comments in the loop.
</ins><span class="cx" style="display: block; padding: 0 10px"> *
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">- * 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.
- *
</del><span class="cx" style="display: block; padding: 0 10px"> * @since 4.4.0
</span><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+ * @deprecated 4.5.0 See wp_queue_comments_for_comment_meta_lazyload().
</ins><span class="cx" style="display: block; padding: 0 10px"> *
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">- * @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'.
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+ * @param mixed $check
+ * @param int $comment_id
+ * @return mixed
</ins><span class="cx" style="display: block; padding: 0 10px"> */
</span><span class="cx" style="display: block; padding: 0 10px"> public function lazyload_comment_meta( $check, $comment_id ) {
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">- // 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 );
- }
-
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+ _deprecated_function( __METHOD__, '4.5.0' );
</ins><span class="cx" style="display: block; padding: 0 10px"> return $check;
</span><span class="cx" style="display: block; padding: 0 10px"> }
</span><span class="cx" style="display: block; padding: 0 10px"> }
</span></span></pre></div>
<a id="trunksrcwpsettingsphp"></a>
<div class="modfile"><h4 style="background-color: #eee; color: inherit; margin: 1em 0; padding: 1.3em; font-size: 115%">Modified: trunk/src/wp-settings.php</h4>
<pre class="diff"><span>
<span class="info" style="display: block; padding: 0 10px; color: #888">--- trunk/src/wp-settings.php 2016-02-17 22:51:01 UTC (rev 36565)
+++ trunk/src/wp-settings.php 2016-02-17 22:57:33 UTC (rev 36566)
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -135,6 +135,7 @@
</span><span class="cx" style="display: block; padding: 0 10px"> require( ABSPATH . WPINC . '/session.php' );
</span><span class="cx" style="display: block; padding: 0 10px"> require( ABSPATH . WPINC . '/meta.php' );
</span><span class="cx" style="display: block; padding: 0 10px"> require( ABSPATH . WPINC . '/class-wp-meta-query.php' );
</span><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+require( ABSPATH . WPINC . '/class-wp-metadata-lazyloader.php' );
</ins><span class="cx" style="display: block; padding: 0 10px"> require( ABSPATH . WPINC . '/general-template.php' );
</span><span class="cx" style="display: block; padding: 0 10px"> require( ABSPATH . WPINC . '/link-template.php' );
</span><span class="cx" style="display: block; padding: 0 10px"> require( ABSPATH . WPINC . '/author-template.php' );
</span></span></pre></div>
<a id="trunktestsphpunitteststermmetaphp"></a>
<div class="modfile"><h4 style="background-color: #eee; color: inherit; margin: 1em 0; padding: 1.3em; font-size: 115%">Modified: trunk/tests/phpunit/tests/term/meta.php</h4>
<pre class="diff"><span>
<span class="info" style="display: block; padding: 0 10px; color: #888">--- trunk/tests/phpunit/tests/term/meta.php 2016-02-17 22:51:01 UTC (rev 36565)
+++ trunk/tests/phpunit/tests/term/meta.php 2016-02-17 22:57:33 UTC (rev 36566)
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -134,107 +134,22 @@
</span><span class="cx" style="display: block; padding: 0 10px"> // First request will hit the database.
</span><span class="cx" style="display: block; padding: 0 10px"> $num_queries = $wpdb->num_queries;
</span><span class="cx" style="display: block; padding: 0 10px"> $this->assertSame( 'bar', get_term_meta( $terms[0], 'foo', true ) );
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">- $this->assertSame( $num_queries + 1, $wpdb->num_queries );
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+ $num_queries++;
+ $this->assertSame( $num_queries, $wpdb->num_queries );
</ins><span class="cx" style="display: block; padding: 0 10px">
</span><span class="cx" style="display: block; padding: 0 10px"> // Second and third requests should be in cache.
</span><span class="cx" style="display: block; padding: 0 10px"> $this->assertSame( 'bar', get_term_meta( $terms[1], 'foo', true ) );
</span><span class="cx" style="display: block; padding: 0 10px"> $this->assertSame( 'bar', get_term_meta( $terms[2], 'foo', true ) );
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">- $this->assertSame( $num_queries + 1, $wpdb->num_queries );
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+ $this->assertSame( $num_queries, $wpdb->num_queries );
</ins><span class="cx" style="display: block; padding: 0 10px">
</span><span class="cx" style="display: block; padding: 0 10px"> // Querying a term not primed should result in a hit.
</span><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+ $num_queries++;
</ins><span class="cx" style="display: block; padding: 0 10px"> $this->assertSame( 'bar', get_term_meta( $orphan_term, 'foo', true ) );
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">- $this->assertSame( $num_queries + 2, $wpdb->num_queries );
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+ $this->assertSame( $num_queries, $wpdb->num_queries );
</ins><span class="cx" style="display: block; padding: 0 10px"> }
</span><span class="cx" style="display: block; padding: 0 10px"> }
</span><span class="cx" style="display: block; padding: 0 10px"> }
</span><span class="cx" style="display: block; padding: 0 10px">
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">- /**
- * @ticket 34073
- */
- public function test_term_meta_should_be_lazy_loaded_only_for_the_queries_in_which_the_term_has_posts() {
- global $wpdb;
-
- $posts = self::factory()->post->create_many( 3, array( 'post_status' => 'publish' ) );
- register_taxonomy( 'wptests_tax', 'post' );
- $terms = self::factory()->term->create_many( 6, array( 'taxonomy' => 'wptests_tax' ) );
-
- wp_set_object_terms( $posts[0], array( $terms[0], $terms[1] ), 'wptests_tax' );
- wp_set_object_terms( $posts[1], array( $terms[2], $terms[3] ), 'wptests_tax' );
- wp_set_object_terms( $posts[2], array( $terms[0], $terms[4], $terms[5] ), 'wptests_tax' );
-
- foreach ( $terms as $t ) {
- add_term_meta( $t, 'foo', 'bar' );
- }
-
- $q0 = new WP_Query( array( 'p' => $posts[0], 'cache_results' => true ) );
- $q1 = new WP_Query( array( 'p' => $posts[1], 'cache_results' => true ) );
- $q2 = new WP_Query( array( 'p' => $posts[2], 'cache_results' => true ) );
-
- /*
- * $terms[0] belongs to both $posts[0] and $posts[2], so `get_term_meta( $terms[0] )` should prime
- * the cache for term matched by $q0 and $q2.
- */
-
- // First request will hit the database.
- $num_queries = $wpdb->num_queries;
-
- // Prime caches.
- $this->assertSame( 'bar', get_term_meta( $terms[0], 'foo', true ) );
-
- // Two queries: one for $q0 and one for $q2.
- $num_queries += 2;
- $this->assertSame( $num_queries, $wpdb->num_queries );
-
- // Next requests should be in cache.
- $this->assertSame( 'bar', get_term_meta( $terms[1], 'foo', true ) );
- $this->assertSame( 'bar', get_term_meta( $terms[4], 'foo', true ) );
- $this->assertSame( 'bar', get_term_meta( $terms[5], 'foo', true ) );
- $this->assertSame( $num_queries, $wpdb->num_queries );
-
- // Querying for $terms[2] will prime $terms[3] as well.
- $this->assertSame( 'bar', get_term_meta( $terms[2], 'foo', true ) );
- $num_queries++;
- $this->assertSame( $num_queries, $wpdb->num_queries );
-
- $this->assertSame( 'bar', get_term_meta( $terms[3], 'foo', true ) );
- $this->assertSame( $num_queries, $wpdb->num_queries );
- }
-
- public function test_adding_term_meta_should_bust_get_terms_cache() {
- $terms = self::factory()->term->create_many( 2, array( 'taxonomy' => 'wptests_tax' ) );
-
- add_term_meta( $terms[0], 'foo', 'bar' );
-
- // Prime cache.
- $found = get_terms( 'wptests_tax', array(
- 'hide_empty' => false,
- 'fields' => 'ids',
- 'meta_query' => array(
- array(
- 'key' => 'foo',
- 'value' => 'bar',
- ),
- ),
- ) );
-
- $this->assertEqualSets( array( $terms[0] ), $found );
-
- add_term_meta( $terms[1], 'foo', 'bar' );
-
- $found = get_terms( 'wptests_tax', array(
- 'hide_empty' => false,
- 'fields' => 'ids',
- 'meta_query' => array(
- array(
- 'key' => 'foo',
- 'value' => 'bar',
- ),
- ),
- ) );
-
- $this->assertEqualSets( array( $terms[0], $terms[1] ), $found );
- }
-
</del><span class="cx" style="display: block; padding: 0 10px"> public function test_updating_term_meta_should_bust_get_terms_cache() {
</span><span class="cx" style="display: block; padding: 0 10px"> $terms = self::factory()->term->create_many( 2, array( 'taxonomy' => 'wptests_tax' ) );
</span><span class="cx" style="display: block; padding: 0 10px">
</span></span></pre>
</div>
</div>
</body>
</html>