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. |
gpshead
added a commit
to gpshead/cpython
that referenced
this pull request
Nov 29, 2026
Encode 24 bytes or decode 32 characters of base64 per step with AVX2
when the CPU supports it, then hand the rest of the input to the
existing scalar loops. Those loops still handle inputs shorter than one
step, the tail, padding, invalid characters and errors, so results and
exceptions are unchanged. There is no intermediate 128-bit code path.
AVX2 is part of x86-64-v3. A build that already targets it calls the
kernels directly; otherwise they are compiled with a function target
attribute and selected at run time, with __builtin_cpu_supports() for
GCC and Clang and with CPUID for MSVC and clang-cl, where the check also
confirms that the OS saves the YMM registers.
The decoder handles the standard alphabet only. The encoder takes any
alphabet whose first 62 characters are the standard ones, which covers
base64.urlsafe_b64encode() and base64.b64encode() with altchars.
On an AMD Zen 2 with GCC 15, in GB/s for 64 KiB inputs:
scalar AVX2
b2a_base64 2.2 18
a2b_base64 1.9 14
urlsafe_b64encode 2.2 18
and per call in nanoseconds for small inputs:
b2a_base64 a2b_base64
size scalar AVX2 scalar AVX2
64 B 150 140 120 110
128 B 180 140 150 110
256 B 230 150 220 120
1 KiB 630 220 680 220
test_base64_long_inputs is adapted from Justin Bennington's pythonGH-162443.
Co-authored with Claude Fable 5.2.
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: optimize via SIMD for performance #124951