Skip to content

Conversation

mukeshpanchal27
Copy link
Member

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

This PR refactors the src/wp-admin/plugins.php file to improve efficiency and maintainability by caching the results of is_multisite() and is_network_admin() in local variables. All subsequent checks throughout the file now use these cached variables, reducing repeated function calls and improving readability. No functional behavior is changed.

Refactoring for efficiency and readability:

  • Cached is_multisite() and is_network_admin() results in $is_multisite and $is_network_admin variables at the top of the file, and replaced all repeated calls to these functions with the variables throughout the plugin management logic.
  • Updated all conditional checks and function calls that previously used is_multisite() and is_network_admin() to use the new local variables, including plugin activation, deactivation, automatic updates, and UI display logic.

This Pull Request is for code review only. Please keep all other discussion in the Trac ticket. Do not merge this Pull Request. See GitHub Pull Requests for Code Review in the Core Handbook for more details.

Comment on lines +24 to +25
$is_multisite = is_multisite();
$is_network_admin = is_network_admin();
Copy link
Member

Choose a reason for hiding this comment

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

It looks like these functions don't really do any work. They're either just looking at constants or the value of a class member variable. So doubt there would be any measurable improvement to doing this, but I'm happy to be wrong!

Copy link
Member Author

Choose a reason for hiding this comment

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

I do the benchmark for function call and local variable use over 100 iterations and found that it will improve ~50% time.

Average time spent for 1 x 100 iterations:
Use function - 5583
Store in local var - 1958
Change: -64.9%

Copy link
Member

Choose a reason for hiding this comment

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

What is the unit of time here?

Copy link
Member Author

Choose a reason for hiding this comment

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

Benchmark script
// Bootstrapping.

define('WP_USE_THEMES', true);

/** Loads the WordPress Environment and Template */
require ('wp-load.php');

// Setup.


$repetitions = 1;
$iterations  = 100;

// Profiling.

for ( $repetition = 0; $repetition <= $repetitions; $repetition ++ ) {

	if( $repetition == 0 ) {
		continue;
	}

	$time_start = hrtime( true );

	for ( $index = 0; $index < $iterations; $index ++ ) {
        is_multisite();
		is_network_admin();
	}

	$time_end                    = hrtime( true );
	$function[ $repetition ] = $time_end - $time_start;

	$time_start = hrtime( true );

    $is_multisite     = is_multisite();
    $is_network_admin = is_network_admin();
	for ( $index = 0; $index < $iterations; $index ++ ) {
		$call_m = $is_multisite;
        $call_n = $is_network_admin;
	}

	$time_end                 = hrtime( true );
	$local_var[ $repetition ] = $time_end - $time_start;
}

$function_avg  = array_sum( $function ) / $repetitions;
$local_var_avg = array_sum( $local_var ) / $repetitions;
$percentage    = round( $local_var_avg / $function_avg * 100, 1 ) - 100;

printf( "Average time spent for {$repetitions} x {$iterations} iterations:<br>" );
printf( "Use function - {$function_avg} <br>");
printf( "Store in local var - {$local_var_avg}<br>" );
printf( "Change: {$percentage}%%" );

Copy link
Member

Choose a reason for hiding this comment

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

OK, so hrtime() returns nanoseconds. That means 5,583 nanoseconds is 0.005583 milliseconds. This is extremely tiny. I don't think it's necessarily bad to make this change, except for possibility that a plugin could override these globals and cause something unexpected.

Copy link
Member Author

Choose a reason for hiding this comment

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

Thanks, @westonruter! What do you think should be the next step here?

Copy link
Member

Choose a reason for hiding this comment

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

Well, I don't really see the need for this change.

Copy link
Member Author

Choose a reason for hiding this comment

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

Thanks. Close the Ticket and PR.

Copy link

github-actions bot commented Oct 4, 2025

Test using WordPress Playground

The changes in this pull request can previewed and tested using a WordPress Playground instance.

WordPress Playground is an experimental project that creates a full WordPress instance entirely within the browser.

Some things to be aware of

  • The Plugin and Theme Directories cannot be accessed within Playground.
  • All changes will be lost when closing a tab with a Playground instance.
  • All changes will be lost when refreshing the page.
  • A fresh instance is created each time the link below is clicked.
  • Every time this pull request is updated, a new ZIP file containing all changes is created. If changes are not reflected in the Playground instance,
    it's possible that the most recent build failed, or has not completed. Check the list of workflow runs to be sure.

For more details about these limitations and more, check out the Limitations page in the WordPress Playground documentation.

Test this pull request with WordPress Playground.

@mukeshpanchal27 mukeshpanchal27 marked this pull request as ready for review October 4, 2025 05:31
Copy link

github-actions bot commented Oct 4, 2025

The following accounts have interacted with this PR and/or linked issues. I will continue to update these lists as activity occurs. You can also manually ask me to refresh this list by adding the props-bot label.

Core Committers: Use this line as a base for the props when committing in SVN:

Props mukesh27, westonruter.

To understand the WordPress project's expectations around crediting contributors, please review the Contributor Attribution page in the Core Handbook.

@mukeshpanchal27 mukeshpanchal27 self-assigned this Oct 4, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants