-
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)? |
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.