Skip to content

perf: finish a node from a value on the frame, not through closures allocated per body #2086

In progress

Description

@OmarAlJarrah

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.keepingUnread

pprof -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.

Related: #1996, #1991

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

  1. added
    type:chore
    performance
    priority:P4
    on Nov 18, 2026
  2. OmarAlJarrah commented on Nov 29, 2026

    @OmarAlJarrah
    MemberAuthor

    #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, through engine) at 7d7208585 against 1cf9539a7:

    7d7208585 1cf9539a7
    flat objects, schemaBody + keepingUnread (now finishUnread) 304.5k–312.7k 0.6k–0.9k
    flat space, same functions 43.2–44.2 MB 0.15–0.19 MB
    schemaBody cumulative 5.14–5.21M objects, 682–686 MB 4.91M objects, 619–620 MB
    whole compile 35.98–36.24M objects, 5.606–5.619 GB 35.78–35.94M objects, 5.544–5.555 GB

    IR and diagnostics are byte-identical between the two on the golden spec but for the version stamp.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    performancepriority:P4type:chore

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions