Skip to content

feat(seq)!: place and reserve wrapper-array elements, with the schema count as Bound.schema (generator#587) - #126

Merged
Andste82 merged 3 commits into
mainfrom
feat/generic-seq-helpers-587
Sep 22, 2026
Merged

Andste82 merged 3 commits into
mainfrom
feat/generic-seq-helpers-587

Conversation

@Andste82

Copy link
Copy Markdown
Member

Summary

Part of sofa-buffers/generator#587. A static helper — code with the same shape for every schema, whose schema dependence is only a bound, an element type and a default value — belongs in the corelib, not re-emitted per field into every user's source tree (generator ARCHITECTURE §8). Until now the java backend emitted the wrapper-array placement itself, per field: refuse an index past the bound, fill the gap the omitted interior elements left, place at the id. This PR adds that layer here, as a generic helper with the bound passed in, so the generator can call it instead.

What changes

  • Seq.placeElem (string / blob elements) and Seq.reserveElem (struct / union elements, element factory passed in), beside the existing reserveRow*, plus a public Seq.checkIndex for the length-word check.
  • Bound now carries the schema count too — Bound.schema(count) beside the receiver-cap variant — so every Seq index call compares id >= count itself and throws INVALID_MSG; generated code no longer emits a literal index check. The two stay exclusive (§6.2.1) and the check runs before any growth (§7.2 item 8).
  • Bound is a record, so a static final bound folds into its compare under C2.

Breaking: Bound.SCHEMA_BOUNDED is removed and every Seq index call rejects a bound that carries no number with ARGUMENT; Bound's private cap() became the record accessor max(). Over-index refusal messages no longer carry the field name (only the category is normative).

Merge order

Compatibility with generator main: no — merge in lockstep with the generator PR. Bound.SCHEMA_BOUNDED is removed. Code generated by the current generator main uses Bound.SCHEMA_BOUNDED, so once this lands, the generator's lang-java job (which pins this corelib's main) goes red until the generator side of #587 is merged too. Merge the two together.

The generator side of #587 is not opened as a PR yet; it lives on the generator branch that calls these helpers.

Tests

  • mvn -B verify — 1,613 tests, 0 failures; mvn javadoc:javadoc clean.
  • SeqTest, ReceiverCapTest, ForgottenCapTest drive the helpers directly; mutation-checked (moving the check below the growth, or weakening the compare, fails them). SequenceGrowthTest now runs the shared sequence_growth block through the real Seq.placeElem / reserveElem instead of a hand-rolled copy.
  • Generator conformance (tests/conformance/java/run.sh) passes against this branch with the #587 generator.

Measurements

Measured end to end with tests/bench/run.sh in the generator repo, both sides in the same container and toolchain: A = generator main against this corelib's main, B = the generator#587 branch against this branch (vehicle_telemetry, Callgrind instructions/op).

row encode Ir/op (A → B) decode Ir/op (A → B)
java 17,008 → 17,007 31,504 → 31,752 (+0.79 %)

Most of the decode cost is reserveElem: an element built behind a Supplier in a shared method is harder for C2 to see through than an inline new. Accepted for one implementation of the rule.

…587)

`Seq` held only the matrix-row half of the wrapper-array rules: `reserveRow`
and its six primitive overloads. The other two shapes -- a `string`/`blob`
element and a `struct`/`union`/nested-row element -- were emitted, inline, into
every generated message class:

    if (id >= 5) throw Sofab.invalid("... above schema capacity 5");
    while (m.warnings.size() <= id) m.warnings.add("");
    m.warnings.set(id, _s);

    if (id >= MAX_DYN_ARRAY_COUNT) throw new java.io.UncheckedIOException(
        new SofabException(SofabError.LIMIT_EXCEEDED, "... above configured limit"));
    while (m.dyn_structs.size() <= id) m.dyn_structs.add(new DynStructsElem());

That is one copy of MESSAGE_SPEC §5.1 per wrapper array per schema, and a
second implementation of the §6.2.1 cap beside the one `reserveRow` already
has. Its schema dependence is a bound, an element type and an element default
-- an argument, a type parameter and an argument -- which is ARCHITECTURE §8's
condition for a helper belonging in the corelib.

Two signatures, beside the row ones and taking the same `Bound`:

  placeElem(List<T> out, int id, T def, T value, Bound bound)
      bound the index, fill the gaps omitted interior elements left with the
      shared default, `set` at the id so a repeat replaces (§7.4).

  reserveElem(List<T> out, int id, Supplier<T> make, Bound bound)
      bound the index, grow to id + 1 with one fresh element per slot. A slot
      already present is LEFT ALONE: a framed element's fields arrive one at a
      time and a re-opened id merges into what the earlier ones built (§7.4).

They are two methods rather than one because the element default cannot be
shared for the framed case: a struct, union or nested row is mutable and an
arriving element decodes into the object the reservation placed, so one shared
instance would alias every element of the array onto it. `make` is a generated
method reference; javac lowers a non-capturing one to a constant, so it
allocates nothing per call.

`checkIndex` becomes public for the one site with no reservation to ride: a
generated `fixlenBegin` arm bounds a string or blob element's index at the
LENGTH WORD, so a message ending right there is refused rather than reported
INCOMPLETE (§5.2).

Java erases generics, so one body serves every element type -- no
monomorphisation and no code growth however many wrapper arrays a schema
declares. Measured with `tests/bench/run.sh --rows java` against this working
tree: encode 17006, decode 31511 Ir/op, bit-identical to the committed row.

Normative details preserved exactly as the emitted code had them: the index
check runs BEFORE any growth, so a rejected id leaves the container unextended
and a lower id delivered afterwards still lands (CORELIB_PLAN §7.2 item 8); a
schema `count` is a capacity, so `id >= count` and the container starts empty;
where the schema leaves the array open the receiver cap governs instead and the
verdict is LIMIT_EXCEEDED, never both (§6.2.1); the cap is passed in, never held
or defaulted. What stays generated is stated in the class note: the schema
`count` verdict, which `Bound.SCHEMA_BOUNDED` deliberately carries no number
for; an element's own `maxlen`, which has no reservation at the length word to
ride and is `PayloadAcc`'s; and the routing of a framed element, which has a
different shape per schema.

Purely additive: no existing signature changes, so generated code from sofabgen
before generator#587 still compiles and still runs.

Tests exercise the helpers directly, with their own containers, rather than only
through a decode -- placement at an id, an interior gap, the length that a lone
high id sets, a repeat replacing (leaf) against a repeat merging (framed), the
shared gap value costing no allocation, the id at the bound, the id past it with
the container checked for partial extension and a lower id landing afterwards,
the factory never called for a refused index, the cap breach as LIMIT_EXCEEDED
rather than INVALID, an omitted bound as ARGUMENT, and the length word's three
comparisons in the order generated code makes them, including an element payload
over its maxlen.
…nerator#587)

Bound gains a schema variant, Bound.schema(n), so the Seq reservations
(placeElem, reserveElem, reserveRow, reserveRow*) and Seq.checkIndex compare
a wrapper array's schema count themselves: id >= count is INVALID_MSG, a
receiver cap stays LIMIT_EXCEEDED, and exactly one of the two is ever handed
in (CORELIB_PLAN 6.2.1). The index is still compared before anything grows
(7.2 item 8), and the fast path is still one comparison; the verdict is
chosen out of line, on the path that throws.

Bound.SCHEMA_BOUNDED stays for one legitimate use: a string/blob whose schema
maxlen the caller already enforced at the length word, where PayloadAcc has
no second number to compare. It carries no count, so every Seq reservation now
refuses it as ARGUMENT instead of growing the list uncompared. PayloadAcc
also honours Bound.schema(n) as a maxlen, INVALID_MSG above it.

SchemaBoundTest pins the new variant on all nine reservations: at the bound,
past it (INVALID, list not extended, a lower id still lands), and never
LIMIT_EXCEEDED. SequenceGrowthTest now drives the real Seq.checkIndex,
Seq.placeElem and Seq.reserveElem instead of a hand-rolled copy.

BREAKING CHANGE: Seq reservations reject Bound.SCHEMA_BOUNDED with ARGUMENT;
pass Bound.schema(count) where the schema bounds the array.
…ts compare (generator#587)

HotSpot trusts the final fields of a record as constants and does not trust an
ordinary class's, so a static final Bound.schema(n) now compiles to a compare
against an immediate - the same code the literal guard generated code used to
emit in front of the call. Measured on the generator's java bench row
(vehicle_telemetry decode, Callgrind subtract): 31852 -> 31745 Ir/op against a
31685 baseline with the literal guard.

The rule travels as a public Bound.Rule (SCHEMA, RECEIVER, CALLER_CHECKED).
The canonical constructor a record exposes validates exactly as schema(n) and
receiver(n) do: a numbered rule needs at least 1, and only CALLER_CHECKED (the
numberless SCHEMA_BOUNDED) carries -1. Seq.checkIndex keeps one compare on the
fast path and decides the verdict out of line.
@Andste82
Andste82 merged commit 18aec63 into main Sep 22, 2026
4 checks passed
@Andste82
Andste82 deleted the feat/generic-seq-helpers-587 branch September 22, 2026 06:34
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