Repository navigation
perf(main): stop a module-level Error from pinning load-time code and buffers - #27335
outof-place wants to merge 1 commit into
Conversation
… buffers journal-item-appender created its "resolved to nothing" sentinel as a module-level Error. V8 keeps an Error's captured stack frames, with their functions, until something reads .stack, and nothing ever did. That Error was created while index.js loaded, so its frames held: - the bundle's top-level function, and with it 5.2 MiB of compiled code and 0.9 MiB of bytecode that would otherwise be dropped after load; - Electron's loader closure, whose context holds the 9.1 MiB Buffer index.js was read into. The appender now throws a fresh NothingResolvedError per null resolve and checks it with instanceof. A heap snapshot of an idle --serve main no longer has the large code object or the Buffer; its footprint is 6.4 MiB lower at idle and 7.6-11.1 MiB lower after a full GC. Co-Authored-By: Claude Opus 5.5 <[email protected]>
|
No actionable comments were generated in the recent review. 🎉 ℹ️ Recent review info
📝 Walkthrough
Priority: ➖ Normal Merge Risk: ⚪ Minimal · up to Null resolutions remain no-ops and later journal writes proceed; no concrete merge-blocking risk is established. Pre-merge checks |
|
|
Superseded by #27342, which credits @outof-place and adopts the focused fresh-error fix with regression coverage. Independent real Electron heap snapshots show 15.19 MiB of retained startup code/bytecode/read-buffer allocations become collectible; no RSS claim. The replacement preserves journal outcomes and passes focused tests, node typecheck and quality checks. Closing this original in favor of the reviewed replacement. |
Credit to outof-place for identifying the retention and proposing the focused fix in #27335. Independently validated 15.19 MiB of collectible startup allocations. Co-authored-by: outof-place <[email protected]>
) Credit to outof-place for identifying the retention and proposing the focused fix in #27335. Independently validated 15.19 MiB of collectible startup allocations. Co-authored-by: outof-place <[email protected]>
ELI5
When JavaScript creates an
Error, V8 remembers the call stack it was created in, including the functions on it, until someone readserror.stack. One file in main creates anErroronce, while the bundle loads, and keeps it forever as a marker. Nobody ever reads its stack. So that one marker kept the whole bundle's start-up function and the 9 MiB buffer the bundle was read from alive for the life of the app. This PR creates the error only when it's thrown, so the start-up leftovers can be freed.What Changed
Before:
journal-item-appender.tshadconst NOTHING_RESOLVED = new Error('journal_item_resolved_to_nothing')at module level, thrown and compared by identity. In a heap snapshot of an idle--servemain process, that Error's stack frames retain:Bufferindex.js was read into.After: the appender throws
new NothingResolvedError(...)when a resolve finds nothing, and checksinstanceof NothingResolvedError. A heap snapshot of the same idle process has neither the large code object nor the 9.1 MiB Buffer, and nothing retains the top-level function. An idle main process uses about 6 MiB less memory, and about 9 MiB less after a full GC.Behavior is unchanged: a resolve that returns null still writes no row and resolves
null; any other error still propagates.Why
Alternatives considered:
NOTHING_RESOLVED.stackonce at load. V8 then keeps the formatted string and drops the frames. It works, but it's a side effect a reader would delete as dead code.catchcode that expects anErrorwould mishandle it.I looked for other holders: in the heap snapshot after this change, nothing retains the top-level function. A search for module-level
const X = new Error(...),AbortSignal.abort()ornew DOMException(...)in src/main and src/shared finds none outside test harnesses.Linked Issue
None. Found while measuring the main process's memory with no windows open.
Visual Proof
N/A: no UI or behavior change.
Testing
New test in
journal-write-queue.test.ts: a resolved append that finds nothing resolvesnull, writes no row, and the next write lands. It passes before and after this change: it pins the behavior the change must keep, not the memory effect.pnpm test journal-write-queue.test.ts structured-agent-session-event-sink.test.ts,pnpm tc:node,oxlinton the changed files andpnpm run check:code-quality:changedpass.Heap snapshots of an idle
--servemain process (pnpm run build:electron-vite, private profile, 45 s after start), before and after:InstructionStreamis 5.2 MiB; its only retainer path is this Error's stack frames. A 9.1 MiBJSArrayBufferDatais retained through the same frames, via the loader closure'stranslateContext.sourceBuffer.InstructionStreamis 176 KB.Main-process footprint A/B: see Memory under Trade-offs.
I manually tested these changes locally (not done: only the builds, heap snapshots, A/B runs and checks above ran)
Automated tests added/updated, or explained why not below
AI Disclosure
Implemented with Claude Opus 5.5 via Claude Code: investigation, code, tests and measurements. A human (outof-place) directed the work.
Review
journal-item-appender.ts: the error class and theinstanceofcheck.journal-write-queue.test.ts.Agent skill upstream boundary
Trade-offs
instanceof. The class is module-private, so nothing else can throw it, and every other error still propagates. The new test covers the null path.phys_footprintof an idle--servehost, private profile, macOS arm64:The heap snapshots show 15.2 MiB becoming unreachable: 5.2 MiB code, 0.9 MiB bytecode and the 9.1 MiB read buffer. How much of that leaves the footprint, and when, depends on V8's next full GC and on how much freed heap it keeps for reuse.
Notes
Error,DOMExceptionorAbortSignal.abort()created at module level in main. All of them capture a stack during load, and with it the bundle's top-level frame.Checklist
N/Awith reasonpnpm lint,pnpm typecheck,pnpm test, andpnpm buildpass (or CI will cover; local preferred)🤖 Generated with Claude Code