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

Improve PHP performance for blocks #37611

Closed
aristath opened this issue Dec 23, 2021 · 3 comments
Closed

Improve PHP performance for blocks #37611

aristath opened this issue Dec 23, 2021 · 3 comments
Labels
[Feature] Blocks Overall functionality of blocks [Type] Performance Related to performance efforts

Comments

@aristath
Copy link
Member

aristath commented Dec 23, 2021

The block.json convention is extremely convenient, however it does add a few extra steps when core blocks get instantiated:

  • We go through folders and look for block.json files
  • When we locate a file, we read its contents
  • json_decode the file contents
  • Register the block using the decoded data

All these things happen on each page load. We could improve the performance and save some processing for all sites by doing one of 2 things:

  • Cache the data for blocks
  • Add an extra task in our compilers, saving the decoded JSON data as a PHP array

Caching the data can be a little complicated because we'd still need to check the JSON file on each page-load to make sure there have been no changes to the file (maybe using a filemtime call - which would still be more effective than reading the file contents & decoding it). Caching would also require a DB call - which would still be better than the current processes that run, but not as optimal as the alternative below.
Adding an extra task and compile the decoded JSON data as a PHP array seems like a better choice. It would be a lot easier to parse a native PHP array on each page-load, and we can also take care of development tasks etc.
We already use the json2php package in Gutenberg, so adding a new task for this should not be a difficult task.

@aristath aristath added [Feature] Blocks Overall functionality of blocks [Type] Performance Related to performance efforts labels Dec 23, 2021
@gziolo
Copy link
Member

gziolo commented Dec 23, 2021

It sounds like a similar improvement to what we did in WordPress core for the script asset files. We changed the output format from JSON to PHP and included a logic that combines them all into a single file:

https://github.com/WordPress/wordpress-develop/blob/trunk/src/wp-includes/assets/script-loader-packages.php

You can check the changes in this commit: WordPress/wordpress-develop@b2d243f. Related Trac ticket: https://core.trac.wordpress.org/ticket/48154.

I haven't done any more profound analysis, but it seems like changing the build process to convert block.json files into a single combined PHP file with everything bundled together might work great for WordPress core. We might also consider it for the Gutenberg plugin since we already do code transformations for PHP when present for core blocks.

@aristath
Copy link
Member Author

These changes will need to be done in WP-Core and not Gutenberg, so I created a ticket on track and I'm closing this one.

@aristath
Copy link
Member Author

PR submitted in core: WordPress/wordpress-develop#2252

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
[Feature] Blocks Overall functionality of blocks [Type] Performance Related to performance efforts
Projects
None yet
Development

No branches or pull requests

2 participants