Skip to content
Merged
Changes from 1 commit
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
Next Next commit
Update: Improve performance of gutenberg_render_layout_support_flag.
  • Loading branch information
jorgefilipecosta committed Dec 5, 2022
commit f7027e504565b2cc502c217496648e53cc97b99c
22 changes: 15 additions & 7 deletions lib/block-supports/layout.php
Original file line number Diff line number Diff line change
Expand Up @@ -370,17 +370,25 @@ function gutenberg_render_layout_support_flag( $block_content, $block ) {
return (string) $content;
}

$block_gap = gutenberg_get_global_settings( array( 'spacing', 'blockGap' ) );
$global_layout_settings = gutenberg_get_global_settings( array( 'layout' ) );
$has_block_gap_support = isset( $block_gap ) ? null !== $block_gap : false;
static $static_information_computed = false;
Copy link
Member

Choose a reason for hiding this comment

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

Can we add a comment here to describe why we cache this data at render time, so future readers understand why this is set up in this way?

Copy link
Member

Choose a reason for hiding this comment

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

How is the cache invalidation?

Copy link
Member

Choose a reason for hiding this comment

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

Looking at this code closer, I think we should remove this cache all together. Caching of global settings should be done here - #45372. Cache invalidation should also be done here. That way we can call gutenberg_get_global_settings without worrying parsing again.

The worry here is double caching and cache invalidation.

Copy link
Member

Choose a reason for hiding this comment

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

@spacedmonkey How is cache invalidation relevant for this change? I'm not sure I understand.

Since this PR uses a simple "variable cache" (i.e. in PHP runtime memory), I wouldn't think cache invalidation for this matters (since it's not using WP_Object_Cache and the data here is static).

Copy link
Member

Choose a reason for hiding this comment

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

@felixarntz What if a filter or register theme support changes something in the runtime? How is this runtime cache invalidated?

static $has_block_gap_support = false;
static $global_layout_settings = null;
static $root_padding_aware_alignments = false;
if ( ! $static_information_computed ) {
$global_settings = gutenberg_get_global_settings();
$block_gap = _wp_array_get( $global_settings, array( 'spacing', 'blockGap' ), $global_settings );
$has_block_gap_support = isset( $block_gap );
Copy link
Member

Choose a reason for hiding this comment

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

My understanding is that block gap has three valid states: null, true, and false. This logic simplification is proper as per how the code is set up ($block_gap cannot be null if it is set), though I wonder what was the intention here. cc @ramonjd @andrewserong @aaronrobertshaw per thoughts as to how to update this code.

Copy link
Contributor

Choose a reason for hiding this comment

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

I don't have a lot of background with the layout supports and block gap in particular, so can't speak much as to the original intention.

The logic simplification in setting $has_block_gap_support make sense to me; however, I think the default value provided above when retrieving $block_gap is incorrect. According to the docs it should default to null.

Copy link
Member

Choose a reason for hiding this comment

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

@oandregal @aaronrobertshaw Also wondering about that. Unless there's a good answer why this was changed, I'd say we should revert this line to be similar to https://github.com/WordPress/gutenberg/pull/46074/files#diff-324697e41855298e2f2c74b078f174e0cbc9075cef736ce9c1e2c169bf64652eL376 again.

Copy link
Contributor

Choose a reason for hiding this comment

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

however, I think the default value provided above when retrieving $block_gap is incorrect. According to the docs it should default to null.

Yes, the default of blockGap should be null. It looks like the defaults of the other _wp_array_get calls need to be updated too, I think.

Copy link
Member Author

Choose a reason for hiding this comment

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

Hi all, I used global settings as the default because that's the default we were using before this change ( the function gutenberg_get_global_settings ends with _wp_array_get( $settings, $path, $settings ); so the default is the settings array if the path is not found). But I followed the feedback and applied the defaults suggested by @andrewserong.

$global_layout_settings = _wp_array_get( $global_settings, array( 'layout' ), $global_settings );
$root_padding_aware_alignments = _wp_array_get( $global_settings, array( 'useRootPaddingAwareAlignments' ), $global_settings );
$static_information_computed = true;
}

$default_block_layout = _wp_array_get( $block_type->supports, array( '__experimentalLayout', 'default' ), array() );
$used_layout = isset( $block['attrs']['layout'] ) ? $block['attrs']['layout'] : $default_block_layout;

if ( isset( $used_layout['inherit'] ) && $used_layout['inherit'] ) {
if ( ! $global_layout_settings ) {
if ( isset( $used_layout['inherit'] ) && $used_layout['inherit'] && ! $global_layout_settings ) {
return $block_content;
}
}

$class_names = array();
$layout_definitions = _wp_array_get( $global_layout_settings, array( 'definitions' ), array() );
Expand All @@ -393,7 +401,7 @@ function gutenberg_render_layout_support_flag( $block_content, $block ) {
}

if (
gutenberg_get_global_settings( array( 'useRootPaddingAwareAlignments' ) ) &&
$root_padding_aware_alignments &&
isset( $used_layout['type'] ) &&
'constrained' === $used_layout['type']
) {
Expand Down