Repository navigation
feat(seq)!: place and reserve wrapper-array elements, with the schema count as Bound.schema (generator#587) - #126
Merged
Merged
Conversation
…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.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
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) andSeq.reserveElem(struct / union elements, element factory passed in), beside the existingreserveRow*, plus a publicSeq.checkIndexfor the length-word check.Boundnow carries the schema count too —Bound.schema(count)beside the receiver-cap variant — so everySeqindex call comparesid >= countitself and throwsINVALID_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).Boundis arecord, so astatic finalbound folds into its compare under C2.Breaking:
Bound.SCHEMA_BOUNDEDis removed and everySeqindex call rejects a bound that carries no number withARGUMENT;Bound's privatecap()became the record accessormax(). 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_BOUNDEDis removed. Code generated by the current generatormainusesBound.SCHEMA_BOUNDED, so once this lands, the generator'slang-javajob (which pins this corelib'smain) 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:javadocclean.SeqTest,ReceiverCapTest,ForgottenCapTestdrive the helpers directly; mutation-checked (moving the check below the growth, or weakening the compare, fails them).SequenceGrowthTestnow runs the sharedsequence_growthblock through the realSeq.placeElem/reserveEleminstead of a hand-rolled copy.tests/conformance/java/run.sh) passes against this branch with the #587 generator.Measurements
Measured end to end with
tests/bench/run.shin the generator repo, both sides in the same container and toolchain: A = generatormainagainst this corelib'smain, B = the generator#587 branch against this branch (vehicle_telemetry, Callgrind instructions/op).javaMost of the decode cost is
reserveElem: an element built behind aSupplierin a shared method is harder for C2 to see through than an inlinenew. Accepted for one implementation of the rule.