perf(renderer): keep pre-handler PTY buffering linear (#8082)

This commit is contained in:
Neil
2026-07-10 20:51:44 -07:00
committed by GitHub
parent 65b7d9a532
commit e085a84df8
2 changed files with 104 additions and 12 deletions
@@ -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)
})
})
@@ -7,7 +7,13 @@ type BufferedPreHandlerPtyData = {
meta?: PtyDataMeta
}
const preHandlerPtyData = new Map<string, BufferedPreHandlerPtyData[]>()
type BufferedPreHandlerPtyState = {
chunks: BufferedPreHandlerPtyData[]
head: number
bytes: number
}
const preHandlerPtyData = new Map<string, BufferedPreHandlerPtyState>()
const preHandlerPtyExit = new Map<string, number>()
// 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)
}
}