-
Notifications
You must be signed in to change notification settings - Fork 2.5k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Cache results from Imagick::queryFormats #6936
base: trunk
Are you sure you want to change the base?
Cache results from Imagick::queryFormats #6936
Conversation
The following accounts have interacted with this PR and/or linked issues. I will continue to update these lists as activity occurs. You can also manually ask me to refresh this list by adding the Core Committers: Use this line as a base for the props when committing in SVN:
To understand the WordPress project's expectations around crediting contributors, please review the Contributor Attribution page in the Core Handbook. |
Test using WordPress PlaygroundThe changes in this pull request can previewed and tested using a WordPress Playground instance. WordPress Playground is an experimental project that creates a full WordPress instance entirely within the browser. Some things to be aware of
For more details about these limitations and more, check out the Limitations page in the WordPress Playground documentation. |
dfb8e27
to
d454fa8
Compare
Great idea, love it! Curious how you came to the realization that the mime type check call was slow/expensive? Did you see it in a trace somewhere? |
// Imagick::queryFormats is not performant, so cache the results. | ||
$supports = wp_cache_get( 'imagick_supports' ); | ||
|
||
if ( ! $supports ) { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Since technically any type could be stored in the cache value:
if ( ! $supports ) { | |
if ( ! is_array( $supports ) ) { |
$supports = array(); | ||
} | ||
|
||
if ( isset( $supports[ $imagick_extension ] ) ) { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Hardening, although this function doesn't currently have a strict type hint return value:
if ( isset( $supports[ $imagick_extension ] ) ) { | |
if ( isset( $supports[ $imagick_extension ] ) && is_bool( $supports[ $imagick_extension ] ) ) { |
} | ||
|
||
if ( isset( $supports[ $imagick_extension ] ) ) { | ||
return $supports[ $imagick_extension ]; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Alternatively:
return $supports[ $imagick_extension ]; | |
return (bool) $supports[ $imagick_extension ]; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@joemcgill This is a good idea IMO, though at first glance I think this cache would apply at a very low level.
I think a more comprehensive solution would be to cache part of what _wp_image_editor_choose()
does, for example:
- After the
wp_image_editors
filter, hash the array of classes and$args
and use that in the cache key. - This cache would then avoid the entire
foreach
loop, including any other potentially expensive check methods from other image editor class implementations. - The values of the filter probably barely ever changes, but by using a hash we can ensure no stale value would be served.
WDYT?
} | ||
|
||
// Imagick::queryFormats is not performant, so cache the results. | ||
$supports = wp_cache_get( 'imagick_supports' ); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We shouldn't use the object cache without a group. Not sure what would be most fitting for this use-case, but I think we should try to find one.
Interesting idea to add caching up at this level, although this PR's changes could still be useful for My one concern would be ensuring the cache has some expiration so we can catch new capabilities when users upgrade their systems. This brings me back to wondering how significant this improvement is - does profiling indicate this function is problematic? |
This is a good point, would be good to clarify how bad the performance of the uncached function makes it. And related to expiration, I wonder: @joemcgill Are you thinking this should be persistently cached, or would it already benefit to be cached non-persistently (e.g. because the function is called many times during a single page load)? |
Why aren't the unit tests and other GitHub jobs working for this PR? |
…agick-mime-supports
I've seen this happen before if the PR is opened with a base branch that does not contain GitHub Action workflows (such as the old It does not show that the base branch for the PR was changed after opening, but I've gone and merged the current state of |
Was alerted to the issue while checkout out the XHProf flamegraphs demo from @joehoyle here:https://x.com/joe_hoyle/status/1806347618185875819
That's a good observation. I was trying to specifically target this one known issue that was impacting the editor. However, I think caching at a higher level makes sense. I've updated this PR to cache the results of the With this PR applied, I've added a new global cache group, called 'image_editor', and store this value to a |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@joemcgill I think the cache is missing one crucial detail, but otherwise it's looking close.
@@ -876,6 +876,7 @@ function wp_start_object_cache() { | |||
'blog-lookup', | |||
'blog_meta', | |||
'global-posts', | |||
'image_editor', |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
+1 to using a global cache group.
Thanks @felixarntz. Good point about the implementation filter. Also, I was originally thinking just caching the supports check would be safer, but caching the implementation does have broader potential impact. I've updated this PR to move the cache to Here's my local profile of the Cold cacheWarm cache |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@joemcgill This implementation looks good to me and would work. I have one question on the approach that I missed to spot before.
$editors = wp_cache_get( 'wp_image_editor_choose', 'image_editor' ); | ||
|
||
if ( ! is_array( $editors ) ) { | ||
$editors = array(); | ||
} | ||
|
||
// Cache the chosen editor implementation based on specific args and available implementations. | ||
$cache_key = md5( serialize( array( $args, $implementations ) ) ); | ||
|
||
if ( isset( $editors[ $cache_key ] ) ) { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'm curious what's the rationale for nesting all the cache values in a single cache key "bucket". In most places in Core we store individual values per cache key, and the dynamic portion becomes part of the actual cache key.
For example the actual cache key would be:
$cache_key = 'wp_image_editor_choose_' . md5( serialize( array( $args, $implementations ) ) );
Since these caches would expire after 1 day anyway, I don't think there's any harm in storing in individual cache keys. This approach here might work too, but there's potential concerns about scalability (if too many different combinations of arguments are all stored under a single cache key) and diverging from how I believe we typically handle such caches in Core.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I didn't have any real intention when I chose to implement it this way, but I can see a few benefits.
Generally, including the hash as part of the name is done for the purpose of avoiding stale caches (i.e., if the values that are hashed change, the wp_get_cache()
call will result in a cache miss). That's not really a concern for this cache because the need to store the data for long periods while avoiding returning stale values is not that high.
This specific function can get called multiple times during the same run with different args. Having the results stored in multiple distinct caches would mean that we need to load separate cache values for each specific set of args. By saving all of the values to one cache, all of the cached values are loaded to memory the first time _wp_image_editor_choose()
is called, and can be accessed from memory on each subsequent call.
Another minor benefit is that it makes it a bit easier to work with a cache that has a static name in the DB instead of a dynamic one. As a theoretical example you could do something like wp cache get wp_image_editor_choose image_editor
or wp cache delete wp_image_editor_choose image_editor
to review or remove the values, which is more difficult with a dynamic name.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
That sounds good to me, makes sense. Let's go with this approach then.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
LGTM!
Trac ticket: https://core.trac.wordpress.org/ticket/61532
This Pull Request is for code review only. Please keep all other discussion in the Trac ticket. Do not merge this Pull Request. See GitHub Pull Requests for Code Review in the Core Handbook for more details.