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

Track the parent block to optimize hierarchy selectors #16392

Merged
merged 9 commits into from
Jul 5, 2019

Conversation

youknowriad
Copy link
Contributor

Extracted from #16368

This PR tracks the parent of a block in the "blocks.parents" state tree in the reducer to optimizer the performance of some block editor selectors.

On its own, this PR doesn't seem to have a huge impact on performance bit it's a good step towards #16368

@youknowriad youknowriad added the [Type] Performance Related to performance efforts label Jul 2, 2019
@youknowriad youknowriad self-assigned this Jul 2, 2019
@youknowriad youknowriad requested a review from aduth July 2, 2019 12:11
packages/block-editor/src/store/reducer.js Outdated Show resolved Hide resolved
packages/block-editor/src/store/reducer.js Outdated Show resolved Hide resolved
packages/block-editor/src/store/reducer.js Outdated Show resolved Hide resolved
@youknowriad
Copy link
Contributor Author

I fixed the memory leak in the parents reducer. It turns out that I discovered memory leaks in other reducers (byClientId and order), I won't be fixing those right now in this PR though. A generic fix would be good but it's not possible due to the fact that we track reusable blocks their as well.

@youknowriad
Copy link
Contributor Author

I think this should be ready and I'm noticing something between 2-5% performance improvement.

case 'REMOVE_BLOCKS':
action = {
...action,
removedClientIds: getAllChildren( action.clientIds ),
Copy link
Member

Choose a reason for hiding this comment

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

Is there a specific reason we need to rename these properties from clientIds, rather than just "enhancing" the existing value to account for the cascade? Seems like it might make this a bit simpler to implement, since you wouldn't need the switch to vary the behavior.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I've been caught off guard in several occasions because of this. The fact that the property don't contain the same value between reducers (selection and blocks), and also the difference between the action creator (which I tend to take a look at to understand the action) led me to this change.

It is still a different action but instead but I think the fact that we introduce a new key makes it less confusing.

Copy link
Contributor

Choose a reason for hiding this comment

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

I get the motivation for this. It could, however, be pretty jarring for someone who notices the discrepancy and pulls up the definition of removeBlocks, since it clearly only returns clientIds in its payload.

Perhaps update removeBlocks to make that relationship clearer?

// actions.js
yield {
	type: 'REMOVE_BLOCKS',
	clientIds,
	removedClientIds: undefined // Explain bla bla higher-order reducer
};

In alternative, if we're really strict about action types, it would be more "correct" to let this higher-order reducer divert from one action type to a new one:

// reducer.js
if ( state ) {
	switch ( action.type ) {
		case 'REMOVE_BLOCKS':
			action = {
				type: DERIVED_TYPE_SIMILAR_TO_REMOVE_BLOCKS,
				removedClientIds: 

Copy link
Member

Choose a reason for hiding this comment

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

Alternatively, with controls implementation, these days we could probably incorporate this into the action creator itself, rather than as a reducer enhancer. That way the logic is consolidated to one place.

case 'REPLACE_BLOCKS':
return {
...omit( state, action.replacedClientIds ),
...mapBlockParents( action.blocks, state[ action.clientIds[ 0 ] ] ),
Copy link
Member

Choose a reason for hiding this comment

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

Re: My previous comment, I guess we still reference the original clientIds here. Can you clarify what you're doing here in referencing only the first of the original of the clientIds ?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Basically we need the parent of the replaced blocks to assign it as parent of the inserted blocks. replacedClientIds contains more than just the "root" level which in theory could result in the wrong value here.

@@ -193,27 +193,31 @@ describe( 'state', () => {
it( 'can replace a child block', () => {
const existingState = deepFreeze( {
byClientId: {
clicken: {
Copy link
Member

Choose a reason for hiding this comment

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

😆

packages/block-editor/src/store/selectors.js Outdated Show resolved Hide resolved
for ( let i = 0; i < clientIds.length; i++ ) {
clientIds.push( ...state.order[ clientIds[ i ] ] );
for ( let i = 0; i < result.length; i++ ) {
if ( state.order[ result[ i ] ] ) {
Copy link
Member

Choose a reason for hiding this comment

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

Is this condition related to your comment at #16392 (comment) with regards to orphaned references? Should we comment it as a temporary fix if it is meant to be temporary?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

no, to be completely honest, I tried but failed to find why this condition is necessary. When you remove an "empty paragraph" using backspace, there's a JS error that is triggered if you remove this condition.

packages/block-editor/src/store/reducer.js Outdated Show resolved Hide resolved
...state,
[ action.clientId ]: action.toRootClientId || '',
};
}
Copy link
Contributor

Choose a reason for hiding this comment

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

Minor: unneeded block around return statement.

case 'REMOVE_BLOCKS':
action = {
...action,
removedClientIds: getAllChildren( action.clientIds ),
Copy link
Contributor

Choose a reason for hiding this comment

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

I get the motivation for this. It could, however, be pretty jarring for someone who notices the discrepancy and pulls up the definition of removeBlocks, since it clearly only returns clientIds in its payload.

Perhaps update removeBlocks to make that relationship clearer?

// actions.js
yield {
	type: 'REMOVE_BLOCKS',
	clientIds,
	removedClientIds: undefined // Explain bla bla higher-order reducer
};

In alternative, if we're really strict about action types, it would be more "correct" to let this higher-order reducer divert from one action type to a new one:

// reducer.js
if ( state ) {
	switch ( action.type ) {
		case 'REMOVE_BLOCKS':
			action = {
				type: DERIVED_TYPE_SIMILAR_TO_REMOVE_BLOCKS,
				removedClientIds: 

Copy link
Member

@aduth aduth left a comment

Choose a reason for hiding this comment

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

I'm not sure if you wanted to make revisions based on @mcsf 's comment (I think either could maybe help), but in any case this looks like a solid improvement as it is.

},
};

expect( getBlockHierarchyRootClientId( state, 56 ) ).toBe( '123' );
Copy link
Member

Choose a reason for hiding this comment

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

Considering it's documented to return a value of type string, I feel like we should choose clientIds which allow us to test for this to be true.

* @return {string} Root client ID
*/
export const getBlockHierarchyRootClientId = createSelector(

case 'REMOVE_BLOCKS':
action = {
...action,
removedClientIds: getAllChildren( action.clientIds ),
Copy link
Member

Choose a reason for hiding this comment

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

Alternatively, with controls implementation, these days we could probably incorporate this into the action creator itself, rather than as a reducer enhancer. That way the logic is consolidated to one place.

@youknowriad youknowriad merged commit ab847cb into master Jul 5, 2019
@github-actions github-actions bot added this to the Gutenberg 6.1 milestone Jul 5, 2019
daniloercoli added a commit that referenced this pull request Jul 8, 2019
…rnmobile/track-unsupported-blocks

* 'master' of https://github.com/WordPress/gutenberg:
  Bump plugin version to 6.1.0-rc.1
  Update HTML anchor explaination text (#16142)
  Move post permalink to beneath title on mobile. (#16277)
  Export cloneBlock method to the mobile (#16441)
  Fix inconsistent references to Settings Sidebar (#16138)
  Adds a cache key to the blocks reducer in order to optimize the getBlock selector (#16407)
  Track the parent block to optimize hierarchy selectors (#16392)
@aduth aduth deleted the add/parent-block-reducer branch July 10, 2019 12:21
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
[Type] Performance Related to performance efforts
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants