WordPress.org

Make WordPress Core

Opened 5 years ago

Closed 5 years ago

#12611 closed defect (bug) (fixed)

Performance hit from update_post_caches

Reported by: prettyboymp Owned by: ryan
Milestone: 3.0 Priority: normal
Severity: normal Version:
Component: Performance Keywords: dev-feedback
Focuses: Cc:

Description

I question the benefit from updating the post cache during the update_post_caches function, specifically where WP_Query->get_posts() calls the update_post_caches with the returned posts. Since a majority of all get_post() calls using posts that were returned from WP_Query->get_posts() will be used within the loop where the global $post would be used instead of the instance in cache.

Normally, this wouldn't be a big deal, except that in performance testing on a site with memcache setup, the cost of the call to update_post_caches is responsible for ~127ms, or 10% - 20% of the rendering time.

Attachments (3)

update_post_caches.diff (1.2 KB) - added by scribu 5 years ago.
add 'cache_results' option
12611.diff (838 bytes) - added by ryan 5 years ago.
12611.2.diff (810 bytes) - added by ryan 5 years ago.

Download all attachments as: .zip

Change History (13)

comment:1 @nacin5 years ago

  • Milestone changed from Unassigned to 3.0

comment:2 @nacin5 years ago

  • Owner set to ryan
  • Status changed from new to assigned

comment:3 @jeffstieler5 years ago

  • Cc jeff@… added

This can cause problems under the following conditions:

If you use query_posts and filter the returned columns using the 'posts_fields' filter, the resulting post objects will be cached, and could disrupt normal behavior in your theme.

This caused an issue for me in a theme recently where I was only retrieving post IDs using query_posts, and then in the archive page loop posts' dates were missing and the permalinks were incorrect.

This also had the side effect of an infinite redirect in the admin when trying to edit the affected posts.

@scribu5 years ago

add 'cache_results' option

comment:4 @scribu5 years ago

  • Keywords has-patch added

If you use query_posts and filter the returned columns using the 'posts_fields' filter, the resulting post objects will be cached, and could disrupt normal behavior in your theme.

I've run into this problem too. Patch adds a 'cache_results' option to WP_Query.

I also noticed that update_post_caches() uses an unefficient for loop, when a faster foreach could be used.

comment:5 @scribu5 years ago

Also see #12989.

comment:6 @scribu5 years ago

  • Keywords has-patch removed
  • Resolution set to fixed
  • Status changed from assigned to closed

Since [14310], you can control wether update_post_caches() is called or not.

comment:7 @ryan5 years ago

Here's something I've been using on wordpress.com (adapted for [14310])

@ryan5 years ago

comment:8 @ryan5 years ago

It probably makes sense to never call update_post_caches() when using a persistent cache. The idea with the threshold in that patch was avoiding extra cache priming queries for queries with a low number of posts, but it probably doesn't matter much.

@ryan5 years ago

comment:9 @ryan5 years ago

  • Resolution fixed deleted
  • Status changed from closed to reopened

Opening to suggest we set cache_results to false, by default, if an external object cache is being used.

comment:10 @automattor5 years ago

  • Resolution set to fixed
  • Status changed from reopened to closed

(In [14665]) Don't update_post_caches if a persistent object cache is installed. fixes #12611

Note: See TracTickets for help on using tickets.