Skip to content

Conversation

@kt-12
Copy link
Member

@kt-12 kt-12 commented Apr 30, 2024

What?

Introduce WP_Theme_JSON_Data_Gutenberg::get_theme_json() to avoid the cyclic call to WP_Theme_JSON_Gutenberg::__construct.

Trac ticket: Trac 61112

Why?

Doing similar changes in core, we got a consistent ~2.5% improvement by reducing 8 calls to the constructor reference core pr.

How?

WP_Theme_JSON_Data_Gutenberg internally has a theme_json private attribute which is an instance of WP_Theme_JSON_Gutenberg. At the moment we try to form the raw JSON data from the WP_Theme_JSON_Gutenberg object and then again from the Object from the raw JSON data, which leads to regression. In this PR we use the existing WP_Theme_JSON_Gutenberg object available to WP_Theme_JSON_Data_Gutenberg instead of the cyclic way.

Testing Instructions

Testing Instructions for Keyboard

Screenshots or screencast

@kt-12 kt-12 requested a review from spacedmonkey as a code owner April 30, 2024 21:05
@github-actions
Copy link

github-actions bot commented Apr 30, 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.

If you're merging code through a pull request on GitHub, copy and paste the following into the bottom of the merge commit message.

Co-authored-by: kt-12 <thekt12@git.wordpress.org>
Co-authored-by: oandregal <oandregal@git.wordpress.org>
Co-authored-by: joemcgill <joemcgill@git.wordpress.org>

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

@kt-12 kt-12 changed the title remove additional call to Remove additional call to WP_Theme_JSON_Gutenberg::__construct Apr 30, 2024
@joemcgill joemcgill added the [Type] Performance Related to performance efforts label Apr 30, 2024
Copy link
Member

@oandregal oandregal left a comment

Choose a reason for hiding this comment

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

See related feedback in the original wordpress-develop PR WordPress/wordpress-develop#6271

$config = $theme_json->get_data();
return new WP_Theme_JSON_Gutenberg( $config, 'custom' );

return $theme_json->get_theme_json();
Copy link
Member

Choose a reason for hiding this comment

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

A bit below, there's another WP_Theme_JSON_Data_Gutenberg call but we cannot update it as we do on this one. That code is different than the core code and we need to consolidate both. That's a follow-up PR that shouldn't prevent this one from landing.

Copy link
Member

Choose a reason for hiding this comment

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

Perhaps we can absorb $config['isGlobalStylesUserThemeJSON'] = true; as part of WP_Theme_JSON_Data_Gutenberg. We may want to still check that's true before returning to be extra-safe (line 553) but we wouldn't need a second transformation.

Copy link
Member Author

Choose a reason for hiding this comment

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

@oandregal I observed it but presumed that GB and Core might be doing things differently here. I'll look at how to incorporate that in a different PR.

Copy link
Member

Choose a reason for hiding this comment

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

Looks like the $config['isGlobalStylesUserThemeJSON'] modification was made as part of 2012a36 (#58409).

@ajlende is there a reason why this would still be necessary if we use the WP_Theme_JSON_Gutenberg object already present in the WP_Theme_JSON_Data_Gutenberg object?

Copy link
Contributor

Choose a reason for hiding this comment

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

The $config['isGlobalStylesUserThemeJSON'] is required for migrations that don't need to run for the custom origin in WP_Theme_JSON_Schema::migrate (called on new WP_Theme_JSON()). If the origin could be passed to migrate in some other way, then it would be fine.

/*
* Remaining changes do not need to be applied to the custom origin,
* as they should take on the value of the theme origin.
*/
if (
isset( $new['isGlobalStylesUserThemeJSON'] ) &&
true === $new['isGlobalStylesUserThemeJSON']
) {
return $new;
}

Copy link
Contributor

Choose a reason for hiding this comment

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

Added a draft PR to explain what I'm talking about #62305

Copy link
Member

Choose a reason for hiding this comment

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

Thanks @ajlende! I'll try to review this later this week.

Copy link
Member

Choose a reason for hiding this comment

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

Approved #62305 which improves the API and should bring back this change. Thanks for the follow-up, Alex!

Copy link
Member

@joemcgill joemcgill left a comment

Choose a reason for hiding this comment

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

This looks good to me. I think looking at the isGlobalStylesUserThemeJSON question can be resolved in a follow-up PR.

$config = $theme_json->get_data();
return new WP_Theme_JSON_Gutenberg( $config, 'custom' );

return $theme_json->get_theme_json();
Copy link
Member

Choose a reason for hiding this comment

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

Looks like the $config['isGlobalStylesUserThemeJSON'] modification was made as part of 2012a36 (#58409).

@ajlende is there a reason why this would still be necessary if we use the WP_Theme_JSON_Gutenberg object already present in the WP_Theme_JSON_Data_Gutenberg object?

@joemcgill
Copy link
Member

@oandregal I don't have permissions to merge this PR but am planning to commit the related Core PR this week. Can you help us get this one shipped in a GB release as well?

@adamsilverstein adamsilverstein merged commit 51775a1 into WordPress:trunk May 22, 2024
@github-actions github-actions bot added this to the Gutenberg 18.5 milestone May 22, 2024
@joemcgill
Copy link
Member

Thanks @adamsilverstein!

@joemcgill
Copy link
Member

This change was committed to WP trunk in 58185.

@oandregal
Copy link
Member

@oandregal I don't have permissions to merge this PR but am planning to commit WordPress/wordpress-develop#6271 this week. Can you help us get this one shipped in a GB release as well?

Sorry I wasn't around to help with this the past week (I was AFK) — happy I wasn't a blocker and you were able to fix it!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

[Type] Performance Related to performance efforts

Projects

None yet

Development

Successfully merging this pull request may close these issues.

5 participants