base64 AVX2 - Use half avx2 to smooth the performance cliffs on small payloads when encoding - #1041
Open
gaspardpetit wants to merge 1 commit into
Open
gaspardpetit wants to merge 1 commit into
gaspardpetit wants to merge 1 commit into
Conversation
gaspardpetit
marked this pull request as draft
September 19, 2026 05:36
gaspardpetit
marked this pull request as ready for review
September 19, 2026 05:54
11 tasks
This branch has not been deployed
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.
Short title (summary):
Smooth AVX2 Base64 encoding ramp-up with 12/24-byte tails
Description
Add 12-byte and 24-byte AVX2 encoding paths that reuse the existing AVX2 packing and alphabet-translation pipeline. This avoids falling back entirely to scalar encoding for inputs and tails that are too short for the regular AVX2 loop, substantially reducing the performance cliffs at 28-byte intervals.
This is very similar to what is already being done in the tail handling of src/icelake/icelake_base64.inl.cpp using AVX-512.
NEON also does this partially (src/arm64/arm_base64.cpp) but not fully - we could add a custom 12 byte tail handling there as well. I'll submit it later if I can find the time.
Type of change
How to verify / test
Checklist before submitting
Final notes
This PR address the performance cliff observed on small payloads as AVX2 improvements are degraded by tails until another AVX2 window can be used. The strategy is to run AVX2 against a partial vector. There is a point where the overhead of a partial vector balances the overhead of the tail - this is where it is interesting to switch.
The effect compounds with #1037 as shown below.
Notice the gap in performance on byte 28 in master - this is when we introduce the first AVX2 block processing - and then we have another ramp up:
Here we have another performance cliff, followed by another ramp up:
And another here.
So the approach significantly reduces the cliffs. It can be further improved with SSE (i.e. transitioning from scalar -> partial SSE -> full SSE -> partial AVX2 -> full AVX2 -> full AVX2 + scalar -> ...
going to partial AVX 512 would also be interesting
But adding SSE substeps would have increased the complexity here and the gains are not as important. If interested, I am happy to provide it as another PR.