-
Notifications
You must be signed in to change notification settings - Fork 4.2k
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
Unmemoize Block component selectors #58355
Changes from 2 commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -6,7 +6,13 @@ import classnames from 'classnames'; | |
/** | ||
* WordPress dependencies | ||
*/ | ||
import { memo, useCallback, RawHTML, useContext } from '@wordpress/element'; | ||
import { | ||
memo, | ||
useCallback, | ||
RawHTML, | ||
useContext, | ||
useMemo, | ||
} from '@wordpress/element'; | ||
import { | ||
getBlockType, | ||
getSaveContent, | ||
|
@@ -497,7 +503,8 @@ function BlockListBlockProvider( props ) { | |
getBlockMode, | ||
isSelectionEnabled, | ||
getTemplateLock, | ||
__unstableGetBlockWithoutInnerBlocks, | ||
getBlockWithoutAttributes, | ||
getBlockAttributes, | ||
canRemoveBlock, | ||
canMoveBlock, | ||
|
||
|
@@ -506,7 +513,7 @@ function BlockListBlockProvider( props ) { | |
getBlockEditingMode, | ||
getBlockName, | ||
isFirstMultiSelectedBlock, | ||
getMultiSelectedBlockClientIds, | ||
getSelectedBlockClientIdsUnmemoized, | ||
hasSelectedInnerBlock, | ||
|
||
getBlockIndex, | ||
|
@@ -524,13 +531,14 @@ function BlockListBlockProvider( props ) { | |
__unstableGetEditorMode, | ||
getSelectedBlocksInitialCaretPosition, | ||
} = unlock( select( blockEditorStore ) ); | ||
const block = __unstableGetBlockWithoutInnerBlocks( clientId ); | ||
const blockWithoutAttributes = | ||
getBlockWithoutAttributes( clientId ); | ||
|
||
// This is a temporary fix. | ||
// This function should never be called when a block is not | ||
// present in the state. It happens now because the order in | ||
// withSelect rendering is not correct. | ||
if ( ! block ) { | ||
if ( ! blockWithoutAttributes ) { | ||
return; | ||
} | ||
|
||
|
@@ -542,7 +550,8 @@ function BlockListBlockProvider( props ) { | |
const templateLock = getTemplateLock( rootClientId ); | ||
const canRemove = canRemoveBlock( clientId, rootClientId ); | ||
const canMove = canMoveBlock( clientId, rootClientId ); | ||
const { name: blockName, attributes, isValid } = block; | ||
const attributes = getBlockAttributes( clientId ); | ||
const { name: blockName, isValid } = blockWithoutAttributes; | ||
const blockType = getBlockType( blockName ); | ||
const match = getActiveBlockVariation( blockName, attributes ); | ||
const { outlineMode, supportsLayout } = getSettings(); | ||
|
@@ -562,11 +571,7 @@ function BlockListBlockProvider( props ) { | |
isLocked: !! templateLock, | ||
canRemove, | ||
canMove, | ||
// Users of the editor.BlockListBlock filter used to be able to | ||
// access the block prop. | ||
// Ideally these blocks would rely on the clientId prop only. | ||
// This is kept for backward compatibility reasons. | ||
block, | ||
blockWithoutAttributes, | ||
name: blockName, | ||
attributes, | ||
isValid, | ||
|
@@ -578,7 +583,7 @@ function BlockListBlockProvider( props ) { | |
mayDisplayControls: | ||
_isSelected || | ||
( isFirstMultiSelectedBlock( clientId ) && | ||
getMultiSelectedBlockClientIds().every( | ||
getSelectedBlockClientIdsUnmemoized().every( | ||
( id ) => getBlockName( id ) === blockName | ||
) ), | ||
mayDisplayParentControls: | ||
|
@@ -634,7 +639,7 @@ function BlockListBlockProvider( props ) { | |
isLocked, | ||
canRemove, | ||
canMove, | ||
block, | ||
blockWithoutAttributes, | ||
name, | ||
attributes, | ||
isValid, | ||
|
@@ -665,6 +670,15 @@ function BlockListBlockProvider( props ) { | |
defaultClassName, | ||
} = selectedProps; | ||
|
||
// Users of the editor.BlockListBlock filter used to be able to | ||
// access the block prop. | ||
// Ideally these blocks would rely on the clientId prop only. | ||
// This is kept for backward compatibility reasons. | ||
const block = useMemo( | ||
() => ( { ...blockWithoutAttributes, attributes } ), | ||
[ blockWithoutAttributes, attributes ] | ||
); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The reason this is better than selector cache is because it's stored by component instance, not globally. |
||
|
||
// Block is sometimes not mounted at the right time, causing it be | ||
// undefined see issue for more info | ||
// https://github.com/WordPress/gutenberg/issues/17013 | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -23,6 +23,8 @@ import { INSERTER_PATTERN_TYPES } from '../components/inserter/block-patterns-ta | |
import { store } from './'; | ||
import { unlock } from '../lock-unlock'; | ||
|
||
export { getSelectedBlockClientIdsUnmemoized } from './utils'; | ||
|
||
/** | ||
* Returns true if the block interface is hidden, or false otherwise. | ||
* | ||
|
@@ -44,6 +46,10 @@ export function getLastInsertedBlocksClientIds( state ) { | |
return state?.lastBlockInserted?.clientIds; | ||
} | ||
|
||
export function getBlockWithoutAttributes( state, clientId ) { | ||
return state.blocks.byClientId.get( clientId ); | ||
} | ||
|
||
/** | ||
* Returns true if all of the descendants of a block with the given client ID | ||
* have an editing mode of 'disabled', or false otherwise. | ||
|
@@ -53,25 +59,17 @@ export function getLastInsertedBlocksClientIds( state ) { | |
* | ||
* @return {boolean} Whether the block descendants are disabled. | ||
*/ | ||
export const isBlockSubtreeDisabled = createSelector( | ||
( state, clientId ) => { | ||
const isChildSubtreeDisabled = ( childClientId ) => { | ||
return ( | ||
getBlockEditingMode( state, childClientId ) === 'disabled' && | ||
getBlockOrder( state, childClientId ).every( | ||
isChildSubtreeDisabled | ||
) | ||
); | ||
}; | ||
return getBlockOrder( state, clientId ).every( isChildSubtreeDisabled ); | ||
}, | ||
( state ) => [ | ||
state.blocks.parents, | ||
state.blocks.order, | ||
state.blockEditingModes, | ||
state.blockListSettings, | ||
] | ||
); | ||
export const isBlockSubtreeDisabled = ( state, clientId ) => { | ||
const isChildSubtreeDisabled = ( childClientId ) => { | ||
return ( | ||
getBlockEditingMode( state, childClientId ) === 'disabled' && | ||
getBlockOrder( state, childClientId ).every( | ||
isChildSubtreeDisabled | ||
) | ||
); | ||
}; | ||
return getBlockOrder( state, clientId ).every( isChildSubtreeDisabled ); | ||
}; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. After #58349, this is no longer called, so the performance improvement of this PR will be lessened. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Probably still worth to remove the memoization I think... Also the dependencies of this selector weren't fully correct. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Also, this selector should also check the editing mode of the top-level If I look at the only call in |
||
|
||
/** | ||
* Returns a tree of block objects with only clientID and innerBlocks set. | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -30,6 +30,7 @@ import { | |
checkAllowListRecursive, | ||
checkAllowList, | ||
getAllPatternsDependants, | ||
getSelectedBlockClientIdsUnmemoized, | ||
} from './utils'; | ||
import { orderBy } from '../utils/sorting'; | ||
import { STORE_NAME } from './constants'; | ||
|
@@ -757,39 +758,7 @@ export function getSelectedBlocksInitialCaretPosition( state ) { | |
* @return {Array} Multi-selected block client IDs. | ||
*/ | ||
export const getSelectedBlockClientIds = createSelector( | ||
( state ) => { | ||
const { selectionStart, selectionEnd } = state.selection; | ||
|
||
if ( ! selectionStart.clientId || ! selectionEnd.clientId ) { | ||
return EMPTY_ARRAY; | ||
} | ||
|
||
if ( selectionStart.clientId === selectionEnd.clientId ) { | ||
return [ selectionStart.clientId ]; | ||
} | ||
|
||
// Retrieve root client ID to aid in retrieving relevant nested block | ||
// order, being careful to allow the falsey empty string top-level root | ||
// by explicitly testing against null. | ||
const rootClientId = getBlockRootClientId( | ||
state, | ||
selectionStart.clientId | ||
); | ||
|
||
if ( rootClientId === null ) { | ||
return EMPTY_ARRAY; | ||
} | ||
|
||
const blockOrder = getBlockOrder( state, rootClientId ); | ||
const startIndex = blockOrder.indexOf( selectionStart.clientId ); | ||
const endIndex = blockOrder.indexOf( selectionEnd.clientId ); | ||
|
||
if ( startIndex > endIndex ) { | ||
return blockOrder.slice( endIndex, startIndex + 1 ); | ||
} | ||
|
||
return blockOrder.slice( startIndex, endIndex + 1 ); | ||
}, | ||
getSelectedBlockClientIdsUnmemoized, | ||
( state ) => [ | ||
state.blocks.order, | ||
state.selection.selectionStart.clientId, | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I don't fully understand why unmemoizing
This would be true if the selector had a
To summarize, there is just one three-level So, I think that unmemoizing this particular selector is controversial: it returns an array that deserves to have stable references, and I don't see where the speedup would come from. For other selectors that return boolean values, I think unmemoization makes sense. Especially the ones that have arguments. It it plausible that recalculating the result from scratch is faster than maintaining large memoization maps. One other such selector that comes to mind is There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Yes, that's true |
||
|
@@ -852,7 +821,10 @@ export const getMultiSelectedBlocks = createSelector( | |
* @return {?string} First block client ID in the multi-selection set. | ||
*/ | ||
export function getFirstMultiSelectedBlockClientId( state ) { | ||
return getMultiSelectedBlockClientIds( state )[ 0 ] || null; | ||
if ( ! hasMultiSelection( state ) ) { | ||
return null; | ||
} | ||
return getSelectedBlockClientIdsUnmemoized( state )[ 0 ]; | ||
} | ||
|
||
/** | ||
|
@@ -864,8 +836,11 @@ export function getFirstMultiSelectedBlockClientId( state ) { | |
* @return {?string} Last block client ID in the multi-selection set. | ||
*/ | ||
export function getLastMultiSelectedBlockClientId( state ) { | ||
const selectedClientIds = getMultiSelectedBlockClientIds( state ); | ||
return selectedClientIds[ selectedClientIds.length - 1 ] || null; | ||
if ( ! hasMultiSelection( state ) ) { | ||
return null; | ||
} | ||
const selectedClientIds = getSelectedBlockClientIdsUnmemoized( state ); | ||
return selectedClientIds[ selectedClientIds.length - 1 ]; | ||
} | ||
|
||
/** | ||
|
@@ -892,7 +867,12 @@ export function isFirstMultiSelectedBlock( state, clientId ) { | |
* @return {boolean} Whether block is in multi-selection set. | ||
*/ | ||
export function isBlockMultiSelected( state, clientId ) { | ||
return getMultiSelectedBlockClientIds( state ).indexOf( clientId ) !== -1; | ||
if ( isBlockSelected( state, clientId ) ) { | ||
return false; | ||
} | ||
return ( | ||
getSelectedBlockClientIdsUnmemoized( state ).indexOf( clientId ) !== -1 | ||
); | ||
} | ||
|
||
/** | ||
|
@@ -1004,7 +984,7 @@ export function __unstableIsSelectionCollapsed( state ) { | |
} | ||
|
||
export function __unstableSelectionHasUnmergeableBlock( state ) { | ||
return getSelectedBlockClientIds( state ).some( ( clientId ) => { | ||
return getSelectedBlockClientIdsUnmemoized( state ).some( ( clientId ) => { | ||
const blockName = getBlockName( state, clientId ); | ||
const blockType = getBlockType( blockName ); | ||
return ! blockType.merge; | ||
|
@@ -2938,7 +2918,7 @@ export const isGroupable = createRegistrySelector( | |
const groupingBlockName = getGroupingBlockName(); | ||
const _clientIds = clientIds?.length | ||
? clientIds | ||
: getSelectedBlockClientIds( state ); | ||
: getSelectedBlockClientIdsUnmemoized( state ); | ||
const rootClientId = _clientIds?.length | ||
? getBlockRootClientId( state, _clientIds[ 0 ] ) | ||
: undefined; | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Maybe
mayDisplayControls
etc. should be selectors instead, but it's private anyway so it matters little.