Skip to content

perf(pty-host): add adaptive batching to MessagePort data path#4899

Merged
gregpriday merged 3 commits intodevelopfrom
perf/issue-4819-add-adaptive-batching
Apr 3, 2026
Merged

perf(pty-host): add adaptive batching to MessagePort data path#4899
gregpriday merged 3 commits intodevelopfrom
perf/issue-4819-add-adaptive-batching

Conversation

@gregpriday
Copy link
Copy Markdown
Collaborator

Summary

  • Adds a PortBatcher class that coalesces node-pty data events on the MessagePort path, reducing message volume during bulk terminal output from hundreds of individual posts per second to batched flushes
  • Near-immediate (~1-2ms via setImmediate) flushes when the buffer is empty, batching to ~16ms when data arrives continuously, with an immediate flush if the buffer exceeds 64KB
  • Integrates with the existing portQueueManager watermark-based backpressure so the batcher pauses when the consumer is behind

Resolves #4819

Changes

  • electron/pty-host/portBatcher.ts — new PortBatcher class with adaptive timer logic and 64KB flush threshold
  • electron/pty-host/__tests__/portBatcher.test.ts — 275-line test suite covering idle flush, bulk batching, size threshold, pause/resume, and cleanup
  • electron/pty-host/index.ts — barrel export for PortBatcher
  • electron/pty-host.ts — wires PortBatcher into the MessagePort send path, replacing the direct postMessage call
  • electron/services/pty/types.ts — adds portBatcher field to PtyProcessEntry

Testing

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.

- 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
@gregpriday gregpriday force-pushed the perf/issue-4819-add-adaptive-batching branch from 0826cf0 to 6c53120 Compare April 3, 2026 11:17
@gregpriday gregpriday merged commit 835c89c into develop Apr 3, 2026
3 checks passed
@gregpriday gregpriday deleted the perf/issue-4819-add-adaptive-batching branch April 3, 2026 11:17
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Add adaptive batching to MessagePort PTY data path

1 participant