Skip to content

ext/standard: Port the simdutf base64 decoders - #25476

Open
lemire wants to merge 5 commits into
php:masterfrom
lemire:base64-simdutf
Open

lemire wants to merge 5 commits into
php:masterfrom
lemire:base64-simdutf

Conversation

@lemire

@lemire lemire commented Nov 16, 2026 •

Copy link
Copy Markdown
Contributor

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:

  • line-wrapped input (MIME, PEM) stays on the vector path; today it drops to the scalar loop at the first line break;
  • short input (keys, tokens) no longer goes through the scalar loop;
  • strict mode is handled in the same loop: whitespace is skipped and padding is checked in the last block. Only = or an invalid byte before the last block goes to the scalar loop.

Output is unchanged, including every strict-mode rejection.

Other changes:

  • The NEON encoder uses a 64-entry table lookup (vqtbl4q), 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.

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.

24 B 44 B
SSSE3 71 → 65 92 → 84
AVX2 135 → 58 226 → 78
AVX-512 VBMI 162 → 69 275 → 92

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

  • A new test, 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.
  • A differential fuzzer compared every kernel with the scalar decoder under ASan and UBSan: x86-64 (SSSE3, AVX2, AVX-512 VBMI), arm64 (NEON, under qemu), 32-bit x86 and scalar. It ran about 150M generated cases plus 136M libFuzzer executions with no mismatch.

References

  • Wojciech Muła, Daniel Lemire, Faster Base64 Encoding and Decoding Using AVX2 Instructions, ACM Transactions on the Web 12 (3), 2018. https://doi.org/10.1145/3132709
  • Wojciech Muła, Daniel Lemire, Base64 encoding and decoding at almost the speed of a memory copy, Software: Practice and Experience 50 (2), 2020. https://doi.org/10.1002/spe.2777
  • Daniel Lemire, Fast Forgiving Base64 Decoding with SIMD Instructions, ACM Transactions on Architecture and Code Optimization, 2026. https://doi.org/10.1145/3859286

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

kocsismate commented Nov 16, 2026 •

Copy link
Copy Markdown
Member

I used our benchmark framework (https://github.com/kocsismate/php-version-benchmarks) to check the performance changes. I used the following pieces of code:

<?php

function bench_decode(string $input): void
{
    for ($i = 0; $i < 500_000; $i++) {
        $result = base64_decode($input);
    }

    var_dump($result);
}

bench_decode("MDEyMzQ1Njc4OWFiY2RlZjAxMjM0NTY3ODlhYmNkZWY=");

and

<?php

function bench_encode(string $input): void
{
    for ($i = 0; $i < 1_000_000; $i++) {
        $result = base64_encode($input);
    }

    var_dump($result);
}

bench_encode("0123456789abcdef0123456789abcdef");

(please note that both base64_encode() and base64_decode() are compile-time evaluable, that's why the above workaround was needed to actually run them).

I got the following results on X86-64, using an AWS metal instance (see first table for more details):

AWS x86_64 (c6id.metal)

Attribute Value
Environment aws
Instance type c6id.metal (dedicated)
Architecture x86_64
CPU Intel(R) Xeon(R) Platinum 8375C CPU @ 2.90GHz, 64 cores @ 2900 MHz
CPU settings disabled deeper C-states, disabled turbo boost, disabled hyper-threading
RAM 251 GB
Kernel 6.18.56-125.164.amzn2023.x86_64
OS Amazon Linux 2023.12.20261026
GCC 14.2.1
Binary layout strategy bolt-align
Time 2026-11-16 12:21:05 UTC

PHP base64_decode - 50 iterations, 10 warmups, 15 requests (sec)

PHP Min Max Std dev Rel std dev % Mean Mean diff % Median Median diff % Skewness Z-stat P-value Memory
PHP without simdutf 0.71817 0.74398 0.00686 0.95% 0.72460 0.00% 0.72196 0.00% 1.653 0.000 1.000 31.18 MB
PHP with simdutf 0.35227 0.36979 0.00349 0.98% 0.35618 -50.84% 0.35533 -50.78% 2.377 8.614 0.000 31.19 MB

PHP base64_encode - 50 iterations, 10 warmups, 10 requests (sec)

PHP Min Max Std dev Rel std dev % Mean Mean diff % Median Median diff % Skewness Z-stat P-value Memory
PHP without simdutf 0.46365 0.48502 0.00607 1.30% 0.46781 0.00% 0.46573 0.00% 2.320 0.000 1.000 31.18 MB
PHP with simdutf 0.29166 0.30492 0.00374 1.27% 0.29506 -36.93% 0.29365 -36.95% 1.987 8.614 0.000 31.19 MB

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

lemire commented Nov 17, 2026

Copy link
Copy Markdown
Contributor Author

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

  • Before: 90628d5 (master); after: 9aa3add (head of this PR).
  • Machines:
    • Intel Xeon Gold 6548N (Emerald Rapids), Linux, GCC 16.2
    • Intel Xeon Gold 6338 (Ice Lake), Linux, GCC 16.2
    • AMD EPYC 9R45 (Zen 5, AWS c8a.2xlarge), Ubuntu 24.04, the same binaries as on Emerald Rapids
    • AWS Graviton 5 (Neoverse V3, c9g.4xlarge), Ubuntu 24.04, GCC 14.2
    • Apple M4 Max, macOS, Apple clang 17
  • php-cgi with opcache and no JIT, --disable-all --enable-cgi, the CFLAGS of your suite (-O2 -fno-pic -fno-pie ...) on Linux and -O2 on macOS, pinned to one core on Linux.
  • Medians of 11 to 15 runs, alternating before and after. Run-to-run variation is mostly below 1%, up to 3% on the shortest cases.
  • base64_bench.php is the test from Add a base64 micro benchmark kocsismate/php-version-benchmarks#17. The second table comes from a separate script that decodes clean input of a single size in a loop.

Results with the default kernel (speedup over master)

The x86-64 machines use AVX-512 VBMI, the ARM machines use NEON.

Workload Emerald Rapids Ice Lake Zen 5 Apple M4 Graviton 5
encode 16 B 1.31× 1.23× 1.27× 1.00× 1.01×
decode 16 B 1.99× 1.76× 1.78× 1.67× 1.42×
encode 32 B 1.79× 1.68× 1.78× 0.99× 1.01×
decode 32 B 2.72× 2.22× 2.34× 2.07× 1.76×
decode 32 B (strict) 2.09× 2.16× 1.95× 1.91× 2.02×
encode 1 KiB 1.29× 1.07× 1.27× 1.33× 1.06×
decode 1 KiB 1.44× 1.38× 1.46× 1.34× 1.18×
decode 1 KiB (strict) 1.32× 1.37× 1.31× 1.34× 1.22×
encode 64 KiB 1.00× 1.00× 1.01× 1.39× 1.11×
decode 64 KiB 1.01× 1.00× 1.04× 1.18× 1.08×
decode 64 KiB (MIME, CRLF) 30.4× 27.6× 39.0× 14.4× 7.38×
encode 1 MiB 1.01× 1.00× 1.01× 1.39× 1.10×
decode 1 MiB (strict) 1.00× 1.00× 1.01× 1.18× 1.10×
encode mixed 1 B–8 KiB 1.30× 1.27× 1.29× 1.20× 1.07×
decode mixed 1 B–8 KiB 1.59× 1.41× 1.72× 1.32× 1.17×
JWT decode (base64url) 1.23× 1.24× 1.21× 1.24× 1.24×
data URI round trip 1.05× 1.04× 1.04× 1.19× 1.08×
Total 1.48× 1.38× 1.50× 1.37× 1.18×

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:

Size Emerald Rapids Ice Lake Zen 5 Apple M4 Graviton 5
256 B 1.66× 1.61× 1.52× 1.49× 1.33×
1 KiB 1.40× 1.37× 1.35× 1.34× 1.17×
4 KiB 1.12× 1.14× 1.20× 1.19× 1.10×
8 KiB 1.16× 1.14× 1.11× 1.25× 1.10×
16 KiB 1.07× 1.04× 1.01× 1.20× 1.09×
64 KiB 1.01× 1.00× 1.01× 1.19× 1.08×
256 KiB 1.00× 1.00× 0.99× 1.19× 1.08×

@jvoisin

jvoisin commented Nov 18, 2026

Copy link
Copy Markdown
Contributor

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?

@lemire

lemire commented Nov 19, 2026 •

Copy link
Copy Markdown
Contributor Author

@jvoisin

Having SIMD-accelerated base64 code is what lead to ... java2shell

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.

@lemire

lemire commented Nov 19, 2026

Copy link
Copy Markdown
Contributor Author

Formal proof (with Lean4): https://github.com/lemire/phpbase64

@lemire

lemire commented Nov 20, 2026

Copy link
Copy Markdown
Contributor Author

For some context.... Look for the two PHP bars....

javashell-mimespeed-2

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

@lemire

lemire commented Nov 23, 2026

Copy link
Copy Markdown
Contributor Author

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

@david-alvarez-rosa

Copy link
Copy Markdown

Nice!

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

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants