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 9 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, desrosj, westonruter, flixos90, adamsilverstein, mukesh27.

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

@joemcgill joemcgill self-assigned this Aug 26, 2024
@mukeshpanchal27
Copy link
Member

Why aren't the unit tests and other GitHub jobs working for this PR?

@desrosj
Copy link
Contributor

desrosj commented Aug 28, 2024

Why aren't the unit tests and other GitHub jobs working for this PR?

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 master branch).

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 trunk into this branch and it seems like everything is now running!

@joemcgill
Copy link
Member Author

@adamsilverstein:

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?

Was alerted to the issue while checkout out the XHProf flamegraphs demo from @joehoyle here:https://x.com/joe_hoyle/status/1806347618185875819

@felixarntz:

This is a good idea IMO, though at first glance I think this cache would apply at a very low level.

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 wp_image_editor_supports check, which is the function that is that causes _wp_image_editor_choose to be called most often (4 times during a normal editor load).

image

With this PR applied, _wp_image_editor_choose never gets called with a warm cache.

I've added a new global cache group, called 'image_editor', and store this value to a wp_image_editor_supports cache key, with a TTL of 1 day.

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 I think the cache is missing one crucial detail, but otherwise it's looking close.

src/wp-includes/media.php Outdated Show resolved Hide resolved
@@ -876,6 +876,7 @@ function wp_start_object_cache() {
'blog-lookup',
'blog_meta',
'global-posts',
'image_editor',
Copy link
Member

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.

@joemcgill
Copy link
Member Author

joemcgill commented Oct 4, 2024

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 _wp_image_editor_choose() and the results are looking pretty good.

Here's my local profile of the /wp-admin/post-new.php page before and after the cache is warmed. Almost all of the savings is still coming from avoiding calls to WP_Image_Editor_Imagick::supports_mime_type but this is a bit broader and should have a noticeable impact on editor loading times on systems with a persistent object cache and may also speed up image uploads.

Cold cache

image

Warm cache

image

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 implementation looks good to me and would work. I have one question on the approach that I missed to spot before.

Comment on lines +4127 to +4136
$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 ] ) ) {
Copy link
Member

@felixarntz felixarntz Oct 4, 2024

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.

Copy link
Member Author

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.

Copy link
Member

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.

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.

LGTM!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
Status: Code Review 👀
Development

Successfully merging this pull request may close these issues.

6 participants