← 输入框|github.com|刷新
Skip to content

gh-124951: Use NEON for base64 in binascii on AArch64 - #162443

Open
somewheresy wants to merge 2 commits into
python:mainfrom
somewheresy:gh-124951-neon
Open

somewheresy wants to merge 2 commits into
python:mainfrom
somewheresy:gh-124951-neon

Conversation

@somewheresy

@somewheresy somewheresy commented Nov 22, 2026 •

Copy link
Copy Markdown

This is the NEON follow-up mentioned in gh-143262. On AArch64, base64 encode and decode in binascii now 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 (TBL over 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:

size encode main encode PR decode main decode PR
64 B 1.05 1.25 1.58 2.05
1 KiB 3.56 11.3 3.56 9.1
64 KiB 4.48 24.2 4.33 13.5
1 MiB 4.50 25.8 4.34 14.0

Under 24 bytes (encode) / 32 bytes (decode) it doesn't use NEON at all, so short inputs are unchanged.

Through the base64 module: b64encode() of 48 KiB goes from 11.1 to 2.1 µs, and b64decode() of 64 KiB from 11.4 to 3.8 µs. urlsafe_b64decode() gains less, since Lib/base64.py runs bytes.translate() first; routing that through alphabet= could be a separate change.

Testing

  • test_binascii and test_base64 pass, and -R 3:3 is clean.
  • Added 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.
  • Ran a differential test against an unpatched build: 360k random calls through binascii and base64, comparing results and exception messages. No differences.
  • Fuzzed the NEON functions against the scalar loops with libFuzzer and ASan/UBSan, about 200M inputs.
  • No warnings with gcc 14 or clang at -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.

@python-cla-bot

python-cla-bot Bot commented Nov 22, 2026 •

Copy link
Copy Markdown

All commit authors signed the Contributor License Agreement.

CLA signed

@bedevere-app bedevere-app Bot added the type-feature A feature request or enhancement label Nov 22, 2026
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

Copy link
Copy Markdown
Author

@python-cla-bot recheck

@read-the-docs-community

read-the-docs-community Bot commented Nov 22, 2026 •

Copy link
Copy Markdown

Documentation build overview

📚 cpython-previews | 🛠️ Build #36084695 | 📁 Comparing b28639b against main (fd71353)

  🔍 Preview build  

3 files changed
± library/warnings.html
± whatsnew/3.16.html
± whatsnew/changelog.html

@StanFromIreland

Copy link
Copy Markdown
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.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

awaiting review DO-NOT-MERGE type-feature A feature request or enhancement

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants