Skip to content

perf(codegen)!: reduce union case allocations - #420

Merged
JasonXuDeveloper merged 12 commits into
mainfrom
codex/go-union-allocation
Nov 19, 2026
Merged

JasonXuDeveloper merged 12 commits into
mainfrom
codex/go-union-allocation

Conversation

@JasonXuDeveloper

@JasonXuDeveloper JasonXuDeveloper commented Nov 19, 2026 •

Copy link
Copy Markdown
Owner

Removes the allocations C# union cases cost in generated Go and TypeScript (#387). Speed comes first here: breaking the generated source API is accepted where it buys real speed, and the bytes stay the same in every language. #418, which regenerates the same Go fixtures, is merged; this branch includes main.

Go

Use pointers for generated cases that would allocate an interface box. Keep direct string and static small cases as values. Decode collections into bounded, typed blocks per pointer case. Each decode owns its storage and the published case IDs and payload bytes are preserved.

Go source API

Regenerate the bindings and use pointer literals and assertions for the affected cases, for example &ChoiceInt32{Value: 42} and case *ChoiceInt32.

Evidence

  • Both official union payloads round-trip byte for byte. The linked benchmark readers on amd64 and Arm64 have zero of the five previous interface boxing calls.
  • Runner A/B measured 808 → 77 allocations for UnionArray and 582 → 43 for TaggedUnionArray on both architectures. Read medians fell 12–15% and 9–20% on x64, and 22% and 16% on Neoverse-N2. Allocation bytes fell 2.2% and 0.5%. The Xeon 6975P-C tagged write point estimate rose 5.2% with 10% baseline variation and p=0.065. The other write medians stayed within 5%.
  • .NET SDK 11.0.100 Codegen and Integration passed 99 and 16 tests. The Go race and required corpus suites, Go 1.22 vet, four fuzz targets, and QEMU 386, Arm32 and s390x passed. New cases exercise pointer lifetime, typed nil, recursive unions, map/grid storage and tracked cycles.

Rust

Retain the enum with a boxed class case. Inlining the measured class payload removed a box per class case and increased the 64-bit enum stride from 24 to 40 bytes. The end-to-end read took 31–33% more time on sampled x64 AMD CPUs and 36% more on Neoverse-N2. Inline-class runner comparison

The merged Rust reader from #402 already leads postcard and bincode on both official union cases on all sampled x64 and Arm64 CPUs. Same-run competitor comparison

TypeScript

Use a direct value for the first case of each runtime type test and keep the tag for additional ambiguous cases. Equal values from different numeric cases keep distinct wire IDs. The official 1,000-element tagged payload reads back with 333 tagged case objects. Regenerate bindings and handle direct values in consumers that inspect case tags.

Narrowing

A direct case has no case field, so switch (m.case) alone no longer narrows every case. Narrow a direct case by its runtime test and the tagged ones by case (typeof m === "number", then Array.isArray(m), then switch (m.case)). The generated type comment now lists each direct case with its test, and docs/codegen.md explains it. A discriminant on every value would mean a wrapper per number, string or array, which is the allocation this change removes, and a generated class cannot carry one because one class can be a case of several unions.

  • Runner A/B reduced tagged write medians by 14–16% on sampled x64 CPUs and 20% on Neoverse-N2. Tagged reads took about 12% less time on the sampled AMD CPUs and 17% less on Neoverse-N2, with the Xeon case on par. The plain union stayed on par.
  • Both official payloads round-trip byte for byte. V8 emits 124 and 235 bytes of bytecode for the generated tagged reader and writer and inlines them into the collection loops. Node, browser-mode and Wasm-disabled tests passed.

The existing plain union already uses direct case values, and its writer was on par with JSON on Neoverse-N2. #402 runner comparison

Review

  • Bytes: the conformance corpus (--verify: 141 valid, 230 malicious, 43 truncated, 0 failures) and the generated-code tests in Go (race), Rust (NINO_CORPUS) and TypeScript (Node, browser mode, no Wasm) pass. The Go and Rust benchmark models regenerate unchanged.
  • Hostile input: Go slab blocks are charged to the reader budget through SliceCap and always hold at least one slot. A case read as null still fails, and depth is counted around nesting cases as before. Tracked class cases bypass the slabs.
  • Local probes (not kept, recorded in optimize-runtimes): an index-advancing slab next with an out-of-line growth path was on par, and larger blocks (256/64) were 4-7 % slower. GC and string reads dominate the remaining Go read profile.

Closes #387


Generated by Claude Code

Give each union sequence small typed blocks for untracked concrete class cases. Allocate a slot after its presence check and charge the block to the reader budget. Keep the concrete pointer types and fresh-value semantics for reads into an existing sequence.

Refs #387

Performance: On the sampled GitHub x64 and Arm64 runners, reading 1,000 mixed Go union values took 636 allocations, down from 808.
Document the lifetime of generated typed blocks and the allocation and throughput results of the value-wrapper experiment.
Use concrete pointers for cases that would allocate a box in an interface. Each collection batches those case allocations in typed blocks limited by the reader budget. Keep direct string and static small cases as values and preserve the case IDs and payload bytes.

Refs #387

BREAKING CHANGE: Regenerate Go bindings and use pointers in literals and type switches for cases that used to box their values, such as &ChoiceInt32{Value: 42} and case *ChoiceInt32.
Performance: On sampled x64 and Arm64 GitHub runners, reading 1,000 Go union values dropped from 808 to 77 allocations, and reading 1,000 tagged values dropped from 582 to 43. The two reads took 9–22% less time.
Explain when a value case uses direct or static storage and record the failed reflection experiment alongside the typed allocation approach.
Record the allocation counts and linked boxing checks for both official union workloads on both architectures.
Keep the measured class representation after the inline payload reduced allocations but increased the cost of reading the complete enum array.
Use one direct case per runtime test and retain tags on cases that share its test or have no test. The first case in declaration order is the direct case, so two equal values of different cases keep distinct wire IDs.

Refs #387

Added: Generated TypeScript unions use direct values for the first case of each runtime test and tagged values for additional ambiguous cases. Regenerate consumers that used tags for every case.
Performance: On sampled GitHub runs, TypeScript tagged union writes took 14–16% less time on x64 and 20% less on Neoverse-N2. The ordinary union cases were on par.
Document declaration-order precedence, the verified V8 collection inlining and runner evidence for removing identifiable wrapping objects.
@JasonXuDeveloper JasonXuDeveloper changed the title perf(codegen): allocate Go union classes in typed blocks perf(codegen)!: reduce union case allocations Nov 19, 2026
…inters

Keep #418 as a separate prerequisite and reconcile the shared generated shapes against both templates. The eight measured Go union functions reproduce the A/B source byte for byte.

Copy link
Copy Markdown
Owner Author

Held for the owner's decision rather than merged. CI on 55da712 is green and the branch still merges cleanly with main now that #418 has landed. The bytes do not change: the union case ids and payloads stay the same.

What breaks is the generated source API:

If this lands:

  • The TypeScript commit 40e42cc is marked ! but has no BREAKING CHANGE: footer, and it uses an Added: trailer to describe a break.
  • 6b53138's Performance: trailer ("636 allocations, down from 808") is superseded by 1240974's 808 → 77. Both would show in the release notes. A git note on release-notes can drop the first one.
  • The nino.docs pages (advanced/typescript, advanced/go, advanced/codegen #unions, and the changelog) still describe the old forms.

Generated by Claude Code

A union with direct and tagged cases no longer narrows every case through switch (m.case). Its generated comment
now lists each direct case with the runtime test that identifies it, and docs/codegen.md says to narrow a direct
case by that test and a tagged one by case: a tag on every value would cost an object per value.

Refs #387
…owing choice

An index-advancing slab next measured on par and larger blocks slower, so nobody retries them blind; the
TypeScript note says why direct cases keep no tag.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Close the C# union gaps: Rust deserialize, TypeScript plain-union serialize on Arm64, Go allocations

1 participant