refactor: read decode words unaligned, dropping the align_to prefix - #39
Merged
Conversation
bonega
force-pushed
the
unaligned-word-decode
branch
from
July 4, 2026 19:08
51a4cce to
556f19f
Compare
The bulk decoders read aligned usize chunks via align_to, which forced a byte-wise decode of the misaligned prefix. LLVM compiles that <=7-byte prefix loop as a generic remainder-plus-4x-unrolled structure and its cost model can't be hinted into specializing it, so every monomorphized decode carried the extra loop machinery. Read each word with chunks_exact + usize::from_ne_bytes instead: the unaligned load is a single mov on x86-64/AArch64 and stays correct on strict-alignment targets. This removes the prefix entirely, along with the len < USIZE_SIZE scalar fallback, the align_of guard, and the prefix-offset term in the incomplete decoder's error positions. Assembly for a monomorphized decode shrinks ~40% (complete) and ~34% (incomplete); decode benchmarks are neutral. Verified with Miri (Stacked/Tree Borrows, big-endian s390x) and thumbv7em no-std builds. Claude-Session: https://claude.ai/code/session_01D3JXrJnefe7pWs3kJRvTjz
bonega
force-pushed
the
unaligned-word-decode
branch
from
July 4, 2026 19:10
556f19f to
48dd6c7
Compare
bonega
enabled auto-merge (squash)
July 4, 2026 19:15
Merged
bonega
added a commit
that referenced
this pull request
Jul 17, 2026
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.
What
Replace the
align_to::<usize>()word-at-a-time structure in both bulk decoders withchunks_exact(USIZE_SIZE)+usize::from_ne_bytes, reading each word unaligned.Why
The aligned-read design forced a byte-wise decode of the misaligned prefix. LLVM compiles that ≤7-byte prefix loop as a generic
%4-remainder loop plus a 4×-unrolled loop, and its unroll cost model cannot be coaxed into the early-exit ladder it emits for the suffix (unreachable_unchecked, length masking, and a hand-unrolled source ladder were all tried — the last one gets re-rolled). Unaligned word loads are a singlemovon x86-64/AArch64, andfrom_ne_bytescompiles to whatever load the target supports, so the prefix problem can simply be removed instead of fixed.This also makes the
len < USIZE_SIZEscalar fallback and thealign_ofguard unnecessary, and drops the prefix-offset term from the incomplete decoder's error positions.Results
mov, the tail keeps its early-exit ladder, and the safetry_into().unwrap()leaves no panic branch.decode_checked,decode_lossy, back-to-back vs master withsetarch -R/taskset): neutral within noise.Verification
cargo test(144 passed),cargo clippy --all-targets -- -D warnings,cargo fmt --checkwriter_stressunder Stacked Borrows, Tree Borrows, and big-endian s390x; Miri lib tests--all-featuresthumbv7em-none-eabino-std builds (bare +alloc)https://claude.ai/code/session_01D3JXrJnefe7pWs3kJRvTjz