Skip to content
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

Open
wants to merge 3 commits into
base: trunk
Choose a base branch
from

Conversation

joemcgill
Copy link
Member

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.

Copy link

github-actions bot commented Jun 28, 2024

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 props-bot label.

Core Committers: Use this line as a base for the props when committing in SVN:

Props joemcgill, westonruter, flixos90, adamsilverstein.

To understand the WordPress project's expectations around crediting contributors, please review the Contributor Attribution page in the Core Handbook.

Copy link

Test using WordPress Playground

The 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

  • The Plugin and Theme Directories cannot be accessed within Playground.
  • All changes will be lost when closing a tab with a Playground instance.
  • All changes will be lost when refreshing the page.
  • A fresh instance is created each time the link below is clicked.
  • Every time this pull request is updated, a new ZIP file containing all changes is created. If changes are not reflected in the Playground instance,
    it's possible that the most recent build failed, or has not completed. Check the list of workflow runs to be sure.

For more details about these limitations and more, check out the Limitations page in the WordPress Playground documentation.

Test this pull request with WordPress Playground.

@joemcgill joemcgill force-pushed the fix/61532-cache-imagick-mime-supports branch from dfb8e27 to d454fa8 Compare June 28, 2024 18:25
@adamsilverstein
Copy link
Member

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 ) {
Copy link
Member

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:

Suggested change
if ( ! $supports ) {
if ( ! is_array( $supports ) ) {
$supports = array();
}

if ( isset( $supports[ $imagick_extension ] ) ) {
Copy link
Member

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:

Suggested change
if ( isset( $supports[ $imagick_extension ] ) ) {
if ( isset( $supports[ $imagick_extension ] ) && is_bool( $supports[ $imagick_extension ] ) ) {
}

if ( isset( $supports[ $imagick_extension ] ) ) {
return $supports[ $imagick_extension ];
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Alternatively:

Suggested change
return $supports[ $imagick_extension ];
return (bool) $supports[ $imagick_extension ];
Copy link
Member

@felixarntz felixarntz left a 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' );
Copy link
Member

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.

@adamsilverstein
Copy link
Member

I think a more comprehensive solution would be to cache part of what _wp_image_editor_choose() does, for example:

Interesting idea to add caching up at this level, although this PR's changes could still be useful for supports_mime_type since that could be called from elsewhere including plugins.

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?

@felixarntz
Copy link
Member

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
4 participants