Files
orca/config/scripts/pty-source-ack-boundary-benchmark.mjs
T
Neil 75e5c996c1 perf(relay): stop ACK boundary scans at first pending boundary (#17491)
* perf(relay): stop ACK boundary scans at first pending boundary

* test(relay): pin PTY source boundary cleanup and guard ascending sends

The early-`break` in advanceCredit is only correct while sentBoundaries is
inserted in ascending sentEndSu order. Turn that implicit invariant into a
throw at the sole live write site (commitPtySourceSend), and assert the
post-state directly instead of inferring it from an iteration budget:

- assert the surviving boundary set after the 1,023-ACK benchmark
- cover the jump-ahead cumulative ACK that must delete many boundaries in
  one pass (the case an over-eager `break` would get wrong)
- cover the settleReservedPtySourceAck -> advanceCredit entry point
- drop an arithmetically-implied assertion and CI benchmark log noise

* perf(relay): reclaim ACK boundaries with a monotone cursor

The early-break Set scan still rebuilt a Set iterator per ACK, so V8 walked
delete tombstones and the drain stayed superlinear; the visit-count test could
not see it because it stubbed sentBoundaries with a generator over a private
Set. Replace the Set with an ascending boundary list plus a monotone cursor,
assert the real structure, and add a benchmark over the shipped code.

* test(relay): enforce ascending sent-boundary inserts in the collection

Move the ascending-order precondition into PtySourceSentBoundaries.add so
both insert sites are covered, and assert per-ACK span reclamation in the drain.

* test(relay): collapse ledger test record accessors into getDeliveryRecord

Rebase onto #17490 left two structurally identical internals accessors
(getCursorRecord, getBoundaryRecord); one typed accessor covers both.
2026-08-31 03:10:44 -07:00

90 lines
3.0 KiB
JavaScript

#!/usr/bin/env node
// Benchmarks relay PTY sent-boundary cleanup across a cumulative ACK drain, using the shipped
// PtySourceSentBoundaries rather than a stub so V8's real Set/array costs are included.
import { performance } from 'node:perf_hooks'
import { createJiti } from 'jiti'
const BOUNDARY_COUNTS = [1_024, 4_096, 16_384, 65_536]
// Set rehash timing swings run to run, so every row reports a median instead of a single sample.
const REPETITIONS = 3
const jiti = createJiti(import.meta.url)
const { PtySourceSentBoundaries } = await jiti.import(
'../../src/relay/pty-source-sent-boundaries.ts'
)
// Pre-change cleanup: rescan every retained boundary on every ACK.
function fullScanDrain(count) {
const boundaries = new Set()
for (let boundary = 0; boundary <= count; boundary += 1) {
boundaries.add(boundary)
}
const started = performance.now()
for (let creditedEndSu = 1; creditedEndSu <= count; creditedEndSu += 1) {
for (const boundary of boundaries) {
if (boundary < creditedEndSu) {
boundaries.delete(boundary)
}
}
}
return performance.now() - started
}
// Early-break cleanup: stops at the first live boundary, but still rebuilds a Set iterator per ACK.
function earlyBreakDrain(count) {
const boundaries = new Set()
for (let boundary = 0; boundary <= count; boundary += 1) {
boundaries.add(boundary)
}
const started = performance.now()
for (let creditedEndSu = 1; creditedEndSu <= count; creditedEndSu += 1) {
for (const boundary of boundaries) {
if (boundary >= creditedEndSu) {
break
}
boundaries.delete(boundary)
}
}
return performance.now() - started
}
// Shipped cleanup: a monotone cursor over ascending boundaries, so no boundary is ever revisited.
function cursorDrain(count) {
const boundaries = new PtySourceSentBoundaries(0)
for (let boundary = 1; boundary <= count; boundary += 1) {
boundaries.add(boundary)
}
const started = performance.now()
for (let creditedEndSu = 1; creditedEndSu <= count; creditedEndSu += 1) {
boundaries.dropBelow(creditedEndSu)
}
return performance.now() - started
}
function medianMs(drain, count) {
const samples = Array.from({ length: REPETITIONS }, () => drain(count)).sort((a, b) => a - b)
return samples[(samples.length - 1) >> 1]
}
function report(count) {
const fullScanMs = medianMs(fullScanDrain, count)
const earlyBreakMs = medianMs(earlyBreakDrain, count)
const cursorMs = medianMs(cursorDrain, count)
console.log(
`${String(count).padStart(6)} boundaries ` +
`full-scan ${fullScanMs.toFixed(2)}ms ` +
`early-break ${earlyBreakMs.toFixed(2)}ms ` +
`cursor ${cursorMs.toFixed(3)}ms ` +
`(cursor is ${(fullScanMs / cursorMs).toFixed(0)}x faster than full-scan, ` +
`${(earlyBreakMs / cursorMs).toFixed(0)}x faster than early-break)`
)
}
// Warm the three drains up first so the smallest row is not dominated by JIT tiering.
fullScanDrain(256)
earlyBreakDrain(256)
cursorDrain(256)
for (const count of BOUNDARY_COUNTS) {
report(count)
}