perf(server): replace quadratic task queue drain with index-based FIFO - #37464
Open
mturac wants to merge 1 commit into
Open
perf(server): replace quadratic task queue drain with index-based FIFO#37464mturac wants to merge 1 commit into
mturac wants to merge 1 commit into
Conversation
The browser server stream scheduler used Array.shift() to dequeue callbacks one at a time. shift() moves every remaining element on each call, making a burst of n scheduled tasks O(n²) to drain. Replace with a head-index that advances in O(1). Null out consumed slots so the callbacks can be collected, and compact the underlying array when the consumed prefix exceeds both 1024 entries and half the array length, keeping amortized memory bounded. Fixes react#37453
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
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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.
Summary
Fixes #37453 — the browser server stream scheduler stores pending callbacks in an array and removes one per
MessageChanneldelivery withArray.shift(). Each shift copies every remaining element, making queue draining O(n²) in the number of scheduled tasks during a burst.Fix: Replace
shift()with a head-index that advances in O(1). Consumed slots are nulled out so callbacks can be garbage-collected. When the consumed prefix exceeds both 1024 entries and half the array length, the live tail is compacted into a fresh array. This preserves FIFO ordering, supports reentrant scheduling, and keeps amortized memory bounded.Changed files
packages/react-server/src/ReactServerStreamConfigBrowser.js— replaceshift()with indexed dequeue and periodic compactionTest plan
ReactDOMFizzServer-test.js— 183/183 passed (core SSR streaming tests)packages/react-server/— 2/3 suites passed (1 pre-existing snapshot failure inReactFlightAsyncDebugInfo-test.js, unrelated to this change — confirmed by running on unmodifiedmain)