-
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
getBlockSettings: avoid memoized selector with clientId #58405
Conversation
Size Change: +7 B (0%) Total Size: 1.7 MB
ℹ️ View Unchanged
|
Flaky tests detected in fe53ba2. 🔍 Workflow run URL: https://github.com/WordPress/gutenberg/actions/runs/7701608481
|
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.
Looks good. There are many other places where getBlockParents
could be similarly replaced to avoid the memoized version.
Many of these selectors also look like good places to use generators and iterators. We could have a function:
function* listBlockParents( state, clientId ) {
for ( let clientId = ... ) { yield clientId; };
}
and then use it like
const getBlockParents = () => [ ...listBlockParents() ];
or
for ( const id of listBlockParents() )
and with stage 3 iterator helpers we could even have
listBlockParents().every( ... );
I'm wondering how that would perform. There are places where we are constructing large arrays and stacks, like flattenedBlocks
in updateBlockTreeForBlocks
, and they could be streamlined using iterators.
if ( hasBlockSupport( name, '__experimentalSettings', false ) ) { | ||
candidates.push( id ); | ||
} | ||
} while ( ( id = state.blocks.parents.get( id ) ) ); |
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.
Could it be a for loop?
for ( let id = clientId; id; id = state.blocks.parent.get( id ) ) {}
That eliminates also the if ( clientId )
condition.
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.
It could be I guess 🤔 Personally I don't find standard for loops very readable, I tend to avoid them
Let's wait with merging this for a few commits to see result of #58355 |
@jsnajdr Yes that would be nice, that way you avoid looping over the same array multiple times if you need to filter it afterwards. |
What?
Calling getBlockParent( clientId ) causes a cache lookup which may be large for a large number of blocks. We don't need a stable reference in getBlockSettings so it should be faster to directly get the parents.
Why?
We use
getBlockSettings
within the inner blocks hook, and note that every list AND list item (even without nested lists) will run this hook, as well as galleries, quotes etc.First run site editor load -3.2%, next runs seems to be around 1%...
How?
Testing Instructions
Testing Instructions for Keyboard
Screenshots or screencast