-
Notifications
You must be signed in to change notification settings - Fork 2.7k
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
base: trunk
Are you sure you want to change the base?
REST API: remove unncessary local variables and reorders the expressions in if
statements to optimize performance
#7708
Conversation
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().
Test using WordPress PlaygroundThe 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
For more details about these limitations and more, check out the Limitations page in the WordPress Playground documentation. |
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 Unlinked AccountsThe 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:
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'] ) ) { |
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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?
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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.
Have you check the performance number before/after? |
@mukeshpanchal27 I measured the performance improvement after you asked this question. |
@@ -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'] ) ) { |
There was a problem hiding this comment.
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 ) ) { |
There was a problem hiding this comment.
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 ); |
There was a problem hiding this comment.
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.
src/wp-includes/rest-api/endpoints/class-wp-rest-widgets-controller.php
Outdated
Show resolved
Hide resolved
if ( ( 'network-active' === $current_status || 'network-active' === $new_status ) | ||
&& is_multisite() | ||
&& ! current_user_can( 'manage_network_plugins' ) | ||
) { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
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' ) | |
) { |
There was a problem hiding this comment.
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!
if
statements to optimize performance;isset()
andunset()
statements;null
inWP_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.