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

gh-124951: Use SSSE3 for base64 in binascii on x86-64 - #162829

Draft
gpshead wants to merge 3 commits into
python:mainfrom
gpshead:binascii-base64-ssse3
Draft

gpshead wants to merge 3 commits into
python:mainfrom
gpshead:binascii-base64-ssse3

Conversation

@gpshead

@gpshead gpshead commented Nov 29, 2026 •

Copy link
Copy Markdown
Member

Encode and decode base64 in binascii with SSSE3 on x86-64. Blocks of 12 bytes (encode) or 16 characters (decode) go through pshufb-based kernels, using the technique from Muła and Lemire's "Faster Base64 Encoding and Decoding using AVX2 Instructions" at 128 bits. The existing scalar loops finish the tail and still handle padding, invalid characters and errors, so results and exceptions are unchanged.

SSSE3 is the target because effectively every x86-64 machine has it (the final shipment of CPUs without it was in 2014), which makes this the simplest change that speeds base64 up for everyone. A build that already targets x86-64-v2 or later calls the kernels directly (the RHEL 9 default; some CSP distros & RHEL 10 default to -v3). A baseline x86_64 v1 build checks the CPU at run time; that check only matters on hardware extremely unlikely to still be in use but we keep it to be pedantically correct given the amount of default x86-64-v1 compilation and shipped binaries still in existence.

Decoding uses the kernel for the standard alphabet. Encoding uses it for any alphabet whose first 62 characters are the standard ones, which covers urlsafe_b64encode() and b64encode() with altchars.

Time per call in microseconds for b2a_base64(data, newline=False) and a2b_base64() of the same data, on an AMD Zen 2 with GCC 15 (-O3, no PGO, binascii built for baseline x86-64 so the run-time check is in use):

data size encode, main encode, PR decode, main decode, PR
64 B 0.15 0.14 1.1x 0.12 0.096 1.2x
128 B 0.18 0.15 1.2x 0.15 0.11 1.4x
256 B 0.23 0.16 1.4x 0.22 0.13 1.7x
512 B 0.37 0.20 1.9x 0.39 0.20 1.9x
1 KiB 0.62 0.27 2.3x 0.69 0.29 2.4x
4 KiB 2.0 0.61 3.3x 2.4 0.75 3.2x
64 KiB 30 6.7 4.5x 35 9.4 3.7x

At 16 and 32 bytes the call overhead dominates and the timings stay within about 5 ns of main. urlsafe_b64encode() tracks b64encode() at every size. Line-wrapped input gains less on decode (decodebytes() only goes from 1.5 to 2.8 GB/s at 64 KiB) because each newline sends the decoder back through the scalar path.

On pyperformance's base64 benchmark with the same builds, base64_large goes from 2.1 ms to 0.55 ms (3.8x), base64_small from 360 to 340 µs (6% faster) and urlsafe_base64_small from 420 to 400 µs (5% faster). The small variants are weighted towards 20-byte inputs, where the call overhead dominates.

What about avx2 or avx512vmbi ?

A similar (same algorithm) but more complicated feature detection AVX2 version (see my -avx2 branch) is about twice as fast again on large inputs, but on small ones it only moves the sizes at which encoding becomes 2x faster from roughly 600 bytes down to roughly 400 bytes, and for decoding from roughly 550 bytes to roughly 200 bytes. pyperformance for the large case is as expected, for the small cases they improve by 1-2%. An AVX512VMBI based implementation can be twice as fast as AVX2 on large data. If choosing this SSSE3 PR, we leave large data performance with more significant conditional CPU vector abilities dispatch logic up to third party PyPI packages to provide to avoid overcomplicating our code. Third party implementations are also likely to get more complicated accelerate newline delimited split common cases far more; beyond what we'd want to maintain.

The arm64 aarch64 equivalent PR GH-162443 also exists (see the issue). It'd be great to share implementation logic... but that winds up being more trouble than it is worth.

Unfortunately, despite common seeming logic primitives involved that CPU vector units all now support, neither C nor modern Rust offer a standard way to express the operations such that generating multiple implementation "kernels" for various hardware variants could be offloaded to them from common code. The LLM is now our compiler: "make base64 fast for platform Z".

Draft until issue discussion happens.

test_base64_long_inputs is adapted from Justin Bennington's GH-162443.

Co-authored with Claude Fable 5.2.

Encode and decode blocks of standard-alphabet base64 with SSSE3 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.

SSSE3 is part of x86-64-v2.  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().  This
covers GCC and Clang; other compilers keep the scalar code.

For 64 KiB inputs on an AMD Zen 2 with GCC 15, b2a_base64() goes from
2.2 to 9.5 GB/s and a2b_base64() from 1.8 to 6.9 GB/s.

test_base64_long_inputs is adapted from Justin Bennington's pythonGH-162443.

Co-authored with Claude Fable 5.2.
Extend the compile-time gate to x64 Windows builds.  MSVC accepts the
SSSE3 intrinsics without a target option, so the kernels need no
attribute there; clang-cl still gets the function target attribute.

__builtin_cpu_supports() needs support from the compiler's runtime
library, which the MSVC runtime does not provide, so on those toolchains
the run-time check asks CPUID directly and caches the answer.

Not yet built with MSVC.  Checked by compiling with Clang for
x86_64-pc-windows-msvc and by running the CPUID branch on Linux with a
stand-in for __cpuid().

Co-authored with Claude Fable 5.2.
The SSSE3 encoder depends on the alphabet only through the two table
entries for the values 62 and 63.  Fill those in from the alphabet in
use, and run the kernel for any alphabet whose first 62 characters are
the standard ones.  That covers base64.urlsafe_b64encode() and
base64.b64encode() with altchars.

Decoding is unchanged: its lookup tables are tied to '+' and '/'.

For 64 KiB inputs on an AMD Zen 2 with GCC 15, urlsafe_b64encode() goes
from 2.2 to 9.5 GB/s.

Co-authored with Claude Fable 5.2.
@gpshead gpshead added performance Performance or resource usage extension-modules C modules in the Modules dir labels Nov 29, 2026
@gpshead gpshead self-assigned this Nov 29, 2026
@bedevere-app bedevere-app Bot added the type-feature A feature request or enhancement label Nov 29, 2026
@read-the-docs-community

Copy link
Copy Markdown

Documentation build overview

📚 cpython-previews | 🛠️ Build #36237591 | 📁 Comparing 2fdb8b8 against main (6e8fc18)

  🔍 Preview build  

2 files changed
± whatsnew/3.16.html
± whatsnew/changelog.html

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

extension-modules C modules in the Modules dir performance Performance or resource usage type-feature A feature request or enhancement

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant