Repository navigation
Description
Problem
Lowering a schema body allocates the machinery that finishes the node it builds whether or not the body builds one. With #1996 (at 6630476), a memory profile of one compile of the Cloudflare schema (cloudflare/api-schemas@17d9fe00, 34,121,283 objects allocated in all) attributes 301,609 objects, 0.88%, flat to two functions:
$ go tool pprof -sample_index=alloc_objects -top -nodecount=6000 allocs.test allocs.prof | grep -E 'schema\.(schemaBody|keepingUnread)$'
154494 0.45% ... schema.schemaBody
147115 0.43% ... schema.keepingUnreadpprof -list puts them on four lines: var kept finished (74,611) and the closure passed to build (72,212), both in keepingUnread (unread.go); the &declaring{...} literal (74,838) and the call passing decl.finish and the closure principalResidue returns (78,719), both in schemaBody (schema.go).
The profile came from a throwaway program that calls openapi.New().Compile on the file with runtime.MemProfileRate = 4096, then runtime.GC() and pprof.Lookup("allocs") (a profile written without the GC omits the lowering's allocations: the allocs profile lags by a collection).
Per body: a declaring struct, the method value decl.finish, the closure principalResidue returns, and in keepingUnread a finished and a closure over it. Nothing in the IR asks for any of them: they carry facts the frame already holds to the one place that writes the node.
Cause
finisher (schema/hoist.go) is a func(td ir.TypeDef, read keySet) chained with then, and finished.on/onRead wrap writers so a frame can learn that one ran. Each frame therefore allocates a closure to hand its facts down.
Direction
A draft. Pass the declaration to the finishing step as a value built on the frame's stack (finish(td, declaration{...})), replacing the chain of finisher closures, which also removes finisher.then and finished.on. Measure by the same profile with declaring passed by value at the call that interns the node; keep it only if the figure above falls. A 0.88% gain is small, so the change earns its place only if it also reads simpler.
Out of scope
Allocation outside these two functions (the parse and the speakeasy-api model dominate the profile). Any change to when a node is finished or to the findings' order.
Priority: P4, rule 1 (an allocation change that alters no lowering, validation, IR or emit-contract behaviour; 0.88% of allocations by the profile above).
Activity
OmarAlJarrah commentedon Nov 29, 2026
#2354 settles this at
c852e0988: a build returns what it made (builtPosition) up the lowering's frames, so no finisher closure is allocated per body, and the issue closes at the pull request's merge.The golden compile's allocation profile (
cloudflare/api-schemas@904c5434,runtime.MemProfileRate = 4096, three runs each, throughengine) at7d7208585against1cf9539a7:7d72085851cf9539a7schemaBody+keepingUnread(nowfinishUnread)schemaBodycumulativeIR and diagnostics are byte-identical between the two on the golden spec but for the version stamp.