Repository navigation
perf(terminal): coalesce PTY output into one xterm.write per animation frame (v2 transport) #5255
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
Merged
Kitenite
merged 2 commits into
ParadevAI:main
from
thebrownproject:perf/v2-terminal-write-coalescing
Jun 22, 2026
Merged
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
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
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
131 changes: 131 additions & 0 deletions
131
apps/desktop/src/renderer/lib/terminal/write-coalescer.test.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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,131 @@ | ||
| import { afterEach, beforeEach, describe, expect, test } from "bun:test"; | ||
| import { createWriteCoalescer, MAX_PENDING_BYTES } from "./write-coalescer"; | ||
|
|
||
| // Capture rAF callbacks so tests control frame timing deterministically. | ||
| let frameCallbacks: Map<number, FrameRequestCallback>; | ||
| let nextFrameId: number; | ||
|
|
||
| const originalRaf = globalThis.requestAnimationFrame; | ||
| const originalCancelRaf = globalThis.cancelAnimationFrame; | ||
|
|
||
| function fireFrame() { | ||
| const callbacks = [...frameCallbacks.values()]; | ||
| frameCallbacks.clear(); | ||
| for (const callback of callbacks) { | ||
| callback(performance.now()); | ||
| } | ||
| } | ||
|
|
||
| beforeEach(() => { | ||
| frameCallbacks = new Map(); | ||
| nextFrameId = 1; | ||
| globalThis.requestAnimationFrame = (callback: FrameRequestCallback) => { | ||
| const id = nextFrameId++; | ||
| frameCallbacks.set(id, callback); | ||
| return id; | ||
| }; | ||
| globalThis.cancelAnimationFrame = (id: number) => { | ||
| frameCallbacks.delete(id); | ||
| }; | ||
| }); | ||
|
|
||
| afterEach(() => { | ||
| globalThis.requestAnimationFrame = originalRaf; | ||
| globalThis.cancelAnimationFrame = originalCancelRaf; | ||
| }); | ||
|
|
||
| function bytes(text: string): Uint8Array { | ||
| return new TextEncoder().encode(text); | ||
| } | ||
|
|
||
| describe("createWriteCoalescer", () => { | ||
| test("coalesces chunks arriving in the same frame into one write", () => { | ||
| const writes: Uint8Array[] = []; | ||
| const coalescer = createWriteCoalescer((data) => writes.push(data)); | ||
|
|
||
| coalescer.push(bytes("foo")); | ||
| coalescer.push(bytes("bar")); | ||
| coalescer.push(bytes("baz")); | ||
| expect(writes).toHaveLength(0); | ||
|
|
||
| fireFrame(); | ||
| expect(writes).toHaveLength(1); | ||
| expect(new TextDecoder().decode(writes[0])).toBe("foobarbaz"); | ||
| }); | ||
|
|
||
| test("schedules a new frame for data arriving after a flush", () => { | ||
| const writes: Uint8Array[] = []; | ||
| const coalescer = createWriteCoalescer((data) => writes.push(data)); | ||
|
|
||
| coalescer.push(bytes("first")); | ||
| fireFrame(); | ||
| coalescer.push(bytes("second")); | ||
| fireFrame(); | ||
|
|
||
| expect(writes).toHaveLength(2); | ||
| expect(new TextDecoder().decode(writes[1])).toBe("second"); | ||
| }); | ||
|
|
||
| test("flushSync writes pending bytes immediately and cancels the scheduled frame", () => { | ||
| const writes: Uint8Array[] = []; | ||
| const coalescer = createWriteCoalescer((data) => writes.push(data)); | ||
|
|
||
| coalescer.push(bytes("pending")); | ||
| coalescer.flushSync(); | ||
| expect(writes).toHaveLength(1); | ||
| expect(new TextDecoder().decode(writes[0])).toBe("pending"); | ||
|
|
||
| // The previously scheduled frame must not produce a second write. | ||
| fireFrame(); | ||
| expect(writes).toHaveLength(1); | ||
| }); | ||
|
|
||
| test("flushSync with nothing pending writes nothing", () => { | ||
| const writes: Uint8Array[] = []; | ||
| const coalescer = createWriteCoalescer((data) => writes.push(data)); | ||
|
|
||
| coalescer.flushSync(); | ||
| expect(writes).toHaveLength(0); | ||
| }); | ||
|
|
||
| test("flushes immediately when pending bytes exceed the cap", () => { | ||
| const writes: Uint8Array[] = []; | ||
| const coalescer = createWriteCoalescer((data) => writes.push(data)); | ||
|
|
||
| coalescer.push(new Uint8Array(MAX_PENDING_BYTES + 1)); | ||
| expect(writes).toHaveLength(1); | ||
| expect(writes[0]).toHaveLength(MAX_PENDING_BYTES + 1); | ||
|
|
||
| // Nothing left for the frame to write. | ||
| fireFrame(); | ||
| expect(writes).toHaveLength(1); | ||
| }); | ||
|
|
||
| test("dispose flushes pending bytes and ignores later pushes", () => { | ||
| const writes: Uint8Array[] = []; | ||
| const coalescer = createWriteCoalescer((data) => writes.push(data)); | ||
|
|
||
| coalescer.push(bytes("tail")); | ||
| coalescer.dispose(); | ||
| expect(writes).toHaveLength(1); | ||
| expect(new TextDecoder().decode(writes[0])).toBe("tail"); | ||
|
|
||
| coalescer.push(bytes("ignored")); | ||
| fireFrame(); | ||
| expect(writes).toHaveLength(1); | ||
| }); | ||
|
|
||
| test("preserves byte order across many small chunks", () => { | ||
| const writes: Uint8Array[] = []; | ||
| const coalescer = createWriteCoalescer((data) => writes.push(data)); | ||
|
|
||
| const parts = Array.from({ length: 100 }, (_, i) => `${i},`); | ||
| for (const part of parts) { | ||
| coalescer.push(bytes(part)); | ||
| } | ||
| fireFrame(); | ||
|
|
||
| expect(writes).toHaveLength(1); | ||
| expect(new TextDecoder().decode(writes[0])).toBe(parts.join("")); | ||
| }); | ||
| }); |
Oops, something went wrong.
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.
cancelAnimationFramemock ignores the ID and clears all callbacksThe mock resets the entire
frameCallbacksarray on everycancelAnimationFramecall regardless of which frame ID was passed. The real API only cancels the one frame identified by the given ID. The coalescer enforces a single-pending-rAF invariant today, so no current test is wrong. But if a future test exercises a scenario where two callbacks happen to be queued simultaneously, the mock would incorrectly cancel the unrelated callback and the test would silently produce a false pass. The write-coalescer unit tests already use the correctMap-based mock — using the same pattern here avoids the fragility.Prompt To Fix With AI