Skip to content

perf(generator): keyed, version-tolerant and nested bodies take the item chain - #450

Merged
JasonXuDeveloper merged 10 commits into
feat/storage-layerfrom
claude/storage-unify-c
Nov 22, 2026
Merged

JasonXuDeveloper merged 10 commits into
feat/storage-layerfrom
claude/storage-unify-c

Conversation

@JasonXuDeveloper

Copy link
Copy Markdown
Owner

This is step C of #355. Version-tolerant bodies, keyed bodies, and flat bodies nested in another now take the one item chain for their runs. They do it as leaves on that chain, never as loops of their own. The bytes on the wire do not change.

The new leaves (Nino.Core, CompilerServices)

  • NinoRegionItem<TBody>: an int32 length, then the body.
    • On write, the body goes first and the length is patched in after it.
    • On read, the body sees only the region's bytes, and whatever a newer version appended is skipped.
    • A length longer than the data, or with the sign bit set, hands the element back to its formatter.
  • NinoTolerantItem<TItem>: if the region runs out, the member reads 0 bytes and keeps its value. A member cut off halfway is an error.
  • NinoKeyItem<TKey, TItem> inside NinoEntriesItem<TBody>: the key, which comes from the accessor's INinoKey literal, then the length, then the value.
    • Another key, or a length that is out of range or not filled, hands the element to the formatter.
    • So do reordered entries, renamed ones (former names) and unknown ones.
  • Nesting: a nested type's whole chain is one item.
    • A class uses NinoFlat<,>.Sealed / .Typed (presence byte or type id). A struct uses NinoFlatValues<>.Item.
    • The generator passes a depth literal, so runs close to NinoDepth.Inline go element by element.
    • A type that nests itself (a cycle) is not flat.
  • NinoReference holds the local copy of a class-typed property. With string as a type argument, the whole chain and its kernels became code shared over __Canon, and a version-tolerant class wrote in 4.2× its twin's time.

A memory-safety bug caught by the hostile twin tests, fixed here

An item that hands its element back returns int.MinValue. The next node then received source + 2 GB and a wrapped negative available, which passed the unsigned length checks. A raw item after the hand-back would have read out of bounds.

Each item now declares a constant, INinoItem.Leaves, and nodes stop right after an item that may hand its element back. For chains that never do, the check folds away.

Also in this PR

  • Item sizes come from constants, never from a static readonly read per element (Keep hot paths fast where the JIT does not fold: NativeAOT, IL2CPP, Mono and FullOpts #436): the formatter's FixedSize literal, Unsafe.SizeOf, or an INinoSize type argument. At TC=0, NullableFields' MaxSize folds to the literal 74.
  • A chain that never hands its element back keeps the string kernels' loops. The choice is made in NinoFlatValues' hooks. Placed in NinoFlatRuns, the branch made the class driver too large to inline at TC=0 ("too many il bytes").
  • Flat struct runs shorter than 16 elements take the per-value loops. The choice is made in the NinoRuns branch, not inside the kernels. Before this, a run of one flat struct wrote in 2.1× main's time.
  • No boxing: no CORINFO_HELP_BOX in any optimized method of the head listings.

Generated code

Keyed type (FlatKeyed):

NinoFlat<__Layout, NinoRegionItem<NinoEntriesItem<NinoItemAt<__M_Id, NinoKeyItem<__M_Id, NinoRawItem<int>>,
    NinoItemAt<__M_Name, NinoKeyItem<__M_Name, NinoUtf16Item>, NinoPropertyAt<__P_Big, long?, NinoKeyItem<__P_Big, NinoSlotItem<long?, __NinoF0, __P_Big>>, ...>>>>

Nested type (FlatOuter), with depth literal 2:

NinoItemAt<__M_Leaf, FlatLeafFormatter.__Flat.Typed, NinoItemAt<__M_Name, NinoUtf16Item,
    NinoPropertyAt<__P_Point, FlatPoint, FlatPointFormatter.__Flat.Item, ...>>>

Tests

  • Byte twins for keyed, version-tolerant, nested and struct bodies: runs of 1 to 600 elements, lists, reads into existing values, and every possible cut point.
  • Reads across versions, runs past the inline depth, and a dictionary with free entries.
  • Mutated bytes read the same way by the flat read and the generated read.
  • A ShapeMatrixTests dimension and fuzz inputs.

Measured locally (guidance only)

Against main 9540e0b, at 1,000 elements, Tier1:

Body Write Read
Version-tolerant class 0.80 0.87
Version-tolerant struct 0.63 0.90
Keyed class 0.77 1.00
Nested class 0.82 0.88
Flat struct 0.69 0.77
Generic class 0.62 0.95
  • Short runs (1 and 3 elements): mostly 0.77–1.07, measured on a machine shared with other agents. Outliers at 1 element:
    • version-tolerant struct write 1.26 (39 ns against 31);
    • generic class write 1.16;
    • version-tolerant class read 1.12.
  • Suite cases against the base branch:
    • ManagedStructs: write 0.97, read 0.98.
    • NullableFieldsArray, EventStream and ScatteredTest: on par.
    • AoiUpdate read: 0.84–1.00.
  • OrderBooks read: 0.93–1.01 at Tier1, but 1.08–1.10 at TC=0 even though the listings are identical. Not explained yet; a runner A/B will tell.

Verify

  • Release build: 0 warnings, also with GITHUB_ACTIONS=true.
  • Unit tests:
    • net10: 1,280 passed.
    • netstandard2.1, NinoRegularCalls and HWIntrinsic=0 also pass.
  • Conformance --verify, benchmark --verify and tools/agents.sh --check pass.
  • The Unity DLLs are refreshed.

Five commits have subjects that start with WIP:; they were pushed under the work-in-progress rule. Each one builds, and the work is now complete.

Not in this step

  • The JIT reports the node after a nested class item as "recursive", which leaves one call per element.
  • Raw-only classes as nestable items: these are classes whose body is copied as one raw block, such as BadgeCount. Allowing them would make ShallowUser flat.
  • UnmanagedNullableFormatter's NinoLayout<T> statics at TC=0. They cost a raw T? slot extra and account for the version-tolerant read's 1.11 at TC=0.

Part of #355.


Generated by Claude Code

…ement (#355)

NinoValueItem and NinoSlotItem read NinoKind<T, TFormatter>.Stride, a static
readonly field, in MaxSize, WriteAt and ReadAt of every element (#436): it folds
only where Tier1 compiles the loop after the class is initialized, not at
FullOpts, NativeAOT, IL2CPP or Mono.

Each item's bytes now come from a constant. A composite's raw item is picked
outside the loop as NinoStructItem<T> (Unsafe.SizeOf, raw values being their
memory) or NinoBoolItem; NinoValueItem is a block only, sized by its formatter's
FixedSize (a literal of a generated formatter). A slot's longest single value is
a type argument (INinoSize): the generated accessor's literal (its presence byte
and the canonical bytes FlatRules reads from the encoding), or NinoMemorySize<T>
in composites, where NinoCursor.Item takes a slot only when Unsafe.SizeOf<T?>
bounds its stride. INinoMember and INinoProperty extend INinoSize. The bytes
are unchanged.
…tem chain (#355)

Step C of the unified storage layer: a version-tolerant or keyed body, and a flat
body nested in another, now take the one chain of items for their runs, as
leaves of that chain rather than loops of their own. The bytes are unchanged.

Nino.Core gains the leaves:
- NinoRegionItem: the length-prefixed region. A read gets the region's bytes only
  and skips what a newer version appended; a region longer than the data, or one
  holding deferred bodies (bit 31), leaves the element to its formatter.
- NinoTolerantItem: a member past the region's end keeps its value.
- NinoKeyItem (the key from the accessor's INinoKey literal, the value's length
  written after it) inside NinoEntriesItem (the entries must fill the region): an
  entry in another order, under a former name or unknown leaves the element.
- A nested flat body is one item: NinoFlat.Sealed / Typed for a class (presence
  or type id, the object made or reused), NinoFlatValues.Item for a struct.

An item that may leave says so as a constant (INinoItem.Leaves), and a node
stops after it. Without the stop, the items after a leaving one read at
source + (uint)int.MinValue with the bytes left wrapped negative, which passes
every unsigned length check; the hostile twins in FlatTests.Bodies caught it.
With a test after every item, a positional flat struct's runs read in 1.08
times their time, so the test exists only after an item that may leave, and a
chain no element leaves keeps the string kernels' loops (no stop, no index).

A property of a class type keeps its local copy as NinoReference: with a class
as NinoPropertyAt's TValue the whole chain and the kernels over it became code
shared over __Canon (a version-tolerant class with one string property wrote
its runs in 4.2 times the generated body's time).

The generator (FlatRules, FlatEmitter, TypeFacts.Flat) admits positional,
version-tolerant and keyed bodies and members of another generated type whose
body is flat (memoized per compilation, cycles not flat), counts levels against
the JIT's inline depth and passes the class bodies one element opens, so a run
nested near NinoDepth.Inline goes element by element. Bodies that are not
positional, structs, generic types and bodies nesting a class keep their
member-by-member bodies and take the chain for runs only.

Tests: byte twins for keyed, version-tolerant, nested and struct bodies (runs of
1 to 600, lists, reads into existing elements, every cut rejected), reads across
versions, a run nested past the inline depth, a dictionary with a free entry,
hostile bytes read alike by the flat and the generated reads (every byte
changed, lengths and keys set to 0, -1, the sign bit, too long), a
ShapeMatrixTests dimension (a flat body nesting two in every format), and fuzz
inputs (keyed, version-tolerant and nested bodies in the Flat targets).

Refs #355
runtime.md and generator.md describe step C of the unified storage layer: the leaves
version-tolerant and keyed bodies take on the one chain of items (the region, a
tolerant member, a keyed entry), a flat body nested as one item, INinoItem.Leaves
and the stop after a leaving item, NinoReference for a property of a class type,
and which bodies take the chain for their runs only.

Refs #355
…355)

Done: a flat struct whose chain never leaves the element (positional, no class
nested) takes the string kernels' loops again, picked in NinoFlatValues' hooks;
the element items of classes test the body's size before storing a new object
only when the body may leave. In NinoFlatRuns' driver the branch made the class
driver too large to inline into NinoFlat.ReadSealed at TC=0 (asm.py: "too many
il bytes", a call more per run). IncrementalGeneratorTests expects NinoReference
for a string property.

Measured (local, alternated, against 047e0e0): ManagedStructs write 0.98 /
read 0.96 at Tier1, a flat struct's run read 0.96, OrderBooks read 0.93 at
Tier1 (1.08 at TC=0 before this change, being re-measured).

Left: re-measure OrderBooks at TC=0 and re-dump the listings; measure the new
leaves at 1, 3 and 1000 elements against main 9540e0b; the full gate.

Refs #355
optimize: item sizes from constants (838cf84), and five rules for keyed,
version-tolerant and nested flat bodies (Leaves and the stop after a leaving
item, NinoReference for a class-typed property, the string kernels' loops for
chains no element leaves picked in the hooks, nesting within the inline depth,
timing generated types through intercepted calls), with local numbers.
audit-generated-code and audit-security carry checks pointing to them.

Left: numbers at 1, 3 and 1000 elements against main once measured.

Refs #355
…ent (#355)

Done: a flat struct's run under NinoFlatRuns.MinRun (16) is written and read
element by element, as a flat class's already was. Against main 9540e0b
(local, alternated, Tier1) the kernels' fixed cost made a run of one flat struct
write in 2.1 times main's time (a version-tolerant struct 2.8), of three 1.35
(2.1); main writes these elements one at a time.

Measured against main at 1000 elements (Tier1): flat struct write 0.68 / read
0.76, version-tolerant class write 0.87 / read 0.99, version-tolerant struct
0.62 / 0.93, keyed class write 0.80 / read 1.13 (being looked at), nested
class 1.00 / 0.98, generic class 0.61 / 0.89.

Left: re-measure 1 and 3 elements with this change; keyed reads of long runs;
the full gate.

Refs #355
…at a time (#355)

Done: the gate for flat struct runs under NinoFlatRuns.MinRun moved from the
kernels' entry (WriteStrings, ReadFlatValues) to the branch in NinoRuns' Write
and Read (ShortFlatValues), so a short run takes the inline loops of values as
on main, not two out-of-line calls and then a loop one at a time. With the gate
in the entry a run of one flat struct still wrote in 1.55 times main's time, of
one version-tolerant struct 1.78 (Tier1, local, alternated).

Measured against main 9540e0b at 1000 elements (Tier1, local): flat struct
write 0.69 / read 0.77, version-tolerant class 0.80 / 0.87, version-tolerant
struct 0.63 / 0.90, keyed class 0.77 / 1.00, nested class 0.82 / 0.88, generic
class 0.62 / 0.95.

Left: re-measure 1 and 3 elements with this change; the full gate.

Refs #355
Nino.Core's public seam gained INinoItem.Leaves, NinoRegionItem,
NinoEntriesItem, NinoKeyItem, NinoTolerantItem, INinoKey, NinoReference and the
nested NinoFlat.Sealed / Typed and NinoFlatValues.Item items; the generator
emits them. UnityPackageDllTests checks the DLLs against the sources.

Refs #355
#355)

optimize: rule (6), a flat struct's run under MinRun takes the loops of values
one at a time, decided at the branch in NinoRuns (2.1 times main's time for a
run of one through the kernels, 1.55 with the gate in the kernels' entry, on par
at the branch), and every new leaf measured at 1, 3 and 1000 elements against
main.

Refs #355
@JasonXuDeveloper
JasonXuDeveloper merged commit e92a7d9 into feat/storage-layer Nov 22, 2026
31 checks passed
@JasonXuDeveloper
JasonXuDeveloper deleted the claude/storage-unify-c branch November 22, 2026 13:53
JasonXuDeveloper added a commit that referenced this pull request Nov 22, 2026
…sses' type id in the typed kernels

The flat hooks (INinoFormatter.WriteFlat/ReadFlat, the generated hooks, NinoFlat.WriteSealed/WriteTyped/
ReadSealed/ReadTyped) take the run by in and the writer or reader by ref, and NinoFlatRuns.WriteRun/ReadRun are
inlined into them on that writer or reader: the run and the writer were copied by value at each level, a
store-forwarding stall per copy. With the entry that cheap, NinoFlatRuns.MinRun (16) is gone: a List<Image> of
1, 3, 16 and 32 wrote in 1.05, 1.02, 0.90 and 0.95 times the time element by element and read in 1.04, 0.99,
0.94 and 0.95 (local x64, alternated; before, with MinRun 1, 1.79 and 1.37 at one element).

Short runs reaching the kernels exposed a bug already on feat/storage-layer: a non-sealed flat class nested in a
generic class wrote type id 0 (null's id) per element in the typed kernels, its layout printing the model's id
instead of the formatter's s_typeId, so a null element read back as a body (ShapeMatrix row 10; at 20 elements on
352c076 too). FormatterEmitter.TypeIdValue is now the one expression both use; FlatTests covers it.

Measured so far: see the previous WIP commits. Left: merge feat/storage-layer (#450), asm.py compare against main,
the remaining single-class gap, the gate, the docs(skills) commit.
JasonXuDeveloper added a commit that referenced this pull request Nov 22, 2026
Step C's chain leaves and depth-aware flat hooks, with this branch's run by in and writer or reader by ref through them (WriteFlat/ReadFlat, NinoFlat's drivers, NinoFlatRuns.WriteRun/ReadRun, NinoItems). MinRun stays for a flat struct's runs only (ShortFlatValues, measured on step C's by-value entry); a class's runs take the kernels at every length. Unity DLLs rebuilt by the gate.
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