Make WordPress Core

Changeset 54276


Ignore:
Timestamp:
09/21/2022 01:55:25 PM (2 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.

Location:
trunk
Files:
1 added
4 edited

Legend:

Unmodified
Added
Removed
  • trunk/Gruntfile.js

    r53688 r54276  
    44var webpackConfig = require( './webpack.config' );
    55var installChanged = require( 'install-changed' );
     6var json2php = require( 'json2php' );
    67
    78module.exports = function(grunt) {
     
    14041405    } );
    14051406
     1407    grunt.registerTask( 'copy:block-json', 'Copies block.json file contents to block-json.php.', function() {
     1408        var blocks = {};
     1409        grunt.file.recurse( SOURCE_DIR + 'wp-includes/blocks', function( abspath, rootdir, subdir, filename ) {
     1410            if ( /^block\.json$/.test( filename ) ) {
     1411                blocks[ subdir ] = grunt.file.readJSON( abspath );
     1412            }
     1413        } );
     1414        grunt.file.write(
     1415            SOURCE_DIR + 'wp-includes/blocks/blocks-json.php',
     1416            '<?php return ' + json2php( blocks ) + ';'
     1417        );
     1418    } );
     1419
    14061420    grunt.registerTask( 'copy:js', [
    14071421        'copy:npm-packages',
     
    14521466        'clean:files',
    14531467        'copy:files',
     1468        'copy:block-json',
    14541469        'copy:version',
    14551470    ] );
  • trunk/package-lock.json

    r54261 r54276  
    43274327                "json2php": "^0.0.4",
    43284328                "webpack-sources": "^3.2.2"
     4329            },
     4330            "dependencies": {
     4331                "json2php": {
     4332                    "version": "0.0.4",
     4333                    "resolved": "https://registry.npmjs.org/json2php/-/json2php-0.0.4.tgz",
     4334                    "integrity": "sha512-hFzejhs28f70sGnutcsRS459MnAsjRVI85RgPAL1KQIZEpjiDitc27CZv4IgOtaR86vrqOVlu9vJNew2XyTH4g==",
     4335                    "dev": true
     4336                }
    43294337            }
    43304338        },
     
    1776417772        },
    1776517773        "json2php": {
    17766             "version": "0.0.4",
    17767             "resolved": "https://registry.npmjs.org/json2php/-/json2php-0.0.4.tgz",
    17768             "integrity": "sha512-hFzejhs28f70sGnutcsRS459MnAsjRVI85RgPAL1KQIZEpjiDitc27CZv4IgOtaR86vrqOVlu9vJNew2XyTH4g==",
    17769             "dev": true
     17774            "version": "0.0.5",
     17775            "resolved": "https://registry.npmjs.org/json2php/-/json2php-0.0.5.tgz",
     17776            "integrity": "sha512-jWpsGAYlQDKOjJcyq3rYaxcZ+5YMhZIKHKTjdIKJPI9zLSX+yRWHSSwtV8hvIg7YMhbKkgPO669Ve2ZgFK5C7w=="
    1777017777        },
    1777117778        "json5": {
  • trunk/package.json

    r54257 r54276  
    144144        "jquery-form": "4.3.0",
    145145        "jquery-hoverintent": "1.10.2",
     146        "json2php": "^0.0.5",
    146147        "lodash": "4.17.21",
    147148        "masonry-layout": "4.2.2",
  • 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.