perf(base64): index-based loops instead of for-in iteration#241
Open
mizchi wants to merge 3 commits into
Open
Conversation
Encoder::encode_to and Decoder::decode_to walked their input with 'for byte in bytes' / 'for ch in input', which desugars through BytesView::iter / StringView::iter + Iter::next per element. On a 64 KiB benchmark those iterators account for ~32% of encode self time and ~35% of decode self time even though the loop body is a tiny match. Switch both to index loops: - encode_to pulls backing Bytes + start_offset once and indexes data[start + idx] per byte. - decode_to iterates 0..<input.length() and reads input.unsafe_get(off) directly. base64's alphabet is ASCII so a code-unit read is safe; any surrogate is rejected by char_to_index as before. base64 bench (native, 3-run median, 64 KiB x 500 iters): encode : 710 -> 564 ms (-20.6%) decode : 685 -> 432 ms (-36.9%)
Collaborator
|
Even though the fix is correct, I would personally not fix it in this way. The performance regression means that something's wrong with the compiler implementation that made the ideal way of writing code less performant. Can you please take a look? @Yu-zh |
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
Both
Encoder::encode_toandDecoder::decode_toincodec/base64/base64.mbtwalk their input withfor byte in bytes/for ch in input. That desugars throughBytesView::iter/StringView::iterplus anIter::nextcall per element. On a 64 KiB benchmark those iterators account for ~32% of the encode self time and ~35% of the decode self time — even though the inner-loop body is a tiny match.Switch both to index loops:
encode_to: pull the backingBytesandstart_offsetonce and indexdata[start + idx]per byte.decode_to: iterate0..<input.length()and readinput.unsafe_get(off)(UTF-16 code unit) directly. base64's alphabet is pure ASCII, so reading by code unit is safe; if a payload contains a high surrogate it isn't valid base64 anyway andchar_to_indexraisesInvalidCharas before.Benchmark
Scenarios:
bench-x/cmd/base64_encode/main.mbtandbench-x/cmd/base64_decode/main.mbt— 64 KiB payload, 500 iterations, Linux x86_64 native release, 3-run median wall time.Callgrind self-time delta (encode, top symbols):
BytesView::iterclosure (for byte in)Iter::next<Byte>Encoder::encode_toSame shape on decode: 23.37%
StringView::iter+ 11.91%Iter::next<Char>collapse to a single index step.Tests
Background
Same iter-overhead pattern as the moonbitlang/async
crc32_updatepatch:for byte in BytesView/for ch in StringViewis convenient to write but, in tight per-byte loops, the iter closure allocation +Iter::nextdispatch dominates over the loop body. Direct indexing of the backing buffer is intrinsic.