perf(runtime): drop old terminal resume output through a head index instead of splicing

This commit is contained in:
Kelvin Amoaba
2026-11-08 18:50:22 +00:00
parent f74bb65fa1
commit c2bb7cd57c
2 changed files with 50 additions and 16 deletions
@@ -25,6 +25,8 @@ export class TerminalStreamResumeLedger {
// The ring coverage the client's screen was drawn from; a reset (clear, respawn) leaves older text on it.
private epoch: number | null = null
private points: ResumePoint[] = []
// Points before `head` are dropped; compacting only once they are half the array keeps each add O(1).
private head = 0
constructor(
readonly ptyId: string,
@@ -45,7 +47,7 @@ export class TerminalStreamResumeLedger {
// A snapshot replaced the client's screen, so earlier counts no longer say what it shows.
if (opcode === TerminalStreamOpcode.SnapshotStart) {
this.shownSeq = null
this.points = []
this.clearPoints()
}
}
@@ -74,7 +76,13 @@ export class TerminalStreamResumeLedger {
/** Output the client missed after receiving `units`, or null unless that is a recorded point the ring still holds. */
readGap(units: number, currentSeq: number): TerminalResumeGap | null {
const point = this.points.findLast((candidate) => candidate.units === units)
let point: ResumePoint | undefined
for (let index = this.points.length - 1; index >= this.head; index -= 1) {
if (this.points[index]!.units === units) {
point = this.points[index]
break
}
}
if (!point) {
return null
}
@@ -87,20 +95,31 @@ export class TerminalStreamResumeLedger {
// Output after a reset lands on a screen that still shows text from before it, which no snapshot now matches.
if (point.epoch !== this.epoch) {
this.shownSeq = null
this.points = []
this.clearPoints()
return
}
this.addPoint(point)
}
private addPoint(point: PublishedOutputPoint): void {
if (this.points.at(-1)?.units === this.units) {
if (this.points.length > this.head && this.points.at(-1)!.units === this.units) {
this.points.pop()
}
this.points.push({ ...point, units: this.units })
const covered = this.points.findIndex((candidate) => this.ring.covers(candidate))
const drop = covered === -1 ? this.points.length : covered
this.points.splice(0, Math.max(drop, this.points.length - MAX_RESUME_POINTS))
let head = this.head
while (head < this.points.length && !this.ring.covers(this.points[head]!)) {
head += 1
}
this.head = Math.max(head, this.points.length - MAX_RESUME_POINTS)
if (this.head * 2 >= this.points.length) {
this.points = this.points.slice(this.head)
this.head = 0
}
}
private clearPoints(): void {
this.points = []
this.head = 0
}
}
@@ -23,6 +23,8 @@ export class TerminalPublishedOutputRing {
/** Changes whenever coverage restarts; output recorded under an older epoch can't be resumed from. */
epoch = 0
private chunks: (PublishedTerminalOutput & { size: TerminalSize })[] = []
// Chunks before `head` are dropped; compacting only once they are half the array keeps append O(1).
private head = 0
private units = 0
private startSeq = 0
private endSeq = 0
@@ -43,20 +45,26 @@ export class TerminalPublishedOutputRing {
this.chunks.push({ ...chunk, size: this.currentSize() })
this.units += chunk.data.length
this.endSeq = chunk.seq
let dropCount = 0
while (this.units > RING_MAX_UNITS && dropCount < this.chunks.length) {
this.units -= this.chunks[dropCount]!.data.length
dropCount += 1
let head = this.head
while (this.units > RING_MAX_UNITS && head < this.chunks.length) {
this.units -= this.chunks[head]!.data.length
head += 1
}
if (dropCount > 0) {
this.startSeq = this.chunks[dropCount - 1]!.seq
this.chunks.splice(0, dropCount)
if (head === this.head) {
return
}
this.startSeq = this.chunks[head - 1]!.seq
this.head = head
if (head * 2 >= this.chunks.length) {
this.chunks = this.chunks.slice(head)
this.head = 0
}
}
resetCoverage(seq = this.endSeq): void {
this.epoch = nextCoverageEpoch++
this.chunks = []
this.head = 0
this.units = 0
this.startSeq = seq
this.endSeq = seq
@@ -68,7 +76,13 @@ export class TerminalPublishedOutputRing {
/** The point a client reaches by receiving output through `seq`: the grid that output was written for. */
pointAt(seq: number): PublishedOutputPoint {
const chunk = this.chunks.findLast((candidate) => candidate.seq - candidate.rawLength < seq)
let chunk: (typeof this.chunks)[number] | undefined
for (let index = this.chunks.length - 1; index >= this.head; index -= 1) {
if (this.chunks[index]!.seq - this.chunks[index]!.rawLength < seq) {
chunk = this.chunks[index]
break
}
}
const size = chunk && seq <= chunk.seq ? chunk.size : this.currentSize()
return { seq, epoch: this.epoch, size }
}
@@ -85,7 +99,8 @@ export class TerminalPublishedOutputRing {
return null
}
const gap: PublishedTerminalOutput[] = []
for (const { size: chunkSize, ...chunk } of this.chunks) {
for (let index = this.head; index < this.chunks.length; index += 1) {
const { size: chunkSize, ...chunk } = this.chunks[index]!
if (chunk.seq <= point.seq) {
continue
}