Class Seq
- Namespace
- sofab
- Assembly
- SofaBuffers.dll
Element placement, row reservation and array growth for generated decode destinations — the support layer, not part of the codec.
public static class Seq
- Inheritance
-
Seq
- Inherited Members
Remarks
Nothing here touches the wire. These are the operations a generated message class performs around an IVisitor callback: put an element at the index its id names, reserve the slot a framed element is routed into, reserve a matrix row, enlarge an array as elements actually arrive. Their code has the same shape for every schema — a bound arrives as an argument, an element type as a type parameter and an element default as an argument or a factory — which is why they live in the corelib rather than being emitted, rationale and all, into every generated source tree (generator#345, #587).
Three reservations, one shape. Every wrapper array a schema can declare
reaches this class through one of three calls, which differ only in what a
slot holds: PlaceElem<T>(List<T>, int, T, T, long, long) puts a decoded string or
blob at the index its id names; ReserveElem<T>(List<T>, int, Func<T>, long, long) makes the
slot a struct, union or nested-array element is routed into;
ReserveRow<T>(List<List<T>>, int, long, long) reserves a matrix row. CheckIndex(int, long, long) is
the bound on its own, published for the one site that has no reservation to
ride. A generated C# row is always a List<T>, native or wrapper,
so one generic ReserveRow<T>(List<List<T>>, int, long, long) serves every matrix; .NET shares one
canonical body for every reference-type instantiation, so the generics cost no
code per schema.
Ids are positions (MESSAGE_SPEC §5.1). An array element's id is its index, an interior element equal to the element default may be omitted, and the highest id present is what gives the decoded array its length — so a missing id fills a gap rather than shifting every later element down by one, a repeated id replaces (or, for a framed element, merges) rather than appends (§7.4), and no trailing fill is ever needed because the last element is never elided. Not one of these rules is visible in the bytes: two implementations can disagree about each and still emit an identical message, which is why they are written here once and tested here directly (CORELIB_PLAN §7.2 item 8).
An element index is untrusted, and both of its bounds travel as
arguments. Growing a list to id + 1 means one over-index element is
by itself the allocation an untrusted index buys, so every placement bounds the
index before the list grows: a refused id leaves the list exactly as
it was, and a lower id delivered afterwards still lands (§7.2 item 8). Each
call takes two numbers, and the schema decides which one applies:
cap— the array's schemacount, a capacity and not a length (the list starts empty and the wire carries the length). An id at or past it contradicts the schema both peers agreed on: malformed input, InvalidMessage (MESSAGE_SPEC §7.1). Where the schema declares no count, the caller passes a negativecap.rcap— the receiver'smax_dyn_array_count, applied only wherecapis negative. The bytes are then well formed and the same element decodes under a looser configuration, so the verdict is the LimitExceeded policy category (CORELIB_PLAN §6.2.1, §6.3). A negativercapstates no cap at all and admits no element, reported as Argument: the mistake is in the call, not in a receiver policy nobody configured.
Exactly one of the two is ever compared — a receiver cap never touches a field
the schema already bounds (§6.2.1) — and neither is this class's: both arrive
per call, are used for that one comparison and are not retained. Nothing here
holds, defaults to or clamps to a limit of its own. Generated code therefore
needs no index comparison of its own at all; it passes the schema count (a
literal the JIT folds) and its MaxDynArrayCount constant to every call.
What these bounds do not cover. A string or blob
element's own length (maxlen, or max_dyn_string_len /
max_dyn_blob_len) is judged at the length word, so that a message
ending right after that word is refused rather than reported
Incomplete (MESSAGE_SPEC §5.2); generated code runs
CheckIndex(int, long, long) there, then the length check, and places the element
only once its payload is complete. The receiver half of that length check is
PayloadAcc's. Neither is the element count of a native
array or of a matrix row bounded here: that count arrives at
ArrayBegin(int, ArrayKind, int) and no call of this class sees it.
A count is untrusted too. An array's element count is the wire's claim
about how many elements follow, and until a schema count or a receiver
limit bounds it, nothing else does. So no method here allocates from a count
alone: the count is a ceiling on growth, never the first allocation.
The encode output buffer is not here: it belongs to the generated layer (CORELIB_PLAN §5.1).
Fields
ArrayInitCap
Initial element capacity for an array whose length the schema does not bound.
public const int ArrayInitCap = 16
Field Value
Remarks
The first reservation is this, not the announced count: sizing the destination from an untrusted count lets a three-byte header ask for gigabytes. Growth starts here and EnsureCap<T>(T[], int, int) doubles it against elements that have actually arrived.
Methods
CheckIndex(int, long, long)
Reject an element index the caller may not accept, before any list is grown to hold it (CORELIB_PLAN §6.2.1).
public static void CheckIndex(int id, long cap, long rcap)
Parameters
idintthe element's wire id, which is its index
caplongthe array's schema
count, or negative where the schema declares nonercaplongthe receiver's
max_dyn_array_count, applied only wherecapis negative
Remarks
Every placement here runs this first. It is public for the one site that
has no reservation to ride: a generated FixlenBegin arm bounds
a string or blob element's index at the length word, so that a
message ending right there is refused rather than reported
Incomplete (MESSAGE_SPEC §5.2). The placement
that follows re-runs it, which costs one comparison against a folded
constant and spares the two sites from having to agree by inspection.
Exactly one of the two numbers applies, and the schema picks which — see the class remarks. The comparison is one branch; the refusal is built out of line so that this stays small enough to inline at every call site. A negative id — which no decoder delivers — is refused too, rather than reaching the list's indexer; against a folded bound the JIT turns the pair into one unsigned comparison.
Exceptions
- SofabException
(InvalidMessage) when
idreaches a declaredcap; (LimitExceeded) when a schema-uncountedidreaches a statedrcap; (Argument) when a schema-uncounted array was handed no cap at all (rcapnegative).
EnsureCap<T>(T[], int, int)
Enlarge array so that index index can
be written, doubling its length but never past cap.
public static T[] EnsureCap<T>(T[] array, int index, int cap)
Parameters
arrayT[]the array so far
indexintindex about to be written
capintgrowth ceiling: the announced or declared element count
Returns
- T[]
array, or a longer copy of it
Type Parameters
Telement type of the destination array
Remarks
This is the growth policy for an array being filled element by element, and
its whole point is that it tracks elements that have actually arrived.
Doubling keeps the fill amortized O(n), and the cap clamp
means an honest array of the announced length still ends up exactly
right-sized rather than at the next power of two.
cap is a ceiling on the result, not a bound the
caller is relieved of checking: it is the announced element count for an
unbounded field and the schema capacity for a bounded one, both already
judged by the caller at ArrayBegin(int, ArrayKind, int) (MESSAGE_SPEC
§7.1), and a fill that stays within its own count therefore never sees it
clamp.
The arithmetic is done in long, so doubling a large array cannot
overflow into a negative length and hand back an array shorter than the one
it was given. Whenever index already fits, the array is
returned untouched — the call sits on the hot path unguarded.
PlaceElem<T>(List<T>, int, T, T, long, long)
Place a leaf element — a wrapper array's string or
blob — at the index its wire id names, growing the list and filling
the gap omitted interior elements left (MESSAGE_SPEC §5.1).
public static void PlaceElem<T>(List<T> list, int id, T empty, T value, long cap, long rcap)
Parameters
listList<T>the destination list, which this grows
idintthe element's wire id, which is its index
emptyTthe element default, filling any gap below
idvalueTthe decoded element
caplongthe array's schema
count, or negative where the schema declares nonercaplongthe receiver's
max_dyn_array_count, applied only wherecapis negative
Type Parameters
Telement type
Remarks
The index is bounded first (CheckIndex(int, long, long)), so a refused id
leaves list exactly as it was. A gap below
id is filled with empty, and a
repeated id replaces the element already there (§7.4).
empty is shared, not copied: the gap value of a
string or blob array is "" or Empty<T>() —
immutable, or zero-length and so with no state to share wrongly — so a gap
costs no allocation. An element whose default is a mutable object
belongs in ReserveElem<T>(List<T>, int, Func<T>, long, long), which makes one per slot.
Exceptions
ReserveElem<T>(List<T>, int, Func<T>, long, long)
Reserve a framed element — the slot a wrapper array's
struct, union or nested-array element is routed into: bound
the index, then grow the list to id + 1, giving each new slot its own
element from make (MESSAGE_SPEC §5.1).
public static void ReserveElem<T>(List<T> list, int id, Func<T> make, long cap, long rcap)
Parameters
listList<T>the destination list, which this grows
idintthe element's wire id, which is its index
makeFunc<T>the element factory, called once per slot this creates
caplongthe array's schema
count, or negative where the schema declares nonercaplongthe receiver's
max_dyn_array_count, applied only wherecapis negative
Type Parameters
Telement type
Remarks
Same rules and same order as PlaceElem<T>(List<T>, int, T, T, long, long), with one deliberate difference: a slot already present is left alone. A framed element's fields arrive one at a time and each is routed into the object reserved here, so a re-opened element id merges into what its earlier fields built (§7.4); replacing the slot would discard them.
A factory, not a shared default. A generated element is mutable and
decodes into the object placed here, so one shared instance would
alias every element of the array onto it. A Func<TResult> and
not a new() constraint: in a shared generic body new T()
becomes a reflective Activator.CreateInstance, whereas a
static () => new X() lambda is cached by the compiler and costs no
allocation per call. It runs only when a slot is created.
What this does not own is the routing — binding the element index, switching into the element's scope. That has a different shape per schema and stays generated; this owns growth and the bound, and stops at the slot.
Exceptions
ReserveRow<T>(List<List<T>>, int, long, long)
Reserve a matrix row — the row at index id of an
array whose elements are themselves arrays — as an empty row, growing the
outer list with empty rows so that a gap in the ids decodes as an empty row
instead of shifting every later row down by one.
public static void ReserveRow<T>(List<List<T>> rows, int id, long cap, long rcap)
Parameters
rowsList<List<T>>the outer list, one entry per row
idintthe row's wire id, which is its index
caplongthe outer array's schema
count, or negative where the schema declares nonercaplongthe receiver's
max_dyn_array_count, applied only wherecapis negative
Type Parameters
Trow element type
Remarks
The row index is bounded first, as in every placement here. Each gap row is a distinct empty list — a row is mutable, so a shared one would alias. A row id delivered again replaces the row's value (§7.4): the list already there is emptied in place rather than swapped for a new one, which reads the same and allocates one list per row instead of two. The row's own element count is not bounded here; it arrives at ArrayBegin(int, ArrayKind, int) or in the row's sequence frame, and the caller judges it.