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
|
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.
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