#61532 closed defect (bug) (fixed)
Improve the performance of `WP_Image_Editor_Imagick::supports_mime_type()`
Reported by: | joemcgill | Owned by: | joemcgill |
---|---|---|---|
Milestone: | 6.7 | Priority: | normal |
Severity: | normal | Version: | |
Component: | Media | Keywords: | has-patch changes-requested |
Focuses: | performance | Cc: |
Description
WP_Image_Editor_Imagick::supports_mime_type()
is called whenever WordPress is choosing which image editor library to use via wp_image_editor_supports()
> _wp_image_editor_choose()
.
To see if the current server's version of ImageMagick supports a specific mime type, that method calls Imagick::queryFormats()
, which can take a long time to run.
In the editor, wp_plupload_default_settings()
makes triggers this code path to see if modern formats like WebP and AVIF are supported, which leads to a slower editor load time.
Since these supports values rarely change except for when changing hosting environments, we can cache these supports values for faster lookups.
Change History (17)
This ticket was mentioned in PR #6936 on WordPress/wordpress-develop by @joemcgill.
5 months ago
#1
- Keywords has-patch added
This ticket was mentioned in Slack in #core-performance by joemcgill. View the logs.
4 months ago
@adamsilverstein commented on PR #6936:
4 months ago
#3
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?
@adamsilverstein commented on PR #6936:
4 months ago
#4
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?
@flixos90 commented on PR #6936:
4 months ago
#5
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)?
This ticket was mentioned in Slack in #core-performance by mukeshpanchal27. View the logs.
3 months ago
@mukesh27 commented on PR #6936:
3 months ago
#7
Why aren't the unit tests and other GitHub jobs working for this PR?
#8
@
3 months ago
- Keywords changes-requested added
@joemcgill Could you please address the feedback on PR!
3 months ago
#9
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!
This ticket was mentioned in Slack in #core-performance by mukeshpanchal27. View the logs.
3 months ago
This ticket was mentioned in Slack in #core by chaion07. View the logs.
2 months ago
#12
@
2 months ago
Thanks @joemcgill for reporting this. We reviewed this Ticket during a recent bug-scrub session. Here's the summary of the discussion:
- The changes requested have not yet addressed in the PR. So we would appreciate if that is addressed sometime soon
- Seconding with Mukesh's comment
- As we are in the Beta stage of the 6.7 release, we would wait for a couple of days before updating the milestone later on
Thank you.
Props to @pratiklondhe
Cheers!
@joemcgill commented on PR #6936:
2 months ago
#13
@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).
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.
@joemcgill commented on PR #6936:
2 months ago
#14
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.
@joemcgill commented on PR #6936:
2 months ago
#15
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.
@joemcgill commented on PR #6936:
2 months ago
#17
Committed in https://core.trac.wordpress.org/changeset/59189.
Trac ticket: https://core.trac.wordpress.org/ticket/61532