Make WordPress Core

Opened 7 years ago

Closed 7 years ago

Last modified 7 years ago

#40490 closed enhancement (fixed)

Prime post cache in `wp_make_content_images_responsive()`

Reported by: dlh's profile dlh Owned by: joemcgill's profile joemcgill
Milestone: 4.9 Priority: normal
Severity: normal Version: 4.7
Component: Media Keywords: has-patch
Focuses: Cc:

Description

[35412] updated wp_make_content_images_responsive() so that it "warmed the cache" with update_meta_cache() before calling get_post_meta().

[38296] replaced get_post_meta() with wp_get_attachment_metadata(), but because the latter function calls get_post(), it can still cause a database query for each attachment that isn't in the cache.

The attached patch would have wp_make_content_images_responsive() use _prime_post_caches() again to avoid the individual queries.

Attachments (2)

40490.diff (812 bytes) - added by dlh 7 years ago.
40490.2.diff (808 bytes) - added by dlh 7 years ago.
Refining docs

Download all attachments as: .zip

Change History (8)

@dlh
7 years ago

@dlh
7 years ago

Refining docs

This ticket was mentioned in Slack in #core-media by joemcgill. View the logs.


7 years ago

#2 @joemcgill
7 years ago

  • Owner set to joemcgill
  • Status changed from new to reviewing

#3 @joemcgill
7 years ago

  • Milestone changed from Awaiting Review to 4.9

#4 @joemcgill
7 years ago

This is a great catch, @dlh.

Originally, when we implemented wp_make_content_images_responsive() we intentionally decided to only query the post meta using get_post_meta() as to avoid the extra Post queries, which is why update_meta_cache() was used in [35412]. However, it makes sense that someone filtering the values of wp_get_attachment_metadata() would want consistency here as well, so switching back to _prime_post_caches() is better here and avoids a bunch on unnecessary queries.

We may also want to consider ways we can avoid the get_post() check inside of wp_get_attachment_metadata() since those queries are often unnecessary when only metadata is needed.

In the mean time, this patch is a good improvement.

#5 @joemcgill
7 years ago

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

In 41315:

Media: Prime post caches in 'wp_make_content_images_responsive()'.

In [38296] we replaced get_post_meta() with wp_get_attachment_metadata()
so that attachment metadata could be consistently filtered. However, this
results in extra post queries which were previously avoided.

This uses _prime_post_caches() instead of update_meta_cache() to improve
post caching before looping through all images to retrieve attachment metadata.

Props dlh.
Fixes #40490.

This ticket was mentioned in Slack in #core-media by joemcgill. View the logs.


7 years ago

Note: See TracTickets for help on using tickets.