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

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.

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