Repository navigation
Conversation
The SSSE3, AVX2, AVX-512 VBMI and NEON decoders now share one loop, following simdutf: each block of 64 bytes is translated to 6-bit values with a mask of the bytes outside the alphabet, and the alphabet characters of a block with whitespace or other ignorable bytes are compressed into a buffer of pending values. So line-wrapped input (MIME, PEM) stays on the vector path instead of falling back to the scalar loop at the first line break, and short input no longer goes through the scalar loop. Strict mode is handled in the same loop: whitespace is skipped, padding is checked in the last block, and only '=' or an invalid byte before the last block goes to the scalar loop. The NEON encoder uses a 64-entry table lookup, and the AVX-512 VBMI encoder handles the tail with masked loads and stores. The AVX-512 VBMI tier now also requires VBMI2 (vpcompressb). The plain AVX-512 decoder is removed: those CPUs use the AVX2 decoder. Output is unchanged, including every strict-mode rejection.
Without pending values, the last 0 to 63 bytes were decoded through the buffers of the vector loop, at the cost of store-forwarding stalls: on short input, the SSSE3 decoder was slower than before. They are now decoded 16 bytes at a time while they are in the alphabet, and the rest (usually the padding) one character at a time.
|
I used our benchmark framework (https://github.com/kocsismate/php-version-benchmarks) to check the performance changes. I used the following pieces of code: and (please note that both I got the following results on X86-64, using an AWS metal instance (see first table for more details): AWS x86_64 (c6id.metal)
PHP base64_decode - 50 iterations, 10 warmups, 15 requests (sec)
PHP base64_encode - 50 iterations, 10 warmups, 10 requests (sec)
The relative std dev % is much worse than it usually is. On a typical day, this should be below 0.15% in most cases when running our demo app benchmark (https://github.com/php/real-time-benchmark-data/blob/main/2026/2026_11_14_00_43_35_1_aws_x86_64_c6id.metal/result.md). Still, the performance improvement is definitely significant. |
The main loop used a 48-byte masked load on every iteration, which made large inputs about 10% slower than before the port on Emerald Rapids. Only the first 48 loaded bytes are used, so load 64 bytes while at least 64 bytes remain, and use a masked load for at most one final step.
The main decoding loop checks, for every 64-byte block, that no values are pending from earlier skipped bytes. With AVX-512 this test costs a mask-register move on the same port as the byte permutations, which made clean inputs of 16 KiB or more up to 6% slower than before the port on Ice Lake. Decode the leading blocks without the test, until the first byte outside the alphabet.
|
Thanks for running these! Since 32-byte inputs only show part of the picture, I wrote a more varied benchmark for your suite (kocsismate/php-version-benchmarks#17) and ran it on five machines, with every x86-64 kernel. Setup
Results with the default kernel (speedup over master)The x86-64 machines use AVX-512 VBMI, the ARM machines use NEON.
The "encode 32 B" result on the Mac (0.99×) is noise: measured on its own over 25 runs, it is 1.00×. This PR is a substantial boost when decoding 64 KiB MIME data. Up to 30x faster. Decoding clean input of one size, in a loop:
|
|
Having SIMD-accelerated base64 code is what lead to java2shell, are we sure that base64 handling really is a bottleneck anywhere, and that it's worth scarifying code reviewability to make it faster? |
PHP does have SIMD-accelerated base64 code right now. What the PR contributes is an update to the algorithm. The proposed update is an algorithm that has been widely distributed (almost all mainstream browsers + all mainstream JavaScript runtimes). It is a C port adapted to PHP. The reason why this is worthwhile is that the current SIMD acceleration only works well if the base64 input has no ignorable character. But, in practice, large base64 inputs will have ignorable characters. The gains can be large. I encourage thorough testing. I have a fuzzer that I am happy to share. I also encourage AI-based analysis. I reviewed Java4Shell: Daniel Lemire, "Java4Shell: when base64 decoding leads to remote code execution?," in Daniel Lemire's blog, November 18, 2026, https://lemire.me/blog/2026/11/18/java4shell-when-base64-decoding-leads-to-remote-code-execution/. It is interesting. |
|
Formal proof (with Lean4): https://github.com/lemire/phpbase64 |
|
For some context.... Look for the two PHP bars....
See Daniel Lemire, "Decoding base64 email attachments: a speed ranking of programming languages," in Daniel Lemire's blog, November 20, 2026, https://lemire.me/blog/2026/11/20/decoding-base64-email-attachments/. |
|
This PR can be made a bit faster in common cases if you have AVX-512, but I am not going to update the PR (because it is already such a huge gain). Interested parties should go see what were are doing at simdutf/simdutf#1100 |
|
Nice! |

Follow-up to the discussion in #24774.
The SSSE3, AVX2, AVX-512 VBMI and NEON base64 decoders now share one loop, following simdutf. Each 64-byte block is translated to 6-bit values together with a mask of the bytes outside the alphabet. When a block contains whitespace or other ignorable bytes, its alphabet characters are compressed into a buffer of pending values instead of falling back to the scalar loop. As a result:
=or an invalid byte before the last block goes to the scalar loop.Output is unchanged, including every strict-mode rejection.
Other changes:
vqtbl4q), and the AVX-512 VBMI encoder handles the tail with masked loads and stores.vpcompressb). The plain AVX-512 decoder is removed; those CPUs use the AVX2 decoder.UPGRADING
This PR adds an entry under "Performance Improvements → Standard" in
UPGRADING, so no follow-up PR is needed for the release notes.Performance
These are decode cycles per call measured with
perf stat, master → this PR. The numbers are from Ice Lake (Xeon 6338); Emerald Rapids gives similar results.On an Apple M4, decoding MIME input goes from 0.67 to 9.7 GB/s. Every kernel is at least as fast as master in every case measured: 24 B, 44 B, 1 KB, 64 KB clean and 64 KB MIME. The AVX2, AVX-512 and NEON decoders are within a few percent of simdutf.
Testing
ext/standard/tests/url/base64_decode_blocks.phpt, compares the result with a character-at-a-time reference decoder in both modes. It covers lengths 0 to 150, line breaks, padding, an inserted byte at every offset around block boundaries, and long inputs.References