@@ -45,6 +45,78 @@ public function the_post() {
4545 }
4646}
4747
48+ class WP_Query_Trunk extends WP_Query {
49+ /**
50+ * Sets up the current post.
51+ *
52+ * Retrieves the next post, sets up the post, sets the 'in the loop'
53+ * property to true.
54+ *
55+ * @since 1.5.0
56+ *
57+ * @global WP_Post $post Global post object.
58+ */
59+ public function the_post () {
60+ global $ post ;
61+
62+ if ( ! $ this ->in_the_loop ) {
63+ // Get post IDs to prime incomplete post objects.
64+ $ post_ids = array_reduce (
65+ $ this ->posts ,
66+ function ( $ carry , $ post ) {
67+ if ( is_numeric ( $ post ) && $ post > 0 ) {
68+ // Query for post ID.
69+ $ carry [] = $ post ;
70+ }
71+
72+ if ( is_object ( $ post ) && isset ( $ post ->ID ) ) {
73+ // Query for object, either WP_Post or stdClass.
74+ $ carry [] = $ post ->ID ;
75+ }
76+
77+ return $ carry ;
78+ },
79+ array ()
80+ );
81+ if ( $ post_ids ) {
82+ _prime_post_caches ( $ post_ids , $ this ->query_vars ['update_post_term_cache ' ], $ this ->query_vars ['update_post_meta_cache ' ] );
83+ }
84+ $ post_objects = array_map ( 'get_post ' , $ post_ids );
85+ update_post_author_caches ( $ post_objects );
86+ }
87+
88+ $ this ->in_the_loop = true ;
89+ $ this ->before_loop = false ;
90+
91+ if ( -1 === $ this ->current_post ) { // Loop has just started.
92+ /**
93+ * Fires once the loop is started.
94+ *
95+ * @since 2.0.0
96+ *
97+ * @param WP_Query $query The WP_Query instance (passed by reference).
98+ */
99+ do_action_ref_array ( 'loop_start ' , array ( &$ this ) );
100+ }
101+
102+ $ post = $ this ->next_post ();
103+
104+ // Ensure a full post object is available.
105+ if ( $ post instanceof stdClass ) {
106+ // stdClass indicates that a partial post object was queried.
107+ $ post = get_post ( $ post ->ID );
108+ } elseif ( is_numeric ( $ post ) ) {
109+ // Numeric indicates that only post IDs were queried.
110+ $ post = get_post ( $ post );
111+ }
112+
113+ // Set up the global post object for the loop.
114+ $ this ->setup_postdata ( $ post );
115+ }
116+ }
117+
118+
119+
48120
49121/**
50122 * @group query
@@ -384,91 +456,65 @@ public function test_post_preview_links_autosaves() {
384456 * @dataProvider data_the_loop_fields
385457 */
386458 public function test_the_post_changes_perf ( $ fields ) {
387-
388- // Prime options, etc in query
389- $ query = new WP_Query (
390- array (
391- 'fields ' => $ fields ,
392- 'post_type ' => 'page ' ,
393- 'posts_per_page ' => -1 ,
394- )
395- );
396-
397-
398-
399- wp_cache_flush_group ( 'posts ' );
400- wp_cache_flush_group ( 'post-queries ' );
401-
402- $ time_start = microtime ( true );
403- $ num_queries_start = get_num_queries ();
404-
405-
406-
407- $ query = new WP_Query (
408- array (
409- 'fields ' => $ fields ,
410- 'post_type ' => 'page ' ,
411- 'posts_per_page ' => -1 ,
412- )
413- );
414-
415-
416-
417- $ iterations = 1000 ;
418- while ( $ iterations -- ) {
419- // Traverse the loop.
420- while ( $ query ->have_posts () ) {
421- $ query ->the_post ();
422- get_post ();
423- get_the_author ();
459+ $ query_classes = array ( 'WP_Query_67 ' , 'WP_Query_Trunk ' , 'WP_Query ' );
460+ $ do_iterations = 1000 ;
461+ $ results = array ();
462+ foreach ( $ query_classes as $ query_class ) {
463+
464+ $ iterations = $ do_iterations ;
465+ $ start_time = microtime ( true );
466+ $ start_queries = get_num_queries ();
467+
468+ while ( $ iterations -- ) {
469+ // Flush the relevant caches.
470+ wp_cache_flush_group ( 'posts ' );
471+ wp_cache_flush_group ( 'post_meta ' );
472+ wp_cache_flush_group ( 'post-queries ' );
473+ wp_cache_flush_group ( 'users ' );
474+ wp_cache_flush_group ( 'user_meta ' );
475+
476+ $ query = new $ query_class (
477+ array (
478+ 'fields ' => $ fields ,
479+ 'post_type ' => 'page ' ,
480+ 'posts_per_page ' => -1 ,
481+ )
482+ );
483+
484+ // Traverse the loop.
485+ while ( $ query ->have_posts () ) {
486+ $ query ->the_post ();
487+ get_post ();
488+ get_the_author ();
489+ }
424490 }
425491
426- $ query ->rewind_posts ();
427- $ query ->in_the_loop = false ;
428- wp_cache_flush_group ( 'posts ' );
429- }
430-
431- $ new_loop_time = microtime ( true ) - $ time_start ;
432- $ new_loop_queries = get_num_queries () - $ num_queries_start ;
433-
434- wp_cache_flush_group ( 'posts ' );
435- wp_cache_flush_group ( 'post-queries ' );
436-
437- $ time_start = microtime ( true );
438- $ num_queries_start = get_num_queries ();
439-
440- $ iterations = 1000 ;
441- $ query = new WP_Query_67 (
442- array (
443- 'fields ' => $ fields ,
444- 'post_type ' => 'page ' ,
445- 'posts_per_page ' => -1 ,
446- )
447- );
448-
449- while ( $ iterations -- ) {
450- // Traverse the loop.
451- while ( $ query ->have_posts () ) {
452- $ query ->the_post ();
453- get_post ();
454- get_the_author ();
455- }
492+ $ loop_time = microtime ( true ) - $ start_time ;
493+ $ loop_queries = get_num_queries () - $ start_queries ;
494+ $ loop_queries_per_iteration = $ loop_queries / $ do_iterations ;
456495
457- $ query ->rewind_posts ();
458- $ query ->in_the_loop = false ;
459- wp_cache_flush_group ( 'posts ' );
496+ // store results.
497+ $ results [ $ fields ][ $ query_class ] = array (
498+ 'loop_time ' => $ loop_time ,
499+ 'loop_queries ' => $ loop_queries ,
500+ 'loop_queries_per_iteration ' => $ loop_queries_per_iteration ,
501+ );
460502 }
461503
462-
463- $ old_loop_time = microtime ( true ) - $ time_start ;
464- $ old_loop_queries = get_num_queries () - $ num_queries_start ;
504+ foreach ( $ results as &$ result ) {
505+ $ result ['Time Trunk - 67 ' ] = $ result ['WP_Query_Trunk ' ]['loop_time ' ] - $ result ['WP_Query_67 ' ]['loop_time ' ];
506+ $ result ['Time Patch - 67 ' ] = $ result ['WP_Query ' ]['loop_time ' ] - $ result ['WP_Query_67 ' ]['loop_time ' ];
507+ $ result ['Time Patch - Trunk ' ] = $ result ['WP_Query ' ]['loop_time ' ] - $ result ['WP_Query_Trunk ' ]['loop_time ' ];
508+ }
465509
466510 // Dump the results.
467511 ob_start ();
468- var_dump ( $ fields );
469- var_dump ( 'new ' , $ new_loop_time , $ new_loop_queries );
470- var_dump ( 'old ' , $ old_loop_time , $ old_loop_queries );
512+ var_dump ( $ results );
471513 $ output = ob_get_clean ();
472514 fwrite (STDERR , $ output );
515+
516+
517+ $ this ->assertLessThanOrEqual ( 0 , $ results [$ fields ]['Time Patch - Trunk ' ], 'Improving performance should improve performance ' );
518+
473519 }
474520}
0 commit comments