Make WordPress Core


Ignore:
Timestamp:
09/21/2022 01:55:25 PM (4 years ago)
Author:
SergeyBiryukov
Message:

Editor: Improve block loading PHP performance.

This commit improves PHP performance for core blocks by reading a single PHP file with block metadata, instead of reading a JSON file per-block and then decoding from JSON to PHP.

Includes:

  • Adding a new Grunt task to convert block.json files to block-json.php.
  • Using the new block-json.php file in the register_block_type_from_metadata() function.

Follow-up to [48141].

Props aristath, gziolo, johnbillion, presstoke, mukesh27, hellofromTonya, petitphp, adamsilverstein, costdev, desrosj, SergeyBiryukov.
Fixes #55005.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/src/wp-includes/blocks.php

    r54181 r54276  
    284284 */
    285285function register_block_type_from_metadata( $file_or_folder, $args = array() ) {
    286         $filename      = 'block.json';
    287         $metadata_file = ( substr( $file_or_folder, -strlen( $filename ) ) !== $filename ) ?
    288                 trailingslashit( $file_or_folder ) . $filename :
     286        /*
     287         * Get an array of metadata from a PHP file.
     288         * This improves performance for core blocks as it's only necessary to read a single PHP file
     289         * instead of reading a JSON file per-block, and then decoding from JSON to PHP.
     290         * Using a static variable ensures that the metadata is only read once per request.
     291         */
     292        static $core_blocks_meta;
     293        if ( ! $core_blocks_meta ) {
     294                $core_blocks_meta = include_once ABSPATH . WPINC . '/blocks/blocks-json.php';
     295        }
     296
     297        $metadata_file = ( ! str_ends_with( $file_or_folder, 'block.json' ) ) ?
     298                trailingslashit( $file_or_folder ) . 'block.json' :
    289299                $file_or_folder;
     300
    290301        if ( ! file_exists( $metadata_file ) ) {
    291302                return false;
    292303        }
    293304
    294         $metadata = wp_json_file_decode( $metadata_file, array( 'associative' => true ) );
     305        // Try to get metadata from the static cache for core blocks.
     306        $metadata = false;
     307        if ( str_starts_with( $file_or_folder, ABSPATH . WPINC ) ) {
     308                $core_block_name = str_replace( ABSPATH . WPINC . '/blocks/', '', $file_or_folder );
     309                if ( ! empty( $core_blocks_meta[ $core_block_name ] ) ) {
     310                        $metadata = $core_blocks_meta[ $core_block_name ];
     311                }
     312        }
     313
     314        // If metadata is not found in the static cache, read it from the file.
     315        if ( ! $metadata ) {
     316                $metadata = wp_json_file_decode( $metadata_file, array( 'associative' => true ) );
     317        }
     318
    295319        if ( ! is_array( $metadata ) || empty( $metadata['name'] ) ) {
    296320                return false;
Note: See TracChangeset for help on using the changeset viewer.