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

Blocks: Try parse caching #66373

Draft
wants to merge 2 commits into
base: trunk
Choose a base branch
from
Draft
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
27 changes: 20 additions & 7 deletions packages/blocks/src/api/parser/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -286,6 +286,8 @@ export function parseRawBlock( rawBlock, options ) {
return updatedBlock;
}

const parseCache = new Map();

/**
* Utilizes an optimized token-driven parser based on the Gutenberg grammar spec
* defined through a parsing expression grammar to take advantage of the regular
Expand All @@ -308,11 +310,22 @@ export function parseRawBlock( rawBlock, options ) {
* @return {Array} Block list.
*/
export default function parse( content, options ) {
return grammarParse( content ).reduce( ( accumulator, rawBlock ) => {
const block = parseRawBlock( rawBlock, options );
if ( block ) {
accumulator.push( block );
}
return accumulator;
}, [] );
const cacheKey = JSON.stringify( { content, options } );
if ( parseCache.has( content ) ) {
return parseCache.get( cacheKey );
}

const parsed = grammarParse( content ).reduce(
( accumulator, rawBlock ) => {
const block = parseRawBlock( rawBlock, options );
if ( block ) {
accumulator.push( block );
}
return accumulator;
},
[]
);
parseCache.set( cacheKey, parsed );

return parsed;
}
Loading