Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

REST API: remove unncessary local variables and reorders the expressions in if statements to optimize performance #7708

Open
wants to merge 5 commits into
base: trunk
Choose a base branch
from

Conversation

anton-vlasenko
Copy link

@anton-vlasenko anton-vlasenko commented Nov 1, 2024

  • remove unnecessary local variables;
  • reorder expressions in if statements to optimize performance;
  • fix issues with calling static methods dynamically and non-static methods statically;
  • merge isset() and unset() statements;
  • explicitly return null in WP_REST_Posts_Controller::handle_terms().

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


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.

2. Reorders order of expressions in if statements to optimize performance.
3. Fixes issues with callling stating methods dynamically and non-static method statically.
4. Merges isset() and unset() statements.
5. Explicitly return null in WP_REST_Posts_Controller::handle_terms().
Copy link

github-actions bot commented Nov 1, 2024

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.

@anton-vlasenko anton-vlasenko marked this pull request as ready for review November 1, 2024 19:53
Copy link

github-actions bot commented Nov 1, 2024

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.

Unlinked Accounts

The following contributors have not linked their GitHub and WordPress.org accounts: @anton@antons-mac-mini.local.

Contributors, please read how to link your accounts to ensure your work is properly credited in WordPress releases.

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

Props antonvlasenko, mukesh27, apermo.

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

@@ -722,7 +721,7 @@ public function edit_media_item( $request ) {
$new_image_meta = wp_generate_attachment_metadata( $new_attachment_id, $saved['path'] );

// Copy the EXIF metadata from the original attachment if not generated for the edited image.
if ( isset( $image_meta['image_meta'] ) && isset( $new_image_meta['image_meta'] ) && is_array( $new_image_meta['image_meta'] ) ) {
if ( isset( $image_meta['image_meta'], $new_image_meta['image_meta'] ) && is_array( $new_image_meta['image_meta'] ) ) {
Copy link
Member

@mukeshpanchal27 mukeshpanchal27 Nov 4, 2024

Choose a reason for hiding this comment

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

Core didn't use isset for two different variable.

Copy link
Author

@anton-vlasenko anton-vlasenko Nov 4, 2024

Choose a reason for hiding this comment

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

Hi @mukeshpanchal27, thank you for your review!

Core didn't use isset for two different variables.

After double-checking, it looks like Core does sometimes use a single isset() statement for different variables. For example, you can find this approach in:

  • WP_Plugin_Install_List_Table::order_callback();
  • WP_REST_Menu_Items_Controller::prepare_items_query();
  • WP_Community_Events::coordinates_match();
  • parts of the Media Library code and other places.

I thought using a single isset() here could be a small optimization since it would prevent calling isset() multiple times. What do you think?

Copy link

Choose a reason for hiding this comment

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

imho using a single isset (as well as unset) is fine. I wouldn't go that far calling it a performance optimization, but in my eyes it improves readability.

Copy link
Member

Choose a reason for hiding this comment

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

It's not a blocker if we go with a single isset. It's used in some places already, so there are no objections.

@mukeshpanchal27
Copy link
Member

Have you check the performance number before/after?

@anton-vlasenko
Copy link
Author

anton-vlasenko commented Nov 6, 2024

Have you check the performance number before/after?

@mukeshpanchal27 I measured the performance improvement after you asked this question.
For example, in the widget controller, the improvement is about 0.6-1.5 microseconds per widget (of course, if certain conditions are met, and it depends greatly on the particular system).
The difference is measureable but minimal.
I expect similar results when measuring the effect that reordering conditional expressions in if statements brings to the other controllers in this PR.

@@ -722,7 +721,7 @@ public function edit_media_item( $request ) {
$new_image_meta = wp_generate_attachment_metadata( $new_attachment_id, $saved['path'] );

// Copy the EXIF metadata from the original attachment if not generated for the edited image.
if ( isset( $image_meta['image_meta'] ) && isset( $new_image_meta['image_meta'] ) && is_array( $new_image_meta['image_meta'] ) ) {
if ( isset( $image_meta['image_meta'], $new_image_meta['image_meta'] ) && is_array( $new_image_meta['image_meta'] ) ) {
Copy link

Choose a reason for hiding this comment

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

imho using a single isset (as well as unset) is fine. I wouldn't go that far calling it a performance optimization, but in my eyes it improves readability.

@@ -418,7 +417,7 @@ public function create_post_autosave( $post_data, array $meta = array() ) {
$revision_id = _wp_put_post_revision( $post_data, true );
}

if ( is_wp_error( $revision_id ) || 0 === $revision_id ) {
if ( 0 === $revision_id || is_wp_error( $revision_id ) ) {
Copy link

Choose a reason for hiding this comment

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

Nice, I like that reordering here. Good catch.

}
return false;

return $taxonomy_obj && ! empty( $taxonomy_obj->show_in_rest );
Copy link

Choose a reason for hiding this comment

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

While this is absolutely correct, the old one was easier to understand in my eyes. Not absolutely convinced here, but not against it either.

Maybe it just feels wrong when seeing them side by side.

Comment on lines +697 to +700
if ( ( 'network-active' === $current_status || 'network-active' === $new_status )
&& is_multisite()
&& ! current_user_can( 'manage_network_plugins' )
) {
Copy link
Member

Choose a reason for hiding this comment

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

Suggested change
if ( ( 'network-active' === $current_status || 'network-active' === $new_status )
&& is_multisite()
&& ! current_user_can( 'manage_network_plugins' )
) {
if (
( 'network-active' === $current_status || 'network-active' === $new_status ) &&
is_multisite() &&
! current_user_can( 'manage_network_plugins' )
) {

Copy link

Choose a reason for hiding this comment

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

Similar to my finding, good catch!

anton-vlasenko and others added 4 commits November 21, 2024 18:32
Co-authored-by: Christoph Daum <c.daum@me.com>
This reverts commit de1dc28.
This reverts commit 3528076.
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.

3 participants