Skip to content
This repository has been archived by the owner on Feb 23, 2024. It is now read-only.

Avoid calling plugin_dir_url a lot #5915

Merged
merged 1 commit into from
Feb 23, 2022
Merged

Avoid calling plugin_dir_url a lot #5915

merged 1 commit into from
Feb 23, 2022

Conversation

colinleroy
Copy link
Contributor

@colinleroy colinleroy commented Feb 22, 2022

Hello,
I've spent a bit of time optimizing my Wordpress + Woocommerce install. I've noticed that Automattic\WooCommerce\Blocks\Domain\Package->get_url() is called quite a few times (79 times on my installation), and recomputes plugin_dir_url each time.
This patch caches it in a static variable. It is a little improvement, but makes this function responsible for about 71 microseconds instead of 3.5 milliseconds, so I suppose it's something :)

Hope this helps.

Callstack before:
image

Callstack after:
image

Testing instructions

Visit some pages containing blocks and ensure they load correctly. Check you can still add blocks in the block editor.

Changelog

Cache plugin_dir_url instead of recomputing it dozens of times.

Copy link
Contributor

@opr opr left a comment

Choose a reason for hiding this comment

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

Hey there @colinleroy thanks for making this PR - it seems like a good improvement for sure. What do you think about moving this logic to the constructor and keep the $plugin_dir_url a field variable? This would be so we don't need to use the static keyword in the get_url method and we can keep get_url super simple.

Also, what are your thoughts on someone using the plugins_url filter somewhere in the middle of a request? How do you value the time improvement made here vs. potentially breaking sites that do this, however rare it might be?

@colinleroy
Copy link
Contributor Author

colinleroy commented Feb 22, 2022

Hey there @colinleroy thanks for making this PR - it seems like a good improvement for sure. What do you think about moving this logic to the constructor and keep the $plugin_dir_url a field variable? This would be so we don't need to use the static keyword in the get_url method and we can keep get_url super simple.

Hi @opr, thanks for your interest ! and thank you for the suggestion, this is cleaner your way! You can see I'm not quite acquainted with WP/WC's codebases yet :)

Also, what are your thoughts on someone using the plugins_url filter somewhere in the middle of a request? How do you value the time improvement made here vs. potentially breaking sites that do this, however rare it might be?

That's a very good question, I'm unsure of the answer ! First of all I think the time improvement is far less important than not breaking sites.
From my understanding, this code path is only taken when the various Woocommerce blocks initialize :
image

From the code, I think that all Woocommerce blocks initialize in sequence, from Woocommerce's main initialization. So I suppose no other plugin code is ran inbetween, which means either a plugins_url filter could run before, or after, but not in the middle of the blocks init iteration. So I think it is not a potential issue... at least I think that it doesn't change the current behaviour. But I'm not a hundred percent sure, it would be nice to have confirmation from developers who know the codebase.

@opr
Copy link
Contributor

opr commented Feb 22, 2022

From the code, I think that all Woocommerce blocks initialize in sequence, from Woocommerce's main initialization. So I suppose no other plugin code is ran inbetween, which means either a plugins_url filter could run before, or after, but not in the middle of the blocks init iteration. So I think it is not a potential issue... at least I think that it doesn't change the current behaviour. But I'm not a hundred percent sure, it would be nice to have confirmation from developers who know the codebase.

Thank you for looking into it and writing up your thoughts.

I've run some tests to see if get_url is called when handling code that extends WooCommerce blocks, it doesn't look like it is. I'm quite satisfied that this change won't affect extensions because like you say, all of this happens at once and nothing else should get chance to add a filter for plugins_url.

Thanks for doing this, and making the amends. Pinging @mikejolley for another review and to confidence check our findings about whether there is going to be issues with the plugin url changing.

Copy link
Member

@mikejolley mikejolley left a comment

Choose a reason for hiding this comment

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

Looks good, thanks for making those changes.

Copy link
Contributor

@opr opr left a comment

Choose a reason for hiding this comment

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

@colinleroy thanks for your work here, this improvement will be nice 😃 👍🏼

I added some testing instructions in your PR body, this is necessary for our release process.

@opr opr added the focus: performance The issue/PR is related to performance. label Feb 23, 2022
@opr
Copy link
Contributor

opr commented Feb 23, 2022

CI fails are due to it being a fork. Merging anyway and will make sure they pass on trunk.

@opr opr merged commit 9edbe4d into woocommerce:trunk Feb 23, 2022
tarhi-saad pushed a commit that referenced this pull request Feb 26, 2022
@frontdevde frontdevde added this to the 7.1.0 milestone Feb 28, 2022
@frontdevde frontdevde added the skip-changelog PRs that you don't want to appear in the changelog. label Feb 28, 2022
@senadir
Copy link
Member

senadir commented Apr 14, 2022

FWIW this PR is causing a regression with symlinked WooCommerce installations.

@colinleroy
Copy link
Contributor Author

Oh, I'm sorry about that! @shendy-a8c's fix seems good.

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
focus: performance The issue/PR is related to performance. skip-changelog PRs that you don't want to appear in the changelog.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

5 participants