DO NOT MERGE: bits pattern in unsafe_extract_byte regresses ~27x on native - #4100
Draft
bobzhang wants to merge 1 commit into
Draft
DO NOT MERGE: bits pattern in unsafe_extract_byte regresses ~27x on native#4100bobzhang wants to merge 1 commit into
bobzhang wants to merge 1 commit into
Conversation
REGRESSES - do not merge as-is. Converts the manual (b0 << 8) | b1 in both the ArrayView[Byte] and BytesView copies of unsafe_extract_byte. Measured: native 467ns -> 12.6us (~27x), wasm-gc +84%, js +37%. The pattern needs a fresh slice at the computed byte_index to replace only two byte reads, so the view construction dominates.
4 tasks
Collaborator
Coverage Report for CI Build 6161Coverage remained the same at 90.692%Details
Uncovered ChangesNo uncovered changes found. Coverage RegressionsNo coverage regressions found. Coverage Stats
💛 - Coveralls |
bobzhang
commented
Aug 19, 2026
| let b1 = bs.unsafe_get(byte_index + 1).to_uint() | ||
| let data = (b0 << 8) | b1 | ||
| guard! bs[byte_index:] is [u16be(data), ..] | ||
| // mask off the top bits |
Contributor
Author
There was a problem hiding this comment.
xx.get(i) is Some(y)
xx.get_view(i,j) is Some([i,j....]
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.
Opened as a draft to record a measured negative result from the bits-pattern sweep, as the counterpart to #4099 (which holds the conversions that win). Keeping it visible so nobody re-derives it later.
What it does
builtin/bitstring.mbthas the same manual 16-bit big-endian assembly in both copies ofunsafe_extract_byte— theArrayView[Byte]one and theBytesViewone:This replaces both with
guard! bs[byte_index:] is [u16be(data), ..]. It is correct — the full suite passes on all three backends (native 7370, wasm-gc 7454, js 7398) — and it is shorter and clearer. It is just much slower.Measured
moon bench --release, a 4096-byte buffer read at unaligned 7-bit offsets spanning two bytes.The native figure was re-run twice to confirm (12.63 µs, 13.26 µs). Its 467 ns baseline is ~0.11 ns per iteration, so the original was being inlined and heavily optimized; the slice plus guard blocks that entirely.
Why
The pattern needs a fresh view built at the computed
byte_indexin order to replace only two byte reads, so view construction dominates. The same shape is whyv128/simd_memory.mbt'sload_u32_le/load_u64_leregressed 1.6–3.5× on wasm-gc when I tried converting them, and it is the boundary that separates this PR from the one that landed the wins:u32/u64)u16at an offset)What would make this mergeable
Either a cheaper way to spell "read a
u16beat a computed offset without materializing a view", or an unsafe/unchecked slice that the pattern can consume. Until then the manual form is the right code here, and this PR should stay closed or draft.🤖 Generated with Claude Code