Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 13 additions & 0 deletions .claude/skills/optimize-runtimes/references/dispatch.md
Original file line number Diff line number Diff line change
Expand Up @@ -63,3 +63,16 @@ 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.
- 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
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.
31 changes: 20 additions & 11 deletions runtimes/csharp/src/Nino.Codegen/Templates/rust/types.sbn
Original file line number Diff line number Diff line change
Expand Up @@ -263,32 +263,41 @@ func union_rs(t)
end
$o = $o + " fn read_case(r: &mut nino::Reader<'_>, id: i32) -> nino::Result<Self> {\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"
if t.model.union_nests
$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<Self> {\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<Self> {\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"
Expand Down
26 changes: 20 additions & 6 deletions runtimes/csharp/src/Nino.Codegen/Templates/typescript/types.sbn
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down Expand Up @@ -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
Expand Down
28 changes: 8 additions & 20 deletions runtimes/rust/bench/src/model.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9048,19 +9048,13 @@ impl UnionTagged {
}
impl NinoRead for UnionTagged {
const MIN_SIZE: usize = 4;
#[inline]
fn read(r: &mut nino::Reader<'_>) -> nino::Result<Self> {
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 {
Expand Down Expand Up @@ -9132,10 +9126,12 @@ impl UnionValue {
}
791437675 => Self::UnionPoint(r.read()?),
635318394 => {
let mark = r.defer_mark();
let value: Option<Box<UnionItem>> = 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::<UnionValue>()?.case_value()));
Self::UnionItem(value)
}
_ => return Err(nino::Error::new(nino::ErrorKind::UnknownTypeId, id)),
Expand All @@ -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<Self> {
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::<UnionValue>()?.case_value()));
Ok(())
value
}
}
impl NinoWrite for UnionValue {
Expand Down
36 changes: 14 additions & 22 deletions runtimes/rust/tests/generated/nino_types.rs

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

34 changes: 12 additions & 22 deletions runtimes/rust/tests/generated/shapes.rs

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading
Loading