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

Optimize compute_style_properties by reducing the number of iterations it performs #51983

Closed
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
69 changes: 67 additions & 2 deletions lib/class-wp-theme-json-gutenberg.php
Original file line number Diff line number Diff line change
Expand Up @@ -1864,6 +1864,50 @@ protected static function flatten_tree( $tree, $prefix = '', $token = '--' ) {
return $result;
}

/**
* Returns an array containing the flattened paths of the given node.
*
* Input:
*
* array(
* 'color' => array(
* 'background' => 'value',
* 'text' => 'value',
* ),
* 'typography' => array(
* 'size' => 'value',
* ),
* )
*
* Output:
*
* array(
* array( 'color', 'background' ),
* array( 'color', 'text' ),
* array( 'typography', 'size'),
* )
*
* @param array $node Style node to process.
* @return array
*/
private static function get_paths_in_node( $node ) {
$paths = array();
foreach ( $node as $key => $value ) {
if ( is_array( $value ) ) {
$subpaths = static::get_paths_in_node( $value );
foreach ( $subpaths as $subpath ) {
if ( ! is_array( $subpath ) ) {
$subpath = array( $subpath );
}
$paths[] = array_merge( array( $key ), $subpath );
}
} else {
$paths[] = $key;
}
}
return $paths;
}

/**
* Given a styles array, it extracts the style properties
* and adds them to the $declarations array following the format:
Expand Down Expand Up @@ -1897,9 +1941,30 @@ protected static function compute_style_properties( $styles, $settings = array()
return $declarations;
}

$root_variable_duplicates = array();
/*
* Instead of iterating over the $property list (54 items as of this comment),
* we iterate only over the properties present in the $styles array.
*
* To gauge the impact this change has, let's take a look at the numbers for
* the TwentyTwentyThree theme at the time of this comment.
*
* - The compute_style_properties method is called 65 times, one per style node.
* - The $properties list has 54 items. For TT3, $styles may have less than 5 items per node.
*
* - Before: 65 * 54 = 3510 times.
* - After: 65 * 5 = 325 times.
*
*/
$paths_in_node = self::get_paths_in_node( $styles );
$properties_to_inspect = array();
foreach ( $properties as $key => $value ) {
if ( in_array( $value, $paths_in_node, true ) ) {
$properties_to_inspect[ $key ] = $value;
}
}

foreach ( $properties as $css_property => $value_path ) {
$root_variable_duplicates = array();
foreach ( $properties_to_inspect as $css_property => $value_path ) {
$value = static::get_property_value( $styles, $value_path, $theme_json );

if ( str_starts_with( $css_property, '--wp--style--root--' ) && ( static::ROOT_BLOCK_SELECTOR !== $selector || ! $use_root_padding ) ) {
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 ( str_starts_with( $css_property, '--wp--style--root--' ) && ( static::ROOT_BLOCK_SELECTOR !== $selector || ! $use_root_padding ) ) {
if ( ( static::ROOT_BLOCK_SELECTOR !== $selector || ! $use_root_padding ) && str_starts_with( $css_property, '--wp--style--root--' ) ) {

Changing this line also helps performance.

Expand Down