perf(desktop): coalesce streaming text/reasoning deltas per animation frame (#3114)#3501
Merged
Conversation
… frame The agent stream pushes a text/reasoning delta per Webview event; at 200 tok/s that is one state update every ~5ms, while rAF is 16ms — so the reducer was running 3-4 times per visible frame and the React tree was re-rendering the entire Message list each time. Add lib/rafBatch.ts: a tiny rAF-coalescing queue with a synchronous drain() so non-text events (tool_dispatch, usage, notice, turn_started/ done, message) flush any pending deltas first to keep causal ordering intact. Drain is also called on unmount so a turn-end that races the teardown doesn't strand the last few tokens in the buffer. tsc --noEmit passes; Go build ./desktop/... is clean.
… frame The agent stream pushes a text/reasoning delta per Webview event; at 200 tok/s that is one state update every ~5ms, while rAF is 16ms — so the reducer was running 3-4 times per visible frame and the React tree was re-rendering the entire Message list each time. Add lib/rafBatch.ts: a tiny rAF-coalescing queue with a synchronous drain() so non-text events (tool_dispatch, usage, notice, turn_started/ done, message) flush any pending deltas first to keep causal ordering intact. Drain is also called on unmount so a turn-end that races the teardown doesn't strand the last few tokens in the buffer. tsc --noEmit passes; Go build ./desktop/... is clean.
# Conflicts: # desktop/frontend/src/lib/useController.ts
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.
Lands the rAF streaming coalescer from #3114 (by @HUQIANTAO) on top of the current
main-v2.The original PR was written against the old single global
dispatch;main-v2has since moved the controller to per-tab dispatch (dispatchTo(targetTabId, …)). Re-applied the batching onto the new model:tabIdand flushes viadispatchTo, so coalesced deltas route to the correct tab;drain()first to preserve causal ordering (unchanged intent);onReady/syncActiveTabFromBackendsetup.Also trimmed
rafBatch.ts's comments to repo style (2-line header, 1-line whys) — logic unchanged.Original author attribution preserved.
Closes #3114