From e085a84df822c52faca1be1f239cf7450632f5e9 Mon Sep 17 00:00:00 2001 From: Neil <4138956+nwparker@users.noreply.github.com> Date: Fri, 10 Jul 2026 20:51:44 -0700 Subject: [PATCH] perf(renderer): keep pre-handler PTY buffering linear (#8082) --- .../pty-pre-handler-buffer.test.ts | 74 +++++++++++++++++++ .../terminal-pane/pty-pre-handler-buffer.ts | 42 ++++++++--- 2 files changed, 104 insertions(+), 12 deletions(-) create mode 100644 src/renderer/src/components/terminal-pane/pty-pre-handler-buffer.test.ts diff --git a/src/renderer/src/components/terminal-pane/pty-pre-handler-buffer.test.ts b/src/renderer/src/components/terminal-pane/pty-pre-handler-buffer.test.ts new file mode 100644 index 000000000..7d95304ba --- /dev/null +++ b/src/renderer/src/components/terminal-pane/pty-pre-handler-buffer.test.ts @@ -0,0 +1,74 @@ +import { afterEach, describe, expect, it } from 'vitest' +import { + bufferPreHandlerPtyData, + clearPreHandlerPtyState, + drainPreHandlerPtyData +} from './pty-pre-handler-buffer' + +const RESCAN_PTY_ID = 'pty-pre-handler-rescan' +const TRIM_PTY_ID = 'pty-pre-handler-trim' + +describe('pre-handler PTY buffer', () => { + afterEach(() => { + clearPreHandlerPtyState(RESCAN_PTY_ID) + clearPreHandlerPtyState(TRIM_PTY_ID) + }) + + it('does not rescan historical chunks while buffering small startup output', () => { + const originalReduce = Array.prototype.reduce + + try { + Object.defineProperty(Array.prototype, 'reduce', { + configurable: true, + writable: true, + value() { + throw new Error('Array.reduce should not be used by the pre-handler PTY buffer') + } + }) + for (let index = 0; index < 4_096; index += 1) { + bufferPreHandlerPtyData(RESCAN_PTY_ID, 'x') + } + } finally { + Object.defineProperty(Array.prototype, 'reduce', { + configurable: true, + writable: true, + value: originalReduce + }) + } + + const drained: string[] = [] + drainPreHandlerPtyData(RESCAN_PTY_ID, (data) => drained.push(data)) + expect(drained).toHaveLength(4_096) + }) + + it('does not shift the live array while trimming a capped backlog', () => { + const originalShift = Array.prototype.shift + const originalWarn = console.warn + + try { + console.warn = () => {} + Object.defineProperty(Array.prototype, 'shift', { + configurable: true, + writable: true, + value() { + throw new Error('Array.shift should not be used by the pre-handler PTY buffer') + } + }) + for (let index = 0; index < 2_048; index += 1) { + bufferPreHandlerPtyData(TRIM_PTY_ID, 'x'.repeat(1_024)) + } + } finally { + console.warn = originalWarn + Object.defineProperty(Array.prototype, 'shift', { + configurable: true, + writable: true, + value: originalShift + }) + } + + const drained: string[] = [] + drainPreHandlerPtyData(TRIM_PTY_ID, (data) => drained.push(data)) + expect(drained).toHaveLength(512) + expect(drained.join('')).toHaveLength(512 * 1_024) + }) +}) diff --git a/src/renderer/src/components/terminal-pane/pty-pre-handler-buffer.ts b/src/renderer/src/components/terminal-pane/pty-pre-handler-buffer.ts index 23c4661ad..c3d12fed9 100644 --- a/src/renderer/src/components/terminal-pane/pty-pre-handler-buffer.ts +++ b/src/renderer/src/components/terminal-pane/pty-pre-handler-buffer.ts @@ -7,7 +7,13 @@ type BufferedPreHandlerPtyData = { meta?: PtyDataMeta } -const preHandlerPtyData = new Map() +type BufferedPreHandlerPtyState = { + chunks: BufferedPreHandlerPtyData[] + head: number + bytes: number +} + +const preHandlerPtyData = new Map() const preHandlerPtyExit = new Map() // Why: Windows startup commands can emit output before pty:spawn resolves and @@ -36,21 +42,32 @@ export function bufferPreHandlerPtyData(ptyId: string, data: string, meta?: PtyD meta && chunk.data.length !== data.length && typeof meta.rawLength === 'number' ? { ...meta, rawLength: chunk.bytes } : meta - const chunks = preHandlerPtyData.get(ptyId) ?? [] - chunks.push({ + let state = preHandlerPtyData.get(ptyId) + if (!state) { + state = { chunks: [], head: 0, bytes: 0 } + preHandlerPtyData.set(ptyId, state) + } + state.chunks.push({ data: chunk.data, bytes: chunk.bytes, ...(bufferedMeta ? { meta: bufferedMeta } : {}) }) - let totalBytes = chunks.reduce((total, entry) => total + entry.bytes, 0) - while (totalBytes > PRE_HANDLER_PTY_DATA_MAX_BYTES && chunks.length > 1) { - totalBytes -= chunks.shift()?.bytes ?? 0 + state.bytes += chunk.bytes + // Why: a missing handler can accumulate many small chunks; a stored total + // and head index keep that failure path linear instead of rescanning/shifting. + while (state.bytes > PRE_HANDLER_PTY_DATA_MAX_BYTES && state.head < state.chunks.length - 1) { + state.bytes -= state.chunks[state.head].bytes + state.chunks[state.head] = { data: '', bytes: 0 } + state.head += 1 } - preHandlerPtyData.set(ptyId, chunks) - if (totalBytes > PRE_HANDLER_PTY_DATA_WARN_BYTES && !warnedLostHandlerPtyIds.has(ptyId)) { + if (state.head > 0 && state.head * 2 >= state.chunks.length) { + state.chunks.splice(0, state.head) + state.head = 0 + } + if (state.bytes > PRE_HANDLER_PTY_DATA_WARN_BYTES && !warnedLostHandlerPtyIds.has(ptyId)) { warnedLostHandlerPtyIds.add(ptyId) console.warn( - `[pty] ${ptyId}: ${totalBytes} bytes buffered with no registered data handler; ` + + `[pty] ${ptyId}: ${state.bytes} bytes buffered with no registered data handler; ` + 'the owning pane may have lost its handler to a detach/attach race' ) } @@ -60,13 +77,14 @@ export function drainPreHandlerPtyData( ptyId: string, handler: (data: string, meta?: PtyDataMeta) => void ): void { - const chunks = preHandlerPtyData.get(ptyId) + const state = preHandlerPtyData.get(ptyId) warnedLostHandlerPtyIds.delete(ptyId) - if (!chunks) { + if (!state) { return } preHandlerPtyData.delete(ptyId) - for (const chunk of chunks) { + for (let index = state.head; index < state.chunks.length; index += 1) { + const chunk = state.chunks[index] handler(chunk.data, chunk.meta) } }