Make WordPress Core


Ignore:
Timestamp:
09/26/2023 01:45:23 PM (17 months ago)
Author:
SergeyBiryukov
Message:

Editor: Reduce the use of the _wp_array_get() function to improve performance.

_wp_array_get() is an expensive function, and it's called thousands of times on each page view on the front end. While the function performance was slightly improved in #58376, it is still called more times than it should be.

This commit aims to further optimize its usage:

  • In many cases, _wp_array_get() can be replaced with a much simpler and faster isset() check.
  • The isset() function is capable of checking nested arrays, so isset( $foo['a']['b']['c'] ) will return false even if $foo['a'] is unset, without throwing any errors or warnings.
  • When _wp_array_get() cannot be directly replaced with isset(), it would be good practice to wrap it in an isset() function so that _wp_array_get() only runs when it needs to.

Original PR from Gutenberg repository:

Follow-up to [55851], [56382].

Props aristath, jrf, spacedmonkey, mukesh27, swissspidy, hellofromTonya.
Fixes #59405.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/src/wp-includes/block-supports/spacing.php

    r56382 r56709  
    5959    }
    6060
    61     $skip_padding                    = wp_should_skip_block_supports_serialization( $block_type, 'spacing', 'padding' );
    62     $skip_margin                     = wp_should_skip_block_supports_serialization( $block_type, 'spacing', 'margin' );
    63     $spacing_block_styles            = array();
    64     $spacing_block_styles['padding'] = $has_padding_support && ! $skip_padding ? _wp_array_get( $block_styles, array( 'spacing', 'padding' ), null ) : null;
    65     $spacing_block_styles['margin']  = $has_margin_support && ! $skip_margin ? _wp_array_get( $block_styles, array( 'spacing', 'margin' ), null ) : null;
    66     $styles                          = wp_style_engine_get_styles( array( 'spacing' => $spacing_block_styles ) );
     61    $skip_padding         = wp_should_skip_block_supports_serialization( $block_type, 'spacing', 'padding' );
     62    $skip_margin          = wp_should_skip_block_supports_serialization( $block_type, 'spacing', 'margin' );
     63    $spacing_block_styles = array(
     64        'padding' => null,
     65        'margin'  => null,
     66    );
     67    if ( $has_padding_support && ! $skip_padding ) {
     68        $spacing_block_styles['padding'] = isset( $block_styles['spacing']['padding'] ) ? $block_styles['spacing']['padding'] : null;
     69    }
     70    if ( $has_margin_support && ! $skip_margin ) {
     71        $spacing_block_styles['margin'] = isset( $block_styles['spacing']['margin'] ) ? $block_styles['spacing']['margin'] : null;
     72    }
     73    $styles = wp_style_engine_get_styles( array( 'spacing' => $spacing_block_styles ) );
    6774
    6875    if ( ! empty( $styles['css'] ) ) {
Note: See TracChangeset for help on using the changeset viewer.