Make WordPress Core

Changeset 57606


Ignore:
Timestamp:
02/12/2024 11:08:55 PM (8 months ago)
Author:
hellofromTonya
Message:

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.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/src/wp-settings.php

    r57592 r57606  
    500500
    501501// Load active plugins.
    502 $all_plugin_data = get_option( 'plugin_data', array() );
    503 $failed_plugins  = array();
     502$all_plugin_data    = get_option( 'plugin_data', array() );
     503$failed_plugins     = array();
     504$plugins_dir_strlen = strlen( trailingslashit( WP_PLUGIN_DIR ) );
    504505foreach ( wp_get_active_and_valid_plugins() as $plugin ) {
    505     $plugin_file    = str_replace( trailingslashit( WP_PLUGIN_DIR ), '', $plugin );
     506    $plugin_file    = substr( $plugin, $plugins_dir_strlen );
    506507    $plugin_headers = $all_plugin_data[ $plugin_file ];
    507508    $errors         = array();
Note: See TracChangeset for help on using the changeset viewer.