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

Performance improvement: Reduce use of _wp_array_get #5244

Closed
wants to merge 4 commits into from

Conversation

aristath
Copy link
Member

@aristath aristath commented Sep 19, 2023

This is a backport of WordPress/gutenberg#51116 from Gutenberg

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


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.

@mukeshpanchal27
Copy link
Member

@aristath thanks for the PR. Could you please share the performance number before after the changes?

@aristath
Copy link
Member Author

@aristath thanks for the PR. Could you please share the performance number before after the changes?

Trunk:
Screenshot 2023-09-21 at 10 56 07 AM

After this PR:
Screenshot 2023-09-21 at 10 54 13 AM

So on my test, we're saving 2582 call to the _wp_array_get() function, and 39ms.
These results are not accurate since XDebug does add a lot of overhead, but there is a clear improvement.

@swissspidy
Copy link
Member

The coding standards disallow using the null coalescing operator right now, see https://core.trac.wordpress.org/ticket/58874

@jrfnl
Copy link
Member

jrfnl commented Sep 21, 2023

The coding standards disallow using the null coalescing operator right now, see https://core.trac.wordpress.org/ticket/58874

The coding standards do no such thing: https://developer.wordpress.org/coding-standards/wordpress-coding-standards/php/

The coding standards have no opinion on whether you use it or not. It's a matter of the minimum PHP version.

As for that Trac ticket: that's about whether to do a bulk update of the Core code base for it (and possibly enforce its use), which is a different matter.

@aristath
Copy link
Member Author

aristath commented Sep 21, 2023

Thank you @swissspidy 👍
Technically it's not "disallowed", it's just not used yet. If it was disallowed, tests would be failing.

Do you think it would be better if I change these to be isset() checks instead in this PR?
Do you believe that would improve the readability of the code?

@swissspidy
Copy link
Member

swissspidy commented Sep 21, 2023

My bad, just glanced over it on mobile. Thanks for correcting me!

Copy link
Member

@spacedmonkey spacedmonkey left a comment

Choose a reason for hiding this comment

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

Other than the null operator question, this change looks good to me.

@spacedmonkey
Copy link
Member

As this feels like a code qualiaty thing, I am going to ping @SergeyBiryukov

@aristath
Copy link
Member Author

Rebased the PR, resolving conflicts 👍

$background_image_source = _wp_array_get( $block_attributes, array( 'style', 'background', 'backgroundImage', 'source' ), null );
$background_image_url = _wp_array_get( $block_attributes, array( 'style', 'background', 'backgroundImage', 'url' ), null );
$background_size = _wp_array_get( $block_attributes, array( 'style', 'background', 'backgroundSize' ), 'cover' );
$background_image_source = $block_attributes['style']['background']['backgroundImage']['source'] ?? null;
Copy link
Contributor

Choose a reason for hiding this comment

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

There is not yet consensus to use ?? in Core. See https://core.trac.wordpress.org/ticket/58874. IMHO I think this should not yet to be introduced until its Trac ticket has consensus to move forward. Then the work here can be updated as part of that ticket.

Copy link
Member Author

Choose a reason for hiding this comment

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

Thank you for the feedback @hellofromtonya!

That ticket is about converting all existing isset( $var ) ? $var : 'default' checks in Core to use ?? where appropriate. It has nothing to do with this PR - which removes unnecessary calls to _wp_array_get. It should not be a part of that ticket... the fact that both PRs use ?? is conincidental. We could convert this PR to use isset() checks and it would be just as valuable and valid as it is now.

The fact that we don't yet use ?? anywhere in Core does not mean that it's not allowed, or that it should not be used. When WordPress had to support PHP 5.6 we could not use it because we had to be compatible with that PHP version. However, "could not use" is not the same as "WordPress does not allow".

The ??operator is a part of the PHP language - just like == or &&. The only difference is the supported PHP versions, something which is no longer blocking us.

@aristath
Copy link
Member Author

@hellofromtonya if the use of ?? is considered a blocker in this PR, I can simply change it to isset() checks.
The performance improvements from this PR are the same regardless of which syntax we use, and the null coalescing operator is not the main point of this PR.

Would that help us move this ticket forward? We can convert isset() checks to ?? at a later date if we want.

@aristath
Copy link
Member Author

Thank you all for the feedback!

I pushed a commit, changing the syntax so it doesn't use ?? anymore, and it now uses isset() checks. 👍

@SergeyBiryukov
Copy link
Member

Thanks for the PR! Merged in r56709.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
No open projects
Status: Done
Development

Successfully merging this pull request may close these issues.

7 participants