Repository navigation
Conversation
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.
Documentation build overview
|
picnixz
left a comment
There was a problem hiding this comment.
This code is small enough that I think it's ok. But I think I would rather revive my PR for detection where I could split it by different kind of detections that we would do at startup and share with the interpreter's state otherwise.
While I plan to remove SIMD support for BLAKE2 and HMAC-BLAKE2 because of linking problems, I can retain the rest of the CPU detection (possibly simplifying it).
| int state = _Py_atomic_load_int_relaxed(&base64_ssse3_state); | ||
| if (state == 0) { | ||
| int info[4]; | ||
| __cpuid(info, 1); |
There was a problem hiding this comment.
While it doesn't matter for leaf 1, let's use __cpuidex(info, 1, 0); so that CPython sources are consistent.
There was a problem hiding this comment.
instead of reviving your whole CPU detection PR, how about something simpler like this: gpshead@109ce29
(since you may be deleting the SIMD blake2 paths I did not have it add the feature detection for that)
my goal with that would be only to add feature detection code in there for specific things we may actually use rather than attempting to keep up with all possible definitions (per the comment in its internal .h file) and to keep it simple enough that no configure.ac checks are needed.
this PR or its -avx2 branch alternative rebased on top of that would drop their own local detection logic.
There was a problem hiding this comment.
Added some comments in the commit, but they don't show up.
The compile time check to # define _Py_CPU_HAS_AVX2() 1 may cause SIGILL on a non-compatible machine. Not sure if this is intended behavior.
Encode and decode base64 in
binasciiwith SSSE3 on x86-64. Blocks of 12 bytes (encode) or 16 characters (decode) go throughpshufb-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()andb64encode()withaltchars.Time per call in microseconds for
b2a_base64(data, newline=False)anda2b_base64()of the same data, on an AMD Zen 2 with GCC 15 (-O3, no PGO,binasciibuilt for baseline x86-64 so the run-time check is in use):At 16 and 32 bytes the call overhead dominates and the timings stay within about 5 ns of main.
urlsafe_b64encode()tracksb64encode()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
base64benchmark with the same builds,base64_largegoes from 2.1 ms to 0.55 ms (3.8x),base64_smallfrom 360 to 340 µs (6% faster) andurlsafe_base64_smallfrom 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_inputsis adapted from Justin Bennington's GH-162443.Co-authored with Claude Fable 5.2.
base64module: optimize via SIMD for performance #124951