Derive the sequence tail position, 2026-10-17
An isolated candidate for Deque, HistoryRing and AppendLog. It changes their
shared collection implementation, not Store or the IC backend. The exact source
baseline is recorded in source-baseline.json; source.patch applies to the
unchanged sequence implementation at commit acbb2d7.
The profiler identified a write to the sequence's stored tail position during ordinary appends. The position is redundant: all interior chunks are full, so for a nonempty sequence it follows from the first slot and public length:
end = ((first + ((len - 1) % 32)) % 32) + 1
first is validated below 32 before this calculation. Reducing the length before
addition bounds the sum by 62, even when len is u64::MAX. Empty decoded headers
use zero. A small unit test compares this arithmetic against a wider u128
reference and another verifies malformed first slots fail before the arithmetic.
The candidate removes the serialized end word, shrinking the control payload
from 64 to 56 bytes. Remaining index fields move down eight bytes. All three
family schema identifiers advance to _0004; there is no old-format branch or
migration for this prototype change. The allocator may still round both payload
sizes to the same backing block size.
The in-memory Header retains a transient end because intermediate mutations
already maintain it correctly. Empty insertion temporarily uses first=end=16
before placing the first element. Front edits change first and length together;
back edits change length and end together. Prefix pruning does the former in
bounded spans, and ring overwrite composes the existing pop and push operations.
Keeping that local state avoids restructuring otherwise correct algorithms.
Publication compares only serialized fields. Back insertion/removal within an existing tail chunk no longer writes the control payload. Slot changes and ResourceMeta (including length/revision) still publish together. Front movement writes its first-slot word; chunk/directory changes write the complete new header. A full ring overwrite usually changes the first slot, so it still writes the control payload. The existing revision increment continues to invalidate cursors even when there are no header bytes to write.
tests.patch adds public-API differential tests for all first-slot residues,
lengths around chunk boundaries, mixed endpoints, ring capacities on either side
of chunk boundaries, absolute log indices, bounded pruning and empty/reuse. All
three families run in retained and stable storage and reopen between mutations.
Existing scalar publication and cursor tests cover one-revision publication and
stale-cursor rejection.
Source review, formatting and patch applicability are separate from execution. The measurement owner must compile and run the candidate before acceptance:
cargo test -p cdk-rsx-collections --lib
cargo test -p cdk-rsx-collections --test sequence_header
cargo test -p cdk-rsx-collections --test scalar_sequence_publication
cargo test -p cdk-rsx-collections --test sequence --test sequence_index --test typed_sequence
cargo test -p cdk-rsx-collections --test log_cursor --test scoped_sequences
Then compare fresh Wasm method-body instruction counts and memory statistics for all three families, both placements, 100/10k/100k sizes, before deciding. Removing one stable write does not imply a fixed instruction saving: another write or read may already map/dirty the same IC accounting region. No measured gain is claimed by this proposal.