Files
orca/src/shared/osc-title-scan-tail-retention.test.ts
Neil 1e2ed20b35 perf(terminal): release oversized backing strings behind pending controls (#19396)
* perf(terminal): release oversized backing strings behind pending controls

* test(terminal): record reproducible pending-storage gate evidence

* perf(terminal): own retained control fragments with a fast copy primitive

The pending-control ownership landed with a charCodeAt block copier (10 us
at 4 Ki, 170 us at 64 Ki), so it needed a "copy only when discarded output
dominates the tail" gate to stay affordable. That gate was the whole cost
problem: on adversarial streams it fires every chunk and pays the slow copy
(+45% on 16 Ki ANSI chunks, +81..111% on 194 Ki status chunks), and it also
skipped ownership on fragments too small to be sliced strings anyway.

ownRetainedString replaces it with a Buffer utf16le round trip (0.57 us at
4 Ki, 21.9 us at 64 Ki) and returns anything below V8's SlicedString
kMinLength unchanged. Buffer is absent in the renderer and on mobile, so the
copier is resolved once behind a lone-surrogate round-trip self-check and
falls back to the block copier. With a ~1 us copy the gate is unnecessary:
ownership is now unconditional at all three retention sites and the
adversarial cases land within noise of the un-owned parsers.

The three forced-GC threshold fixtures are replaced by one forced-GC test
for the primitive plus deterministic spy assertions that each site routes
its retained value through ownRetainedString. All fidelity and differential
coverage is kept.

* fix(terminal): escape the NUL in the round-trip probe

A raw NUL byte in the source made git treat the file as binary, so its
diffs and blame were unreadable. Escapes are equivalent at runtime.
2026-09-09 03:40:03 -07:00

66 lines
2.6 KiB
TypeScript

import { describe, expect, it, vi } from 'vitest'
import * as ownership from './own-retained-string'
import { extractOscTitleScanTail } from './osc-title-scan-tail'
const INCOMPLETE_TITLE = '\x1b]2;Working on a terminal title'
describe('OSC title scan tail storage', () => {
it('routes every retained title tail through ownRetainedString', () => {
const own = vi.spyOn(ownership, 'ownRetainedString')
try {
const tail = extractOscTitleScanTail('x'.repeat(32 * 1024) + INCOMPLETE_TITLE)
expect(own).toHaveBeenCalledTimes(1)
expect(own).toHaveBeenLastCalledWith(INCOMPLETE_TITLE)
expect(tail).toBe(INCOMPLETE_TITLE)
// Ownership is unconditional: a growing title is re-owned on every chunk.
let pending = tail
for (let index = 0; index < 8; index += 1) {
pending = extractOscTitleScanTail(pending + 'x'.repeat(512))
}
expect(own).toHaveBeenCalledTimes(9)
expect(own).toHaveBeenLastCalledWith(pending)
// An unrelated incomplete OSC still yields nothing to retain.
expect(extractOscTitleScanTail(`${'x'.repeat(32 * 1024)}\x1b]9999;incomplete`)).toBe('')
} finally {
own.mockRestore()
}
})
it.each([
[16 * 1024, 256, INCOMPLETE_TITLE],
[64 * 1024, 64, INCOMPLETE_TITLE],
[1024 * 1024, 16, INCOMPLETE_TITLE],
[16 * 1024, 128, INCOMPLETE_TITLE + 'x'.repeat(16 * 1024)]
])('keeps %i-character chunks with %i incomplete titles byte-exact', (size, count, title) => {
const tails: string[] = []
for (let index = 0; index < count; index++) {
tails.push(
extractOscTitleScanTail(String.fromCharCode(65 + (index % 26)).repeat(size) + title)
)
}
const expected = title.length <= 4096 ? title : title.slice(0, 4) + title.slice(-4092)
expect(tails).toEqual(Array.from({ length: count }, () => expected))
})
it.each(['0', '1', '2'])('preserves title %s introducer and exact UTF-16 at the cap', (code) => {
const prefix = `\x1b]${code};`
for (const length of [4095, 4096, 4097, 16 * 1024]) {
const value = `${prefix}${'x'.repeat(length - 9)}漢\ud800|\udc00\ud83d`
const expected = value.length <= 4096 ? value : prefix + value.slice(-4092)
const tail = extractOscTitleScanTail('a'.repeat(32 * 1024) + value)
expect(tail).toBe(expected)
expect(extractOscTitleScanTail(`${tail}\ude00\x1b\\`)).toBe('')
}
})
it('keeps trimming an owned title that grows past the cap', () => {
let pending = '\x1b]2;'
for (let index = 0; index < 128; index++) {
pending = extractOscTitleScanTail(pending + 'x'.repeat(512))
}
expect(pending).toBe(`\x1b]2;${'x'.repeat(4092)}`)
})
})