-
Notifications
You must be signed in to change notification settings - Fork 4.3k
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
Blocks: Try parse caching #66373
base: trunk
Are you sure you want to change the base?
Blocks: Try parse caching #66373
Conversation
Size Change: +46 B (0%) Total Size: 1.81 MB
ℹ️ View Unchanged
|
The following accounts have interacted with this PR and/or linked issues. I will continue to update these lists as activity occurs. You can also manually ask me to refresh this list by adding the If you're merging code through a pull request on GitHub, copy and paste the following into the bottom of the merge commit message.
To understand the WordPress project's expectations around crediting contributors, please review the Contributor Attribution page in the Core Handbook. |
Quick impressions: I don't think this is wise. In no particular order of importance:
Do I think it's worth looking at instances of superfluous block parsing in Gutenberg? Yes. Some thoughts:
|
Share the same ideas/concerns as @mcsf In addition to that, I'll say that in my experience, parsing has been an issue in two places:
Feels like there's potentially a universal solution in adding "virtual/transient" lazy and cached properties to entities. |
Thank you for the feedback, folks! It was quite clear that it won't be that simple. I will consider some alternatives based on what you suggested. I'll keep the PR open for now, since it helps with evaluating the impact |
Lately I’ve been trying hard to unify efforts to improve parsing performance for these kind of “inspection” related operations around moving towards a low-overhead streaming parser. WordPress/wordpress-develop#6760 This is in PHP but a JavaScript port is trivial. The new streaming parser is lazy and can stop parsing whenever the desired question is answered, such as “is there a So while I also applaud the efforts to improve the full block tree parser, I encourage folks working on parsing related problems to consider if the greater wins are available outside of the current profiling metrics: can we go faster by doing less (streaming and avoiding the construction of a tree) rather than by adding more (caching and complexity)? |
What?
This PR attempts to cache the block parser function.
Why?
Initially, the experimentation with React Compiler in #66361 brought me here. I was getting a bunch of errors, which seemed to be occurring while parsing blocks.
Then I realized, if we
parse()
10 times a simple string like this:we'll always get 10 different arrays, with 10 different paragraph
WPBlock
objects. That's probably fine at first glance, but given that those blocks later influence the entire editor and cause re-rendering virtually everything (different client IDs in practice means different blocks, even if all block properties and attributes are the same). At a large scale, it might actually influence the editor performance. So, wouldn't it make sense if the same input always produces the same output (at least in a single user session)?Disclaimer: this may be a very naive conclusion: I'm not that familiar with the block parser, and I'm only trying out things. Ideally, assume that I'm missing something.
How?
To get the
parse()
function to be more predictable, I'm introducing some naive caching toparse()
. It's quite straightforward, because the input is a string, and an object of options. I'm building a predictable string key out of these 2 arguments and using that key for caching theparse()
output.It was interesting that once I introduced the caching, I saw the errors reported from React Compiler drop from 65 to 7!
This appears to be because we're calling
parse()
in some functions that are memoized, and with React Compiler,useMemo()
doesn't seem to like functions that return different output on different calls.Testing Instructions
Testing Instructions for Keyboard
Same
Screenshots or screencast