-
Notifications
You must be signed in to change notification settings - Fork 4.7k
[Sidebar][BlockPatterns] Prevent pattern previews from rendering in parallel due to React's concurrent mode #48085
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
Closed
fullofcaffeine
wants to merge
1
commit into
trunk
from
try/improve-rendering-perf-concurrent-mode-pattern-preview
Closed
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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 may have an unexpected side effect on other areas of the editor that use the
useAsyncListhook. Have you measured any potential effects there?I wonder if alternatively, we can just debounce the showing of the pattern previews instead of the addition of patterns to the async list?
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.
That's the whole purpose of "useAsyncList" debounce the showing of the previews. But instead of relying on random "debounce" value, it relies on
requestIdleCallbackinternally (in priority queue) to continue rendering previews as long as the browser is idle.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.
Makes sense to me.
Do you have any concerns with shipping this change @youknowriad? I wasn't able to measure any noticeable difference before and after this PR in my testing.
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.
The change is fine but I'm surprised it's needed. Maybe @fullofcaffeine know the difference here
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.
We have a bug that's fixed by changing a subtle detail deep inside the core and there's no comment. Seems like a very good place to say "We cannot use
asyncQueue.add()here because …" 😉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.
🤡
Uh oh!
There was an error while loading. Please reload this page.
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.
When originally adapting
useAsyncListfor concurrent mode, I didn't notice that we're checkingtimeRemaining()and trying to fit as much work as possible into one idle callback. I thought it's just a series of idle callbacks each performingsteprenders. Exactly the behavior we're getting in this branch after adding thequeueMicrotasks.Here's how we could get the original behavior even in concurrent mode:
The magical trick here is that the idle callback can be an async function, doing its work asynchronously, with promises, and still keep checking the
deadline!. It doesn't need to be fully sync.The code I wrote doesn't use the
priority-queue, it callsrequestIdleCallbackdirectly.To make it work with
priority-queue, we'd have to:runWaitingListfunction async, and make itawait callback().asyncQueue.addthat we have now. We know upfront what we want to get done, and then it will get executed in idle callbacks.One thing about
flushSyncis that it's in thereact-dompackage. For mobile, we'd have to use thereact-nativeequivalent, which I hope exists. And export them from@wordpress/element.By the way, I think that in concurrent mode we eventually shouldn't need
useAsyncListat all. Processing a long render queue asynchronously, taking smaller bites, checking deadlines, and letting higher-priority interruptions run, that's what concurrent mode is supposed to do natively. I don't have much insight into the precise details right now, but once we learn about that, we should be able to eliminateuseAsyncList.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.
Ok so
ReactDOM.flushSyncdoesn't re-render synchronously? So in that case we still need the microtask. In that case, yeah the only part forward would be to promisify priority-queue (or built a promisified alternative). Also, I don't see why we needflushSyncin this case? what does it do?setCurrentalready schedules the microtask internallyThere 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.
Yeah, I agree but it seems React is y et to provide the APIs to define the priorities and basically
useAsyncListcould probably but one of these APIs.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.
Turns out I was wrong abou this:
flushSyncis indeed completely synchronous, including effects. That means we don't need any microtasks and promises, only aflushSyncwrapper aroundsetCurrent. I submitted this fix as #48238.I'm not 100% sure if the scheduling of
setStateis always the microtask. The scheduling strategy is different in different scenarios, and sometimes the update can be scheduled for later.