From f0f4b6d25f047e891367a0e48de42cd0c9dcae2e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?JasonXuDeveloper=20-=20=E5=82=91?= Date: Thu, 19 Nov 2026 01:48:38 +1100 Subject: [PATCH 1/5] docs(skills): record Go union boxing constraints 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 --- .claude/skills/optimize-runtimes/references/dispatch.md | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/.claude/skills/optimize-runtimes/references/dispatch.md b/.claude/skills/optimize-runtimes/references/dispatch.md index 59cd3b636..88c8737a9 100644 --- a/.claude/skills/optimize-runtimes/references/dispatch.md +++ b/.claude/skills/optimize-runtimes/references/dispatch.md @@ -63,3 +63,8 @@ call, a helper that takes a function, or a `read_into` / `deserializeInto` path. and the tagged union is the faster form (12.1 µs serialize, 7.5 deserialize against 23.1 and 13.0 on EPYC 9V45, with fewer and smaller values); Go deserializes in about three times its serialize time (35.7 against 11.9 µs, EPYC 9V74) with an allocation per non-int case (808 for UnionArray). The gaps are #387. +- Treat the dynamic type of a generated Go union case as part of its API: callers assert the concrete value wrapper. + `go build -gcflags=-m ./model` in the Go bench module reports heap escape for `UnionValueInt32`, + `UnionValueInt64`, `UnionValueUnionPoint`, `UnionTaggedInt32` and `UnionTaggedInt16`. Go's small-integer + conversion table can still supply some of those boxes. The string wrapper has a single pointer field and fits + directly in an interface. Returning pointers into a typed slice would change what callers' type assertions accept. From 64e4ef4318c1322c6a8d81ced4765008caa3581b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?JasonXuDeveloper=20-=20=E5=82=91?= Date: Thu, 19 Nov 2026 01:57:08 +1100 Subject: [PATCH 2/5] perf(rust): construct generated union variants directly 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. --- .../src/Nino.Codegen/Templates/rust/types.sbn | 31 ++++++++++------ runtimes/rust/bench/src/model.rs | 28 +++++---------- runtimes/rust/tests/generated/nino_types.rs | 36 ++++++++----------- runtimes/rust/tests/generated/shapes.rs | 34 +++++++----------- 4 files changed, 54 insertions(+), 75 deletions(-) diff --git a/runtimes/csharp/src/Nino.Codegen/Templates/rust/types.sbn b/runtimes/csharp/src/Nino.Codegen/Templates/rust/types.sbn index 22da9ae52..66bfeed4a 100644 --- a/runtimes/csharp/src/Nino.Codegen/Templates/rust/types.sbn +++ b/runtimes/csharp/src/Nino.Codegen/Templates/rust/types.sbn @@ -263,14 +263,25 @@ func union_rs(t) end $o = $o + " fn read_case(r: &mut nino::Reader<'_>, id: i32) -> nino::Result {\n Ok(match id {\n" for $k in t.cases + $defer = t.model.union_nests && !$k.model.leaf + if !$k.model.nullable && !$defer + $o = $o + " " + $k.model.type_id + " => Self::" + $k.name + "(r.read()?),\n" + continue + end + $o = $o + " " + $k.model.type_id + " => {\n" + if $defer + # A primitive, raw value or string cannot introduce a deferred class body. + $o = $o + " let mark = r.defer_mark();\n" + end + $o = $o + " let value: " + rtype($k.shape) + " = r.read()?;\n" if $k.model.nullable # A case read as None is invalid: .NET writes the empty union for a case given null. - $o = $o + " " + $k.model.type_id + " => {\n let value: " + rtype($k.shape) + " = r.read()?;\n" $o = $o + " if value.is_none() {\n return Err(nino::Error::new(nino::ErrorKind::UnexpectedNull, id));\n }\n" - $o = $o + " Self::" + $k.name + "(value)\n }\n" - else - $o = $o + " " + $k.model.type_id + " => Self::" + $k.name + "(r.read()?),\n" end + if $defer + $o = $o + " r.defer_trail(mark, nino::Step::Field(|p| p.downcast_mut::<" + $n + ">()?.case_value()));\n" + end + $o = $o + " Self::" + $k.name + "(value)\n }\n" end $o = $o + " _ => return Err(nino::Error::new(nino::ErrorKind::UnknownTypeId, id)),\n })\n }\n}\n" $o = $o + "impl NinoRead for " + $n + " {\n" @@ -278,17 +289,15 @@ func union_rs(t) $o = $o + " const MAY_DEFER: bool = true;\n" end $o = $o + " const MIN_SIZE: usize = 4;\n" - $o = $o + " fn read(r: &mut nino::Reader<'_>) -> nino::Result {\n let mut v = Self::Empty;\n v.read_into(r)?;\n Ok(v)\n }\n" - $o = $o + " fn read_into(&mut self, r: &mut nino::Reader<'_>) -> nino::Result<()> {\n let id = r.i32()?;\n" - $o = $o + " if id == 0 {\n *self = Self::Empty;\n return Ok(());\n }\n" + $o = $o + " #[inline]\n fn read(r: &mut nino::Reader<'_>) -> nino::Result {\n let id = r.i32()?;\n" + $o = $o + " if id == 0 {\n return Ok(Self::Empty);\n }\n" if t.model.union_nests $o = $o + " // A case can hold the union again: each level counts toward the depth limit.\n r.enter_struct()?;\n" - $o = $o + " let mark = r.defer_mark();\n let value = Self::read_case(r, id);\n r.exit_struct();\n *self = value?;\n" - $o = $o + " r.defer_trail(mark, nino::Step::Field(|p| p.downcast_mut::<" + $n + ">()?.case_value()));\n" + $o = $o + " let value = Self::read_case(r, id);\n r.exit_struct();\n value\n" else - $o = $o + " *self = Self::read_case(r, id)?;\n" + $o = $o + " Self::read_case(r, id)\n" end - $o = $o + " Ok(())\n }\n}\n" + $o = $o + " }\n}\n" $o = $o + "impl NinoWrite for " + $n + " {\n fn write<'a>(&'a self, w: &mut nino::Writer<'a>) -> nino::Result<()> {\n match self {\n" for $k in t.cases $o = $o + " Self::" + $k.name + "(v)" + ($k.model.nullable ? " if v.is_some()" : "") + " => {\n" diff --git a/runtimes/rust/bench/src/model.rs b/runtimes/rust/bench/src/model.rs index 0de0c16e8..a2b1888b4 100644 --- a/runtimes/rust/bench/src/model.rs +++ b/runtimes/rust/bench/src/model.rs @@ -9048,19 +9048,13 @@ impl UnionTagged { } impl NinoRead for UnionTagged { const MIN_SIZE: usize = 4; + #[inline] fn read(r: &mut nino::Reader<'_>) -> nino::Result { - let mut v = Self::Empty; - v.read_into(r)?; - Ok(v) - } - fn read_into(&mut self, r: &mut nino::Reader<'_>) -> nino::Result<()> { let id = r.i32()?; if id == 0 { - *self = Self::Empty; - return Ok(()); + return Ok(Self::Empty); } - *self = Self::read_case(r, id)?; - Ok(()) + Self::read_case(r, id) } } impl NinoWrite for UnionTagged { @@ -9132,10 +9126,12 @@ impl UnionValue { } 791437675 => Self::UnionPoint(r.read()?), 635318394 => { + let mark = r.defer_mark(); let value: Option> = r.read()?; if value.is_none() { return Err(nino::Error::new(nino::ErrorKind::UnexpectedNull, id)); } + r.defer_trail(mark, nino::Step::Field(|p| p.downcast_mut::()?.case_value())); Self::UnionItem(value) } _ => return Err(nino::Error::new(nino::ErrorKind::UnknownTypeId, id)), @@ -9145,25 +9141,17 @@ impl UnionValue { impl NinoRead for UnionValue { const MAY_DEFER: bool = true; const MIN_SIZE: usize = 4; + #[inline] fn read(r: &mut nino::Reader<'_>) -> nino::Result { - let mut v = Self::Empty; - v.read_into(r)?; - Ok(v) - } - fn read_into(&mut self, r: &mut nino::Reader<'_>) -> nino::Result<()> { let id = r.i32()?; if id == 0 { - *self = Self::Empty; - return Ok(()); + return Ok(Self::Empty); } // A case can hold the union again: each level counts toward the depth limit. r.enter_struct()?; - let mark = r.defer_mark(); let value = Self::read_case(r, id); r.exit_struct(); - *self = value?; - r.defer_trail(mark, nino::Step::Field(|p| p.downcast_mut::()?.case_value())); - Ok(()) + value } } impl NinoWrite for UnionValue { diff --git a/runtimes/rust/tests/generated/nino_types.rs b/runtimes/rust/tests/generated/nino_types.rs index 69b65b545..26d2dedbb 100644 --- a/runtimes/rust/tests/generated/nino_types.rs +++ b/runtimes/rust/tests/generated/nino_types.rs @@ -2279,24 +2279,30 @@ impl Choice { Self::String(value) } -1773443910 => { + let mark = r.defer_mark(); let value: Option> = r.read()?; if value.is_none() { return Err(nino::Error::new(nino::ErrorKind::UnexpectedNull, id)); } + r.defer_trail(mark, nino::Step::Field(|p| p.downcast_mut::()?.case_value())); Self::Point(value) } -749123728 => { + let mark = r.defer_mark(); let value: Option = r.read()?; if value.is_none() { return Err(nino::Error::new(nino::ErrorKind::UnexpectedNull, id)); } + r.defer_trail(mark, nino::Step::Field(|p| p.downcast_mut::()?.case_value())); Self::Animal(value) } -217079013 => { + let mark = r.defer_mark(); let value: Option> = r.read()?; if value.is_none() { return Err(nino::Error::new(nino::ErrorKind::UnexpectedNull, id)); } + r.defer_trail(mark, nino::Step::Field(|p| p.downcast_mut::()?.case_value())); Self::ListInt32(value) } 1279255967 => { @@ -2313,25 +2319,17 @@ impl Choice { impl NinoRead for Choice { const MAY_DEFER: bool = true; const MIN_SIZE: usize = 4; + #[inline] fn read(r: &mut nino::Reader<'_>) -> nino::Result { - let mut v = Self::Empty; - v.read_into(r)?; - Ok(v) - } - fn read_into(&mut self, r: &mut nino::Reader<'_>) -> nino::Result<()> { let id = r.i32()?; if id == 0 { - *self = Self::Empty; - return Ok(()); + return Ok(Self::Empty); } // A case can hold the union again: each level counts toward the depth limit. r.enter_struct()?; - let mark = r.defer_mark(); let value = Self::read_case(r, id); r.exit_struct(); - *self = value?; - r.defer_trail(mark, nino::Step::Field(|p| p.downcast_mut::()?.case_value())); - Ok(()) + value } } impl NinoWrite for Choice { @@ -6654,10 +6652,12 @@ impl UnionTree { fn read_case(r: &mut nino::Reader<'_>, id: i32) -> nino::Result { Ok(match id { 1950069485 => { + let mark = r.defer_mark(); let value: Option> = r.read()?; if value.is_none() { return Err(nino::Error::new(nino::ErrorKind::UnexpectedNull, id)); } + r.defer_trail(mark, nino::Step::Field(|p| p.downcast_mut::()?.case_value())); Self::UnionTreeArray(value) } 337757159 => Self::Int64(r.read()?), @@ -6668,25 +6668,17 @@ impl UnionTree { impl NinoRead for UnionTree { const MAY_DEFER: bool = true; const MIN_SIZE: usize = 4; + #[inline] fn read(r: &mut nino::Reader<'_>) -> nino::Result { - let mut v = Self::Empty; - v.read_into(r)?; - Ok(v) - } - fn read_into(&mut self, r: &mut nino::Reader<'_>) -> nino::Result<()> { let id = r.i32()?; if id == 0 { - *self = Self::Empty; - return Ok(()); + return Ok(Self::Empty); } // A case can hold the union again: each level counts toward the depth limit. r.enter_struct()?; - let mark = r.defer_mark(); let value = Self::read_case(r, id); r.exit_struct(); - *self = value?; - r.defer_trail(mark, nino::Step::Field(|p| p.downcast_mut::()?.case_value())); - Ok(()) + value } } impl NinoWrite for UnionTree { diff --git a/runtimes/rust/tests/generated/shapes.rs b/runtimes/rust/tests/generated/shapes.rs index 12d076696..86a3d089d 100644 --- a/runtimes/rust/tests/generated/shapes.rs +++ b/runtimes/rust/tests/generated/shapes.rs @@ -1140,17 +1140,21 @@ impl Measure { 546808484 => Self::Int32(r.read()?), -1251563749 => Self::Double(r.read()?), -217079013 => { + let mark = r.defer_mark(); let value: Option> = r.read()?; if value.is_none() { return Err(nino::Error::new(nino::ErrorKind::UnexpectedNull, id)); } + r.defer_trail(mark, nino::Step::Field(|p| p.downcast_mut::()?.case_value())); Self::ListInt32(value) } -1803246716 => { + let mark = r.defer_mark(); let value: Option>> = r.read()?; if value.is_none() { return Err(nino::Error::new(nino::ErrorKind::UnexpectedNull, id)); } + r.defer_trail(mark, nino::Step::Field(|p| p.downcast_mut::()?.case_value())); Self::ListString(value) } _ => return Err(nino::Error::new(nino::ErrorKind::UnknownTypeId, id)), @@ -1160,25 +1164,17 @@ impl Measure { impl NinoRead for Measure { const MAY_DEFER: bool = true; const MIN_SIZE: usize = 4; + #[inline] fn read(r: &mut nino::Reader<'_>) -> nino::Result { - let mut v = Self::Empty; - v.read_into(r)?; - Ok(v) - } - fn read_into(&mut self, r: &mut nino::Reader<'_>) -> nino::Result<()> { let id = r.i32()?; if id == 0 { - *self = Self::Empty; - return Ok(()); + return Ok(Self::Empty); } // A case can hold the union again: each level counts toward the depth limit. r.enter_struct()?; - let mark = r.defer_mark(); let value = Self::read_case(r, id); r.exit_struct(); - *self = value?; - r.defer_trail(mark, nino::Step::Field(|p| p.downcast_mut::()?.case_value())); - Ok(()) + value } } impl NinoWrite for Measure { @@ -2058,10 +2054,12 @@ impl Pick { fn read_case(r: &mut nino::Reader<'_>, id: i32) -> nino::Result { Ok(match id { 334101194 => { + let mark = r.defer_mark(); let value: Option> = r.read()?; if value.is_none() { return Err(nino::Error::new(nino::ErrorKind::UnexpectedNull, id)); } + r.defer_trail(mark, nino::Step::Field(|p| p.downcast_mut::()?.case_value())); Self::EmptyCase(value) } -2134742284 => Self::PickPair(r.read()?), @@ -2073,25 +2071,17 @@ impl Pick { impl NinoRead for Pick { const MAY_DEFER: bool = true; const MIN_SIZE: usize = 4; + #[inline] fn read(r: &mut nino::Reader<'_>) -> nino::Result { - let mut v = Self::Empty; - v.read_into(r)?; - Ok(v) - } - fn read_into(&mut self, r: &mut nino::Reader<'_>) -> nino::Result<()> { let id = r.i32()?; if id == 0 { - *self = Self::Empty; - return Ok(()); + return Ok(Self::Empty); } // A case can hold the union again: each level counts toward the depth limit. r.enter_struct()?; - let mark = r.defer_mark(); let value = Self::read_case(r, id); r.exit_struct(); - *self = value?; - r.defer_trail(mark, nino::Step::Field(|p| p.downcast_mut::()?.case_value())); - Ok(()) + value } } impl NinoWrite for Pick { From d78b209105d3a97ebaf72adde2628f0535d997b2 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?JasonXuDeveloper=20-=20=E5=82=91?= Date: Thu, 19 Nov 2026 02:28:08 +1100 Subject: [PATCH 3/5] docs(skills): record Rust union read results Record the runner evidence and the nesting and deferral invariants for the generated union path. --- .claude/skills/optimize-runtimes/references/dispatch.md | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/.claude/skills/optimize-runtimes/references/dispatch.md b/.claude/skills/optimize-runtimes/references/dispatch.md index 88c8737a9..ab1c2231e 100644 --- a/.claude/skills/optimize-runtimes/references/dispatch.md +++ b/.claude/skills/optimize-runtimes/references/dispatch.md @@ -63,6 +63,10 @@ call, a helper that takes a function, or a `read_into` / `deserializeInto` path. and the tagged union is the faster form (12.1 µs serialize, 7.5 deserialize against 23.1 and 13.0 on EPYC 9V45, with fewer and smaller values); Go deserializes in about three times its serialize time (35.7 against 11.9 µs, EPYC 9V74) with an allocation per non-int case (808 for UnionArray). The gaps are #387. +- Construct the Rust union variant directly in its read, and record a deferred trail only for cases that can open + deferred bodies. Against 44d49866 on runtime A/B 44333395182, `UnionArray` / `TaggedUnionArray` deserialize + medians fell 38 % / 51 % on EPYC 9V74, 43 % / 55 % on Xeon 6975P-C and 33 % / 44 % on Neoverse-N2. + Both serialize controls stayed on par. Count struct depth around each non-null case, including a failed read. - Treat the dynamic type of a generated Go union case as part of its API: callers assert the concrete value wrapper. `go build -gcflags=-m ./model` in the Go bench module reports heap escape for `UnionValueInt32`, `UnionValueInt64`, `UnionValueUnionPoint`, `UnionTaggedInt32` and `UnionTaggedInt16`. Go's small-integer From 9cf0a4318a54cc461267039d62cc035c74120cb1 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?JasonXuDeveloper=20-=20=E5=82=91?= Date: Thu, 19 Nov 2026 01:50:16 +1100 Subject: [PATCH 4/5] perf(typescript): reserve fixed union cases in one block 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. --- .../Templates/typescript/types.sbn | 26 +++++++++++---- .../test/generated/Nino.Codegen.Tests.ts | 32 ++++++++++++------- .../generated/Nino.Conformance.TypedArrays.ts | 17 ++++++---- .../test/generated/Nino.Conformance.ts | 17 ++++++---- 4 files changed, 60 insertions(+), 32 deletions(-) diff --git a/runtimes/csharp/src/Nino.Codegen/Templates/typescript/types.sbn b/runtimes/csharp/src/Nino.Codegen/Templates/typescript/types.sbn index 44651d1fa..d17dff304 100644 --- a/runtimes/csharp/src/Nino.Codegen/Templates/typescript/types.sbn +++ b/runtimes/csharp/src/Nino.Codegen/Templates/typescript/types.sbn @@ -247,12 +247,26 @@ func case_ts(k) ret $t end -# Writing a union case's value, never null: a nullable's presence byte is 1. +# Writing a union case's id and value. Reserve both together if the payload size is known. func case_write(k, v) - if k.shape.kind == "nullable" - ret "w.u8(1);\n " + stmt(write(k.shape.inner, v)) + $s = k.shape + $nullable = $s.kind == "nullable" + if $nullable + $s = $s.inner end - ret stmt(write(k.shape, v)) + if $s.fixed + $size = 4 + ($nullable ? 1 : 0) + $o = "const at = " + reserve($size + $s.size, $s.zeroed) + ";\nw.view.setInt32(at, " + k.model.type_id + ", true);\n" + if $nullable + $o = $o + "w.view.setUint8(at + 4, 1);\n" + end + ret $o + stmt(write_at($s, $size, v)) + end + $o = "w.i32(" + k.model.type_id + ");\n" + if $nullable + $o = $o + "w.u8(1);\n" + end + ret $o + stmt(write($s, v)) end # A C# union (spec §7.12): the type id of its case (0, null, for the empty union), then the case's value, never null. @@ -318,12 +332,12 @@ func union_type(c) if c.tagged $o = $o + " switch (v.case) {\n" for $k in c.cases - $o = $o + " case \"" + $k.model.name + "\":\n w.i32(" + $k.model.type_id + ");\n " + case_write($k, "v.value") + "\n break;\n" + $o = $o + " case \"" + $k.model.name + "\": {\n " + (string.replace (case_write($k, "v.value")) "\n" "\n ") + "\n break;\n }\n" end $o = $o + " default:\n throw types.unwritable(v);\n }\n" else for $k in c.cases - $o = $o + (for.first ? " if (" : " else if (") + $k.test + ") {\n w.i32(" + $k.model.type_id + ");\n " + (string.replace (case_write($k, "v")) "\n " "\n ") + "\n }\n" + $o = $o + (for.first ? " if (" : " else if (") + $k.test + ") {\n " + (string.replace (case_write($k, "v")) "\n" "\n ") + "\n }\n" end $o = $o + " else throw types.unwritable(v as object);\n" end diff --git a/runtimes/typescript/test/generated/Nino.Codegen.Tests.ts b/runtimes/typescript/test/generated/Nino.Codegen.Tests.ts index a9ea7c971..be2163c83 100644 --- a/runtimes/typescript/test/generated/Nino.Codegen.Tests.ts +++ b/runtimes/typescript/test/generated/Nino.Codegen.Tests.ts @@ -1418,22 +1418,28 @@ function writeMeasure(w: NinoWriter, v: Measure | null): void { } w.enterStruct(); switch (v.case) { - case "Int32": - w.i32(546808484); - w.i32(v.value); + case "Int32": { + const at = w.reserve(8); + w.view.setInt32(at, 546808484, true); + w.view.setInt32(at + 4, v.value, true); break; - case "Double": - w.i32(-1251563749); - w.f64(v.value); + } + case "Double": { + const at = w.reserve(12); + w.view.setInt32(at, -1251563749, true); + w.view.setFloat64(at + 4, v.value, true); break; - case "ListInt32": + } + case "ListInt32": { w.i32(-217079013); write$32(w, v.value); break; - case "ListString": + } + case "ListString": { w.i32(-1803246716); write$33(w, v.value); break; + } default: throw types.unwritable(v); } @@ -1545,12 +1551,14 @@ function writePick(w: NinoWriter, v: Pick | null): void { writeEmpty(w, v); } else if (v instanceof PickPair) { - w.i32(-2134742284); - writePickPair(w, w.reserve(8), v); + const at = w.reserve(12); + w.view.setInt32(at, -2134742284, true); + writePickPair(w, at + 4, v); } else if (typeof v === "number") { - w.i32(31168724); - w.u8(v); + const at = w.reserve(5); + w.view.setInt32(at, 31168724, true); + w.view.setUint8(at + 4, v); } else throw types.unwritable(v as object); w.exitStruct(); diff --git a/runtimes/typescript/test/generated/Nino.Conformance.TypedArrays.ts b/runtimes/typescript/test/generated/Nino.Conformance.TypedArrays.ts index d565c9f4e..2e02468cb 100644 --- a/runtimes/typescript/test/generated/Nino.Conformance.TypedArrays.ts +++ b/runtimes/typescript/test/generated/Nino.Conformance.TypedArrays.ts @@ -3346,8 +3346,9 @@ function writeChoice(w: NinoWriter, v: Choice | null): void { } w.enterStruct(); if (typeof v === "number") { - w.i32(546808484); - w.i32(v); + const at = w.reserve(8); + w.view.setInt32(at, 546808484, true); + w.view.setInt32(at + 4, v, true); } else if (typeof v === "string") { w.i32(-65934901); @@ -3367,9 +3368,10 @@ function writeChoice(w: NinoWriter, v: Choice | null): void { write$10(w, v); } else if (typeof v === "bigint") { - w.i32(1279255967); - w.u8(1); - w.i64(v); + const at = w.reserve(13); + w.view.setInt32(at, 1279255967, true); + w.view.setUint8(at + 4, 1); + w.view.setBigInt64(at + 5, v, true); } else throw types.unwritable(v as object); w.exitStruct(); @@ -3852,8 +3854,9 @@ function writeUnionTree(w: NinoWriter, v: UnionTree | null): void { write$48(w, v); } else if (typeof v === "bigint") { - w.i32(337757159); - w.i64(v); + const at = w.reserve(12); + w.view.setInt32(at, 337757159, true); + w.view.setBigInt64(at + 4, v, true); } else throw types.unwritable(v as object); w.exitStruct(); diff --git a/runtimes/typescript/test/generated/Nino.Conformance.ts b/runtimes/typescript/test/generated/Nino.Conformance.ts index 0783db25b..9ca106949 100644 --- a/runtimes/typescript/test/generated/Nino.Conformance.ts +++ b/runtimes/typescript/test/generated/Nino.Conformance.ts @@ -3346,8 +3346,9 @@ function writeChoice(w: NinoWriter, v: Choice | null): void { } w.enterStruct(); if (typeof v === "number") { - w.i32(546808484); - w.i32(v); + const at = w.reserve(8); + w.view.setInt32(at, 546808484, true); + w.view.setInt32(at + 4, v, true); } else if (typeof v === "string") { w.i32(-65934901); @@ -3367,9 +3368,10 @@ function writeChoice(w: NinoWriter, v: Choice | null): void { write$11(w, v); } else if (typeof v === "bigint") { - w.i32(1279255967); - w.u8(1); - w.i64(v); + const at = w.reserve(13); + w.view.setInt32(at, 1279255967, true); + w.view.setUint8(at + 4, 1); + w.view.setBigInt64(at + 5, v, true); } else throw types.unwritable(v as object); w.exitStruct(); @@ -3852,8 +3854,9 @@ function writeUnionTree(w: NinoWriter, v: UnionTree | null): void { write$49(w, v); } else if (typeof v === "bigint") { - w.i32(337757159); - w.i64(v); + const at = w.reserve(12); + w.view.setInt32(at, 337757159, true); + w.view.setBigInt64(at + 4, v, true); } else throw types.unwritable(v as object); w.exitStruct(); From c6f75325a485618e391b01f79204119d051f98d2 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?JasonXuDeveloper=20-=20=E5=82=91?= Date: Thu, 19 Nov 2026 02:33:31 +1100 Subject: [PATCH 5/5] docs(skills): record fixed union write results Record the Node runner evidence for folding a union tag and its fixed payload into one reservation. --- .claude/skills/optimize-runtimes/references/dispatch.md | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/.claude/skills/optimize-runtimes/references/dispatch.md b/.claude/skills/optimize-runtimes/references/dispatch.md index ab1c2231e..79efd1351 100644 --- a/.claude/skills/optimize-runtimes/references/dispatch.md +++ b/.claude/skills/optimize-runtimes/references/dispatch.md @@ -67,6 +67,10 @@ call, a helper that takes a function, or a `read_into` / `deserializeInto` path. deferred bodies. Against 44d49866 on runtime A/B 44333395182, `UnionArray` / `TaggedUnionArray` deserialize medians fell 38 % / 51 % on EPYC 9V74, 43 % / 55 % on Xeon 6975P-C and 33 % / 44 % on Neoverse-N2. Both serialize controls stayed on par. Count struct depth around each non-null case, including a failed read. +- Reserve the case ID and fixed payload together in TypeScript union writers. Use the same offset writer and padding + policy as other fixed shapes. On runtime A/B 44333395182, Node's `UnionArray` / `TaggedUnionArray` serialize + medians fell 17 % / 22 % on Neoverse-N2. The plain union wrote its 1,000 values in 37.1 µs against JSON's 38.8 µs + on that CPU, within 5 %. The sampled x64 medians fell 10–14 % / 17–23 %. - Treat the dynamic type of a generated Go union case as part of its API: callers assert the concrete value wrapper. `go build -gcflags=-m ./model` in the Go bench module reports heap escape for `UnionValueInt32`, `UnionValueInt64`, `UnionValueUnionPoint`, `UnionTaggedInt32` and `UnionTaggedInt16`. Go's small-integer