Repository navigation
gh-124951: Use NEON for base64 in binascii on AArch64 - #162443
Open
somewheresy wants to merge 2 commits into
Open
somewheresy wants to merge 2 commits into
somewheresy wants to merge 2 commits into
Conversation
somewheresy
requested review from
a team,
AA-Turner,
emmatyping,
erlend-aasland and
itamaro
as code owners
November 22, 2026 16:00
Encode and decode whole groups with NEON before handing the rest of the input to the existing scalar loops. Those loops still handle the tail, padding, invalid characters and errors, so results and exceptions are unchanged. NEON is part of the AArch64 baseline, so no build flags or runtime detection are needed.
somewheresy
force-pushed
the
gh-124951-neon
branch
from
November 22, 2026 16:02
7ab0470 to
b45a6da
Compare
Author
|
@python-cla-bot recheck |
Documentation build overview
|
Member
|
Marking as DO-NOT-MERGE as I think we need more discussion on the issue. |
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.
This is the NEON follow-up mentioned in gh-143262. On AArch64, base64 encode and decode in
binasciinow process whole groups with NEON, then hand off to the existing scalar loops.The scalar code is unchanged and still does the tail, padding, invalid characters, and all error reporting. The NEON part only consumes complete 3-byte / 4-char groups. On decode it stops before any 16- or 64-char block that contains a rejected character, and the scalar loop finds the exact spot. So output and exceptions match main for every input.
NEON is baseline on AArch64, so there are no new build flags, no configure changes, and no runtime CPU detection. Other platforms compile the same code as before.
Encode works with any alphabet (
TBLover the 64-byte table). Decode uses nibble lookups for the standard alphabet (Muła/Lemire) and the 256-byte reverse table for custom alphabets.I also have SSSE3/AVX2 versions, but I left them out because they need runtime dispatch, and per the discussion on the issue that should get a shared home first.
Performance
Apple M5 Max, default
./configure(no PGO/LTO), best of 9, GB/s of binary data:Under 24 bytes (encode) / 32 bytes (decode) it doesn't use NEON at all, so short inputs are unchanged.
Through the
base64module:b64encode()of 48 KiB goes from 11.1 to 2.1 µs, andb64decode()of 64 KiB from 11.4 to 3.8 µs.urlsafe_b64decode()gains less, sinceLib/base64.pyrunsbytes.translate()first; routing that throughalphabet=could be a separate change.Testing
test_binasciiandtest_base64pass, and-R 3:3is clean.test_base64_long_inputs. It covers every length up to 512 bytes with the standard alphabet and two custom ones (one ASCII, one using 0x80-0xBF), plus an invalid byte at every position. I checked that it fails when bugs are planted in the error check, the tail handling, or the custom-table path.binasciiandbase64, comparing results and exception messages. No differences.-Wall -Wextra -Wpedantic. An earlier version of this code built cleanly with MSVC for ARM64, but I don't have Windows on ARM hardware, so that hasn't been run.base64module: Link against SIMD library for 10x performance. #124951