perf(pty-host): add adaptive batching to MessagePort data path#4899
Merged
gregpriday merged 3 commits intodevelopfrom Apr 3, 2026
Merged
perf(pty-host): add adaptive batching to MessagePort data path#4899gregpriday merged 3 commits intodevelopfrom
gregpriday merged 3 commits intodevelopfrom
Conversation
- Add PortBatcher class with two-mode coalescing: setImmediate for interactive latency (~1-2ms), setTimeout(16) for bulk throughput - Sync-flush at 64KB threshold to prevent timer-drift stalls - Capacity checks include pending (unflushed) bytes to prevent exceeding IPC_MAX_QUEUE_BYTES before first flush - flushTerminal() for ordered exit: flush pending data before exit event - Drop on disconnect (no flush to closing port) - Wire into pty-host.ts RendererConnection with per-window batcher - Add PORT_BATCH_THRESHOLD_BYTES and PORT_BATCH_THROUGHPUT_DELAY_MS constants - 16 unit tests covering all modes, boundaries, and error paths
- Flush pending terminal data before capacity rejection to prevent split-channel delivery (buffered on MessagePort + rejected on SAB/IPC) - Reset mode and cancel stale timers in flushTerminal when buffer empties - Import PORT_BATCH_THRESHOLD_BYTES in tests instead of hardcoded copy - Add tests for capacity-rejection flush and mode-staleness after flushTerminal
- Remove orphaned callCount variable to pass lint ratchet
0826cf0 to
6c53120
Compare
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
PortBatcherclass that coalescesnode-ptydata events on the MessagePort path, reducing message volume during bulk terminal output from hundreds of individual posts per second to batched flushessetImmediate) flushes when the buffer is empty, batching to ~16ms when data arrives continuously, with an immediate flush if the buffer exceeds 64KBportQueueManagerwatermark-based backpressure so the batcher pauses when the consumer is behindResolves #4819
Changes
electron/pty-host/portBatcher.ts— newPortBatcherclass with adaptive timer logic and 64KB flush thresholdelectron/pty-host/__tests__/portBatcher.test.ts— 275-line test suite covering idle flush, bulk batching, size threshold, pause/resume, and cleanupelectron/pty-host/index.ts— barrel export forPortBatcherelectron/pty-host.ts— wiresPortBatcherinto the MessagePort send path, replacing the directpostMessagecallelectron/services/pty/types.ts— addsportBatcherfield toPtyProcessEntryTesting
Unit tests pass. The SAB primary path is unchanged — this only touches the MessagePort fallback path that handles traffic during initialisation and on certain platform configurations.