perf(terminal): avoid rescanning cold restore payloads

This commit is contained in:
Brennan Benson
2026-07-22 19:53:19 -07:00
parent 846ad263cd
commit 42218e3a8f
2 changed files with 12 additions and 2 deletions
@@ -10,6 +10,15 @@ function payload(scrollback: string): ColdRestorePayload {
}
describe('ColdRestorePayloadCache', () => {
it('counts retained strings by UTF-16 code units', () => {
expect(
getColdRestorePayloadBytes({
...payload('😀'),
oscLinks: [{ row: 0, startCol: 0, endCol: 1, uri: 'é' }]
})
).toBe(54)
})
it('evicts least-recently-used payloads to stay under its byte bound', () => {
const first = payload('a'.repeat(100))
const second = payload('b'.repeat(100))
@@ -13,8 +13,9 @@ export const MAX_COLD_RESTORE_CACHE_BYTES = 16 * 1024 * 1024
export function getColdRestorePayloadBytes(payload: ColdRestorePayload): number {
const oscLinkBytes =
payload.oscLinks?.reduce((bytes, link) => bytes + Buffer.byteLength(link.uri) + 24, 0) ?? 0
return Buffer.byteLength(payload.scrollback) + Buffer.byteLength(payload.cwd) + oscLinkBytes + 16
payload.oscLinks?.reduce((bytes, link) => bytes + link.uri.length * 2 + 24, 0) ?? 0
// Why: code-unit sizing bounds V8 string storage without rescanning or flattening multi-MB ropes.
return payload.scrollback.length * 2 + payload.cwd.length * 2 + oscLinkBytes + 16
}
export class ColdRestorePayloadCache {