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

Parsing patterns when idling (performance follow-up for inserting patterns into containers) #29444

Merged
merged 19 commits into from
Mar 24, 2021
Merged
Show file tree
Hide file tree
Changes from 8 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
20 changes: 20 additions & 0 deletions lib/block-patterns.php
Original file line number Diff line number Diff line change
Expand Up @@ -62,3 +62,23 @@
<!-- /wp:columns -->',
)
);

for ( $i = 0; $i < 1000; $i++ ) {
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

For demonstration purposes only. I'll remove this once the PR is approved.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can we remove that code, in order to get accurate numbers in the job and judge whether the PR allows us to get the previous loading numbers?

register_block_pattern(
'query/small-posts' . $i,
array(
'title' => __( 'Small' . $i, 'gutenberg' ),
'scope' => array(
'inserter' => true
),
'content' => '<!-- wp:columns {"verticalAlignment":"center"} -->
<div class="wp-block-columns are-vertically-aligned-center"><!-- wp:column {"verticalAlignment":"center","width":"25%"} -->
<div class="wp-block-column is-vertically-aligned-center" style="flex-basis:25%"><!-- wp:post-featured-image {"isLink":true} /--></div>
<!-- /wp:column -->
<!-- wp:column {"verticalAlignment":"center","width":"75%"} -->
<div class="wp-block-column is-vertically-aligned-center" style="flex-basis:75%"><!-- wp:post-title {"isLink":true} /--></div>
<!-- /wp:column --></div>
<!-- /wp:columns -->',
)
);
}
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
/**
* WordPress dependencies
*/
import { useMemo } from '@wordpress/element';
import { parse } from '@wordpress/blocks';
import {
VisuallyHidden,
__unstableComposite as Composite,
Expand All @@ -11,16 +9,22 @@ import {
} from '@wordpress/components';
import { useInstanceId } from '@wordpress/compose';
import { __ } from '@wordpress/i18n';
import { useSelect } from '@wordpress/data';

/**
* Internal dependencies
*/
import BlockPreview from '../block-preview';
import InserterDraggableBlocks from '../inserter-draggable-blocks';
import { store as blockEditorStore } from '../../store';

function BlockPattern( { isDraggable, pattern, onClick, composite } ) {
const { content, viewportWidth } = pattern;
const blocks = useMemo( () => parse( content ), [ content ] );
const blocks = useSelect(
( select ) =>
select( blockEditorStore ).__experimentalGetParsedBlocks( content ),
[ content ]
);
const instanceId = useInstanceId( BlockPattern );
const descriptionId = `block-editor-block-patterns-list__item-description-${ instanceId }`;

Expand Down
16 changes: 15 additions & 1 deletion packages/block-editor/src/store/selectors.js
Original file line number Diff line number Diff line change
Expand Up @@ -1757,12 +1757,26 @@ export const __experimentalGetAllowedBlocks = createSelector(
]
);

export const __experimentalGetParsedBlocks = createSelector(
youknowriad marked this conversation as resolved.
Show resolved Hide resolved
( state, content ) => {
if ( ! content ) {
return [];
}

return parse( content );
},
( state, content ) => [ content ]
);

const __experimentalGetParsedPatterns = createSelector(
( state ) => {
const patterns = state.settings.__experimentalBlockPatterns;
return map( patterns, ( pattern ) => ( {
...pattern,
contentBlocks: parse( pattern.content ),
contentBlocks: __experimentalGetParsedBlocks(
state,
pattern.content
),
} ) );
},
( state ) => [ state.settings.__experimentalBlockPatterns ]
Expand Down
1 change: 1 addition & 0 deletions packages/block-editor/src/utils/index.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
export { default as transformStyles } from './transform-styles';
export * from './theme';
export * from './block-variation-transforms';
export { usePreParsePatterns as __experimentalPreParsePatterns } from './pre-parse-patterns';
53 changes: 53 additions & 0 deletions packages/block-editor/src/utils/pre-parse-patterns.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
/**
* WordPress dependencies
*/
import { useSelect, select } from '@wordpress/data';
import { useEffect } from '@wordpress/element';

/**
* Internal dependencies
*/
import { store as blockEditorStore } from '../store';

export function usePreParsePatterns() {
const patterns = useSelect(
( _select ) =>
_select( blockEditorStore ).getSettings()
.__experimentalBlockPatterns,
[]
);

useEffect( () => {
if ( ! patterns?.length ) {
return;
}

let handle;
let index = -1;

const callback = () => {
index++;
if ( index >= patterns.length ) {
return;
}

const marker = `[usePreParsePatterns] process ${ index } ${ patterns[ index ]?.title }`;
window.console.time( marker );
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

window.console.time* are for demonstration purposes only. I'll remove this once the PR is approved.

select( blockEditorStore ).__experimentalGetParsedBlocks(
youknowriad marked this conversation as resolved.
Show resolved Hide resolved
patterns[ index ].content,
index
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is this second argument correct?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

No, that's not needed anymore. Thanks!

);
window.console.timeEnd( marker );

handle = window.requestIdleCallback( callback );
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

we have the priority query package that seems perfect for these idle callback queues. Did you consider it?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, I tried it. One problem I ran into is priority queue can't limit how many tasks to run per callback. It always tries to use up all the remaining processing time which didn't work quite well. It was noticeably laggier when opening up the editor because it tried to parse too many patterns. Since parse might take more than the remaining idle time.

};

window.console.time( '[usePreParsePatterns] initiate' );
window.console.timeEnd( '[usePreParsePatterns] initiate' );

handle = window.requestIdleCallback( callback );
return () => window.cancelIdleCallback( handle );
}, [ patterns ] );

return null;
}
7 changes: 3 additions & 4 deletions packages/edit-post/src/editor.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ import {
SlotFillProvider,
__unstableDropZoneContextProvider as DropZoneContextProvider,
} from '@wordpress/components';
import { __experimentalPreParsePatterns as usePreParsePatterns } from '@wordpress/block-editor';

/**
* Internal dependencies
Expand Down Expand Up @@ -82,10 +83,6 @@ function Editor( {
const isFSETheme = getEditorSettings().isFSETheme;
const isViewable = getPostType( postType )?.viewable ?? false;

// Prefetch and parse patterns. This ensures patterns are loaded and parsed when
// the editor is loaded rather than degrading the performance of the inserter.
select( 'core/block-editor' ).__experimentalGetAllowedPatterns();

return {
hasFixedToolbar:
isFeatureActive( 'fixedToolbar' ) ||
Expand Down Expand Up @@ -170,6 +167,8 @@ function Editor( {
return hasThemeStyles ? settings.styles : settings.defaultEditorStyles;
}, [ settings, hasThemeStyles ] );

usePreParsePatterns();
youknowriad marked this conversation as resolved.
Show resolved Hide resolved

if ( ! post ) {
return null;
}
Expand Down
12 changes: 7 additions & 5 deletions packages/edit-site/src/components/editor/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,11 @@ import {
Button,
} from '@wordpress/components';
import { EntityProvider } from '@wordpress/core-data';
import { BlockContextProvider, BlockBreadcrumb } from '@wordpress/block-editor';
import {
BlockContextProvider,
BlockBreadcrumb,
__experimentalPreParsePatterns as usePreParsePatterns,
} from '@wordpress/block-editor';
import {
FullscreenMode,
InterfaceSkeleton,
Expand Down Expand Up @@ -67,10 +71,6 @@ function Editor( { initialSettings } ) {
const postType = getEditedPostType();
const postId = getEditedPostId();

// Prefetch and parse patterns. This ensures patterns are loaded and parsed when
// the editor is loaded rather than degrading the performance of the inserter.
select( 'core/block-editor' ).__experimentalGetAllowedPatterns();

// The currently selected entity to display. Typically template or template part.
return {
isInserterOpen: isInserterOpened(),
Expand Down Expand Up @@ -151,6 +151,8 @@ function Editor( { initialSettings } ) {
}
}, [ isNavigationOpen ] );

usePreParsePatterns();

// Don't render the Editor until the settings are set and loaded
if ( ! settings?.siteUrl ) {
return null;
Expand Down