-
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
Improve getBlockEditingMode() and useAppender() performance #51675
Conversation
Size Change: +8.96 kB (+1%) Total Size: 1.42 MB
ℹ️ View Unchanged
|
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.
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.
This is testing just as on trunk for me:
✅ Appender shows and hides when expected in post editor and site editor
✅ Custom appender for container blocks shows and hides as expected
Code change LGTM, I noticed that getBlockEditingMode
is used in a few other places (list view, block toolbar, etc), but everything else appeared to be working correctly, too, as far as I can tell. I'll just ping @youknowriad for visibility since he's commented a few times recently on performance related concerns related to useSelect
and selectors in general. Not a blocker, though, as this PR would be easy to revert if there are any concerns.
Nice work! ✨
It's very cool to see that the graphs in codevitals.io are back to their previous state approximatively. |
…s#51675) * Don't memoize getBlockEditingMode() * useAppender(): Check block selection before other checks * Don't return JSX from useSelect()
What?
Fixes a performance regression introduced in #51148 (comment).
How?
There's two fixes / issues at play here:
useAppender()
will only show a default appender in a container block when the container block is selected or at the document level when there is no selection. ButgetTemplateLock()
,getBlockEditingMode()
, and__unstableGetEditorMode()
are called regardless of whether the appender is shown or not. This is a little wasteful. We can fix this by swapping the order of theisParentSelected
andisInserterHidden
checksgetBlockEditingMode()
is memoised usingcreateSelector()
fromrememo
. I did this becausegetBlockEditingMode()
will recurse through the block's ancestors. This is problematic though becauserememo
will iterate through every cached entry when the selector is called which in this case is significant as there will be one cache entry per block. The fix is to not usecreateSelector()
. In most cases the depth of the block tree (number of ancestors) will be less than the number of blocks.Testing Instructions
Nothing specific. Check that the block appender still appears when expected at the document level and in container blocks.
Benchmark
trunk
This branch