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
4 changes: 4 additions & 0 deletions .claude/skills/optimize-runtimes/references/blocks.md
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,10 @@ value check in a runtime or a template.
intact. In [44347813411](https://github.com/JasonXuDeveloper/Nino/actions/runs/44347813411) the revised GUID map
write took 82–85 % less time on x64 and 81 % less on Neoverse-N2. The revised map read took 52–54 % less on
x64 and 45 % less on Neoverse-N2.
- Keep generated raw fallback loops on direct codecs after the raw-memory attempt. In the
[prepared-key follow-up](https://github.com/JasonXuDeveloper/Nino/actions/runs/44451141322) both native
listings call `repeatedBits` directly for padded primitive pairs. The eight-pair read was on par on
EPYC and Neoverse-N2. Its Xeon 6973P-C sample took 9 % less time, with no same-CPU control.

- Check a block of range-limited 8- or 16-byte values four a step with one compare each (`w[0].Valid() && ... &&
w[3].Valid()`, then the failing step value by value), each `Valid` a single compare (a signed range from 0 as
Expand Down
7 changes: 6 additions & 1 deletion .claude/skills/optimize-runtimes/references/dispatch.md
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,12 @@ call, a helper that takes a function, or a `read_into` / `deserializeInto` path.
[44339615693](https://github.com/JasonXuDeveloper/Nino/actions/runs/44339615693) measured the eight-int control
15 % higher on Neoverse-N2. Caching only the composite field walk and restoring the string probe brought both
controls within 2 % by median on that CPU ([44347813411](https://github.com/JasonXuDeveloper/Nino/actions/runs/44347813411)).
The bytewise path still uses the reader's shared seeded table and bounded probing.
The bytewise path still uses the reader's shared seeded table and bounded probing. Prepare generated duplicate
checks in package initializers, shared by key type across set and dictionary helpers; primitive-entry readers
know the key width and distinguish integer `K(1)/K(2)` from floating point at compile time.
In [44451141322](https://github.com/JasonXuDeveloper/Nino/actions/runs/44451141322), eight-element Guid sets and
maps took 6–10 % and 5–7 % less time on sampled x64 CPUs and stayed on par on Neoverse-N2. The 1,024-entry Guid
map, eight-integer and 1,024-string controls were within 2 % by CPU median.

## Reuse branches

Expand Down
4 changes: 3 additions & 1 deletion .claude/skills/platforms/references/emulation.md
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,9 @@ crashing. Read it before running anything under qemu or taking a listing for Arm
garbage collector's lock-free lists (`runtime.(*lfstack).pop` SIGSEGV at address 0, during any test) in 2 to 5 of
8 runs with qemu 8.2 and the runners' qemu, and main's code before the run that showed it (f618b973) crashed too
(1 of 10, more allocation only made it likelier); Go 1.24 crashed in 0 of 8, `GOMAXPROCS=1` in 0 of 18, `GOARM=6`
did not help (4 of 8). runtimes.yml sets it on that step (r101).
did not help (4 of 8). runtimes.yml sets it on that step (r101). If qemu-arm still faults in
`runtime.(*lfstack).pop`, retry with `GODEBUG=asyncpreemptoff=1` as well. With Go 1.26.11 and QEMU
10.0.13, the first #353 run faulted during `TestStringEncodings`; all packages passed with both flags.
Use QEMU user-mode 10.0.13 or later for local Go 386 tests on Arm64 Linux. Go 1.26.11's same static
test binary reported `bad g in signal handler` or `morestack on gsignal` on QEMU 7.2.22. On Debian
trixie's QEMU 10.0.13 it passed three runs and `GOARCH=386 go test -exec qemu-i386 ./...` passed with
Expand Down
15 changes: 15 additions & 0 deletions runtimes/csharp/src/Nino.Codegen/Targets/Go/GoTarget.cs
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ public sealed partial class GoTarget : CodegenTarget
private readonly List<GoNamed> _namedCollections = new();
private readonly Dictionary<string, GoHelper> _helperByKey = new(StringComparer.Ordinal);
private readonly List<GoHelper> _helpers = new();
private readonly Dictionary<string, int> _keyChecks = new(StringComparer.Ordinal);
private readonly HashSet<int> _tuples = new();
private readonly Dictionary<TypeModel, GoGeneric> _generics = new();
private readonly Dictionary<string, GoClosed> _closedByKey = new(StringComparer.Ordinal);
Expand Down Expand Up @@ -110,6 +111,9 @@ public string Render(string source)
renderer.Set("generic_slots", _slots.ToList());
renderer.Set("uses_elements", types.Any(t => t.Elements != null));
renderer.Function("helper", new Func<GoShape, string, string>(Helper));
renderer.Function("key_check", new Func<string, string>(KeyCheck));
renderer.Function("key_check_types", new Func<string[]>(() =>
_keyChecks.OrderBy(p => p.Value).Select(p => p.Key).ToArray()));
renderer.Function("helper_count", new Func<int>(() => _helpers.Count));
renderer.Function("helper_at", new Func<int, GoHelper>(i => _helpers[i]));
renderer.Function("tag_name", new Func<string, string>(TagName));
Expand Down Expand Up @@ -190,6 +194,7 @@ protected override void Reset()
_namedCollections.Clear();
_helperByKey.Clear();
_helpers.Clear();
_keyChecks.Clear();
_tuples.Clear();
_generics.Clear();
_closedByKey.Clear();
Expand Down Expand Up @@ -404,6 +409,16 @@ private static string GoString(string text)
return sb.Append('"').ToString();
}

private string KeyCheck(string type)
{
if (!_keyChecks.TryGetValue(type, out var index))
{
index = _keyChecks.Count;
_keyChecks.Add(type, index);
}
return "keyCheck" + index.ToString(CultureInfo.InvariantCulture);
}

// ---------------------------------------------------------------- helpers

/// <summary>
Expand Down
23 changes: 16 additions & 7 deletions runtimes/csharp/src/Nino.Codegen/Templates/go/types.sbn
Original file line number Diff line number Diff line change
Expand Up @@ -214,6 +214,10 @@ func write_members(c)
ret $o
end

func check_unique_values(s, value)
ret key_check(gotype(s)) + ".Values(r, " + value + ")"
end

func helper_body(h)
$s = h.shape
$n = h.name
Expand Down Expand Up @@ -291,34 +295,34 @@ func helper_body(h)
$o = $o + read_loop($e, plus($s.key, $s.value), "k := " + read($s.key) + "\n\t\te = " + $e + "{Key: k, Value: " + read($s.value) + "}")
end
# Keys read twice fail, checked once over the pairs.
ret $o + ($s.unique_keys ? "\treturn nino.CheckUniqueKeys(r, v)\n}\n" : "\treturn v\n}\n")
ret $o + ($s.unique_keys ? "\treturn nino.CheckUniqueKeysWith(r, v, &" + key_check(gotype($s.key)) + ")\n}\n" : "\treturn v\n}\n")
end
# A sequence.
case $s.mode
when "unique", "fixed"
# Elements read twice fail, checked once over the elements (primitives read as one block).
if $s.mode == "unique" && $s.element.block
ret $o + "\treturn nino.CheckUniqueValues(r, nino.ReadPrimitives[" + gotype($s.element) + "](r))\n}\n"
ret $o + "\treturn " + check_unique_values($s.element, "nino.ReadPrimitives[" + gotype($s.element) + "](r)") + "\n}\n"
end
if $s.element.block_value
ret $o + "\treturn nino.CheckUniqueValues(r, nino.ReadBlock[" + gotype($s.element) + "](r))\n}\n"
ret $o + "\treturn " + check_unique_values($s.element, "nino.ReadBlock[" + gotype($s.element) + "](r)") + "\n}\n"
end
if $s.element.kind == "prim" && $s.element.primitive == "Bool"
ret $o + "\treturn nino.CheckUniqueValues(r, nino.ReadBools(r))\n}\n"
ret $o + "\treturn " + check_unique_values($s.element, "nino.ReadBools(r)") + "\n}\n"
end
$unique = $s.mode == "unique" || $s.unique_slots
$b = $s.element.raw_block
if $b
$o = $o + "\tif v, ok := nino.TryReadRaw[" + gotype($s.element) + "](r, " + $s.element.size + ", " + $b.as_is + ", " + $b.bools + "); ok {\n\t\treturn " + ($unique ? "nino.CheckUniqueValues(r, v)" : "v") + "\n\t}\n"
$o = $o + "\tif v, ok := nino.TryReadRaw[" + gotype($s.element) + "](r, " + $s.element.size + ", " + $b.as_is + ", " + $b.bools + "); ok {\n\t\treturn " + ($unique ? check_unique_values($s.element, "v") : "v") + "\n\t}\n"
end
ret $o + read_fixed(gotype($s.element), $s.element.size, "e = " + decode($s.element, 0), !$s.element.runnable) + "\treturn " + ($unique ? "nino.CheckUniqueValues(r, v)" : "v") + "\n}\n"
ret $o + read_fixed(gotype($s.element), $s.element.size, "e = " + decode($s.element, 0), !$s.element.runnable) + "\treturn " + ($unique ? check_unique_values($s.element, "v") : "v") + "\n}\n"
when "stringset"
# A set of strings in the order of the data: a string (or null) read twice fails.
ret $o + read_loop(gotype($s.element), $s.element.min, "e = " + read($s.element)) + "\treturn nino.CheckUniqueStrings(r, v)\n}\n"
when "nullableslots"
# A set of the slots of a number (any other sequence of them calls the block directly): an element read twice
# fails, null included.
ret $o + "\treturn nino.CheckUniqueValues(r, nino.ReadNullableSlots[" + gotype($s.element.inner) + "](r))\n}\n"
ret $o + "\treturn " + check_unique_values($s.element, "nino.ReadNullableSlots[" + gotype($s.element.inner) + "](r)") + "\n}\n"
end
ret $o + read_loop(gotype($s.element), $s.element.min, "e = " + read($s.element)) + "\treturn v\n}\n"
end
Expand Down Expand Up @@ -1023,6 +1027,11 @@ while $i < helper_count()
$i = $i + 1
end

# The duplicate check initializers shared across helpers by the Go key type.
for $type in key_check_types()
"\nvar keyCheck" + for.index + " = nino.PrepareKeyCheck[" + $type + "]()\n"
end

for n in tuples
$params = []
$fields = []
Expand Down
10 changes: 6 additions & 4 deletions runtimes/go/AGENTS.md
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ endian_little.go littleEndian = true on little-endian architectures
endian_other.go littleEndian = false elsewhere (blocks converted element by element)
unique.go duplicate keys of sets and maps read in data order: numbers, strings, NullableSlots of
numbers (a set of T?) and keys whose equality is their bytes, pairwise or in a seeded
open-addressing table (the reader's scratch); key metadata cached by Go type
open-addressing table (the reader's scratch); generated codecs prepare key metadata per Go key type
collections.go slices, maps, sets, entries, fixed-size runs, multidimensional shapes (the func-value
helpers unexported, for the runtime's rare paths); counts checked against the bytes left
depth.go class bodies (Class, ReadClass, WriteClass), deferral past 128, the 512 limit
Expand Down Expand Up @@ -153,9 +153,11 @@ requirements:
(it then measured 7-14 % faster) rather than land a loss.
- Checks over a block (ranges of dates and decimals, the bools of raw structs) and UTF-16 copies: what measured
faster and slower is in the `optimize-runtimes` skill, "Checking blocks in Go"; time them with BenchmarkBlocks and BenchmarkUTF16.
- Struct tags (`tagged.go`, `layout.go`, `marshal.go`) are the one place reflection is allowed: a plan built once
per type, then walked. Anything the wire format cannot express is a `*TagError` naming the field when the plan
is built, never a silent default; reads go through the same `Reader` calls as generated code, so the guarantees
- Prepare generated set and dictionary duplicate checks once in package initializers (`PrepareKeyCheck`).
Primitive-entry checks use their generic type constraint. Struct tags (`tagged.go`, `layout.go`,
`marshal.go`) also build a reflection plan once per type, then walk it. Anything the wire format cannot express
is a `*TagError` naming the field when the plan is built, never a silent default; reads go through the same
`Reader` calls as generated code, so the guarantees
above hold there too. The sample types carry the tags and `NinoLayout` a generator emits (README, "Tags on
generated structs"); keep them and `internal/sample/tagged_test.go` in step with the hand-written codecs.
A type decides its own encoding wherever the wire has two for one .NET shape: `nino.NinoNullable[T]` is .NET's
Expand Down
18 changes: 12 additions & 6 deletions runtimes/go/bench/model/nino_types.go

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

21 changes: 15 additions & 6 deletions runtimes/go/collections.go
Original file line number Diff line number Diff line change
Expand Up @@ -378,16 +378,21 @@ func (w *Writer) WriteShape(dims ...int) {

// CheckUniqueValues returns v, the elements of a set read in the order of the data, unless two of them are equal: then
// it fails r with CodeDuplicateKey (as .NET's HashSet and SortedSet do when filled from empty) and returns nil. It
// returns nil for nil and after r failed. Generated code reads the elements with direct calls, then checks them
// once here, as [CheckUniqueKeys] does for a dictionary's keys. Numbers and strings are compared as .NET compares them
// returns nil for nil and after r failed. Numbers and strings are compared as .NET compares them
// (a NaN equals every NaN), without a map, and so is a [NullableSlot] of a number, a set of T? (two nulls are equal);
// other values as Go compares them (unique.go).
func CheckUniqueValues[T comparable](r *Reader, v []T) []T {
return (*KeyCheck[T])(nil).Values(r, v)
}

// Values checks the elements of a set with c and returns them in their original order.
// It has the same result and error behavior as [CheckUniqueValues]. A nil check discovers the key kind.
func (c *KeyCheck[T]) Values(r *Reader, v []T) []T {
if v == nil || r.err != nil {
return nil
}
var e T
if repeated[T](unsafe.Pointer(unsafe.SliceData(v)), len(v), int(unsafe.Sizeof(e)), &r.keys) {
if c.repeated(unsafe.Pointer(unsafe.SliceData(v)), len(v), int(unsafe.Sizeof(e)), &r.keys) {
r.Fail(CodeDuplicateKey, "")
return nil
}
Expand Down Expand Up @@ -415,14 +420,18 @@ func readUniqueEntries[K comparable, V any](r *Reader, minSize int, readKey func

// CheckUniqueKeys returns e, the pairs of a dictionary read in the order of the data, unless two of its keys are equal:
// then it fails r with CodeDuplicateKey (as .NET's dictionaries do when filled from empty) and returns nil. It
// returns nil for nil and after r failed. Generated code reads the pairs with direct calls, then checks them once
// here. Keys are compared as [CheckUniqueValues] compares elements.
// returns nil for nil and after r failed. Keys are compared as [CheckUniqueValues] compares elements.
func CheckUniqueKeys[K comparable, V any](r *Reader, e []Entry[K, V]) []Entry[K, V] {
return CheckUniqueKeysWith[K, V](r, e, nil)
}

// CheckUniqueKeysWith is [CheckUniqueKeys] with key metadata prepared before decoding. A nil check discovers the key kind.
func CheckUniqueKeysWith[K comparable, V any](r *Reader, e []Entry[K, V], c *KeyCheck[K]) []Entry[K, V] {
if e == nil || r.err != nil {
return nil
}
var pair Entry[K, V]
if repeated[K](unsafe.Pointer(unsafe.SliceData(e)), len(e), int(unsafe.Sizeof(pair)), &r.keys) {
if c.repeated(unsafe.Pointer(unsafe.SliceData(e)), len(e), int(unsafe.Sizeof(pair)), &r.keys) {
r.Fail(CodeDuplicateKey, "")
return nil
}
Expand Down
Loading
Loading