Make WordPress Core

Opened 2 months ago

Closed 2 months ago

Last modified 2 months ago

#60510 closed defect (bug) (fixed)

Plugins Dependencies: performance improvements with finding plugin's file relative to plugins directory.

Reported by: hellofromtonya's profile hellofromTonya Owned by: hellofromtonya's profile hellofromTonya
Milestone: 6.5 Priority: normal
Severity: normal Version: 6.5
Component: Upgrade/Install Keywords: has-patch commit
Focuses: performance Cc:

Description (last modified by hellofromTonya)

Micro-optimizations exist for finding each plugin's file relative to the plugins' directory within the following logic of the plugins' loader loop in wp-settings.php:

foreach ( wp_get_active_and_valid_plugins() as $plugin ) {
    $plugin_file = str_replace( trailingslashit( WP_PLUGIN_DIR ), '', $plugin );
  • The path to the plugin directory is a constant. Invoking the trailingslashit() within the loop for each plugin is unnecessary and less performant.

Propose to move it before the foreach() loop.

Propose: calculating the plugins' directory path's strlen() before the loop and switching to substr().

Follow-up to [57545] / #22316 and [57592] / #60461.

References:

Change History (7)

#1 @hellofromTonya
2 months ago

  • Description modified (diff)

This ticket was mentioned in PR #6089 on WordPress/wordpress-develop by @hellofromTonya.


2 months ago
#2

  • Keywords has-patch added

For the Plugins Dependencies additions to the plugins' loader loop in wp-settings.php, there are micro-optimization improvements for finding each plugin's file relative to the plugins' directory:

  • Micro-optimization 1:

    The path to the plugin directory is a constant. Invoking the trailingslashit() within the loop for each plugin is unnecessary and less performant.

Move the plugin directory logic to before the loop.

  • Micro-optimization 2:

    str_replace() is less performant than substr()

Calculate the plugins' directory path string length strlen() before the loop and then switch to using substr() instead of str_replace(). See the performance differences in action https://3v4l.org/TbQ9U).

Trac ticket: https://core.trac.wordpress.org/ticket/60510

@hellofromTonya commented on PR #6089:


2 months ago
#3

Awesome @costdev. I'll commit it.

#4 @hellofromTonya
2 months ago

  • Keywords commit added
  • Owner set to hellofromTonya
  • Status changed from new to reviewing

Discussed and reviewed the changes with @costdev who approved the patch. Marking it for commit.

#5 @hellofromTonya
2 months ago

  • Resolution set to fixed
  • Status changed from reviewing to closed

In 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:

Follow-up to [57545], [57592].

Props hellofromTonya, costdev.
Fixes #60510.

#7 @costdev
2 months ago

In 57658:

Plugin Dependencies: Remove auto-deactivation and bootstrapping logic.

Automatic deactivation of dependents with unmet dependencies requires a write operation to the database. This was performed during Core's bootstrap, which risked the database and cache becoming out-of-sync on sites with heavy traffic.

No longer loading plugins that have unmet requirements has not had a final approach decided core-wide, and is still in discussion in #60491 to be handled in a future release.

The plugin_data option, used to persistently store plugin data for detecting unmet dependencies during Core's bootstrap, is no longer needed.

Follow-up to [57545], [57592], [57606], [57617].

Props dd32, azaozz, swissspidy, desrosj, afragen, pbiron, zunaid321, costdev.
Fixes #60457. See #60491, #60510, #60518.

Note: See TracTickets for help on using tickets.