Skip to content

perf(codegen): optimize Rust and TypeScript union codecs - #402

Merged
JasonXuDeveloper merged 5 commits into
mainfrom
codex/runtime-union-codecs
Nov 18, 2026
Merged

JasonXuDeveloper merged 5 commits into
mainfrom
codex/runtime-union-codecs

Conversation

@JasonXuDeveloper

@JasonXuDeveloper JasonXuDeveloper commented Nov 18, 2026 •

Copy link
Copy Markdown
Owner

Changes

  • Have generated Rust readers construct the union variant directly. Record deferred trails only for cases that can open deferred bodies, and keep depth accounting around each non-null case.
  • Reserve the type ID and fixed-size payload together for generated TypeScript union writes. Reuse the existing offset writers and padding policy.

Verification

  • SDK 11.0.100 Codegen and integration tests passed (98 and 16 tests). Regeneration is clean.
  • Rust fmt, Clippy and the generated corpus, truncation and mutational fuzz suite passed. Two short cargo-fuzz runs completed for the hand-written and generated corpus readers.
  • TypeScript's three test modes each passed 830 tests with zero failures.
  • Runner A/B lowered Rust's median UnionArray / TaggedUnionArray deserialize time by 38% / 51% on EPYC 9V74, 43% / 55% on Xeon 6975P-C and 33% / 44% on Neoverse-N2. The Rust serialization controls stayed on par.
  • The same run lowered TypeScript's median UnionArray / TaggedUnionArray serialize time by 17% / 22% on Neoverse-N2 and 10–14% / 17–23% across the sampled x64 CPUs. The Arm64 plain union measured 37.1 µs against JSON's 38.8 µs, within 5%.

Go value wrappers escape when returned through the union interface. Returning pointers would change the concrete dynamic types callers can assert. The Go allocation item remains open.

Refs #387


Generated by Codex

The generated value wrappers escape when the decoder returns an interface. Keep the dynamic type available to callers that assert a concrete value case. Capture the escape-analysis command to distinguish boxing from the direct pointer cases.

Refs #387
Return the new enum from its read and use the default read_into implementation to replace the old value. Track deferred class bodies only in cases that can introduce them, while retaining the union depth check on every nonempty value.

Refs #387

Performance: Generated Rust codecs read union cases directly and record deferred bodies only for cases that can contain them.
Record the runner evidence and the nesting and deferral invariants for the generated union path.
Combine the type ID with a fixed-size payload in a single reservation. Emit the payload through the existing offset writers, including zeroing when the shape has padding.

Refs #387

Performance: Generated TypeScript unions reserve the type ID and fixed-size payload together.
Record the Node runner evidence for folding a union tag and its fixed payload into one reservation.
@JasonXuDeveloper JasonXuDeveloper changed the title perf(rust): construct generated union variants directly perf(codegen): optimize Rust and TypeScript union codecs Nov 18, 2026
@JasonXuDeveloper
JasonXuDeveloper merged commit 8625e06 into main Nov 18, 2026
35 checks passed
@JasonXuDeveloper
JasonXuDeveloper deleted the codex/runtime-union-codecs branch November 18, 2026 23:14
JasonXuDeveloper added a commit that referenced this pull request Nov 19, 2026
**Merge #418 first.** Both changes regenerate the same Go shapes
fixture. This branch includes #418 and the combined regeneration. The
eight measured Go union codec functions are identical to the A/B
candidate.

## 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](https://github.com/JasonXuDeveloper/Nino/actions/runs/44458051073)
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](https://github.com/JasonXuDeveloper/Nino/actions/runs/44458051073)

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](https://github.com/JasonXuDeveloper/Nino/actions/runs/44451141322)

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

- [Runner
A/B](https://github.com/JasonXuDeveloper/Nino/actions/runs/44458051073)
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](https://github.com/JasonXuDeveloper/Nino/actions/runs/44333395182)

Closes #387

---
_Generated by [Codex](https://openai.com/codex/)_
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.

1 participant