[wp-trac] [WordPress Trac] #60510: Plugins Dependencies: performance improvements with finding plugin's file relative to plugins directory.
WordPress Trac
noreply at wordpress.org
Mon Feb 12 23:09:11 UTC 2024
#60510: Plugins Dependencies: performance improvements with finding plugin's file
relative to plugins directory.
------------------------------+-----------------------------
Reporter: hellofromTonya | Owner: hellofromTonya
Type: defect (bug) | Status: closed
Priority: normal | Milestone: 6.5
Component: Upgrade/Install | Version: trunk
Severity: normal | Resolution: fixed
Keywords: has-patch commit | Focuses: performance
------------------------------+-----------------------------
Changes (by hellofromTonya):
* status: reviewing => closed
* resolution: => fixed
Comment:
In [changeset:"57606" 57606]:
{{{
#!CommitTicketReference repository="" revision="57606"
Upgrade/Install: Micro-optimizations for getting plugin_file in plugins
loader loop.
RE: Plugins Dependencies.
The following micro-optimization improvements are included for finding
each plugin's file relative to the plugins' directory within `wp-
settings.php`:
* Move `trailingslashit()` before `foreach()`.
The path to the plugin directory is a constant. Invoking the
`trailingslashit()` within the loop for each plugin is unnecessary and
less performant.
This commit moves the plugin directory logic to before the loop. The
result: the logic will now run 1x instead of Px where P represents the
number of active and valid plugins to be loaded.
* Use `substr()` instead of `str_replace()` to extract the plugin's file
relative to the plugins' directory.
`substr()` is more performant than `str_replace()`.
Why?
Per the PHP handbook:
>"This function returns a string or an array with all occurrences of
search in subject replaced with the given replace value."
`str_replace()` searches the entire string to find and replace each
substring occurrence.
whereas
>"Returns the portion of string specified by the offset and length
parameters."
`substr()` starts at the given offset and stops at the given (or end of
the) string length.
In other words, `substr()` iterates over less of and only a specific
portion of the given input string, whereas `str_replace()` iterates
through the entire string searching for matches (plural).
References:
* `str_replace()` https://www.php.net/manual/en/function.str-replace.php
* `substr()` https://www.php.net/manual/en/function.substr.php
* `strlen()` https://www.php.net/manual/en/function.strlen.php
* Show the comparison in action https://3v4l.org/TbQ9U.
Follow-up to [57545], [57592].
Props hellofromTonya, costdev.
Fixes #60510.
}}}
--
Ticket URL: <https://core.trac.wordpress.org/ticket/60510#comment:5>
WordPress Trac <https://core.trac.wordpress.org/>
WordPress publishing platform
More information about the wp-trac
mailing list