[php-src] PR #25812: ext/mbstring: Convert the case of leading ASCII characters with SIMD

From: Date: Thu, 26 Nov 2026 22:58:33 +0000
Subject: [php-src] PR #25812: ext/mbstring: Convert the case of leading ASCII characters with SIMD
Groups: php.git-pulls 
Request: Send a blank email to git-pulls+get-41085@lists.php.net to get a copy of this message
Pull Request: https://github.com/php/php-src/pull/25812 Author: lemire Follow-up to the discussion in #25473 about using SIMD in mbstring's case conversion. When both the input and output encodings are UTF-8, php_unicode_convert_case() now converts the leading ASCII characters 16 bytes at a time (SSE2 on x86-64, NEON on arm64, with a scalar loop elsewhere and for the tail). We use the fact that all x64 processors support SSE2. It stops at the first non-ASCII byte, and the existing loop handles the rest of the string. The string is not scanned first: the fast loop converts as it goes. On a non-ASCII string, the existing loop redoes at most 63 bytes of that work. **Final sigma.** The existing loop converts 64 codepoints at a time, and the special casing of the final Greek sigma looks back into the previous block's output (converted_buf). So the fast loop hands over at a multiple of 64 bytes, with the last 64 converted characters copied into converted_buf. The output is therefore exactly what the existing loop alone produces, including for long runs of case-ignorable characters. **Scope.** Upper, lower and fold, both full and simple. Title case still goes through the existing loop. This covers mb_strtolower(), mb_strtoupper(), mb_convert_case() and the case-insensitive searches (mb_stripos(), mb_stristr(), ...) when the string is UTF-8. ## Benchmarks Best of 5 runs, ns per call, UTF-8 input. | input | Apple M4 (NEON) before → after | Xeon 6548N (SSE2) before → after | |---|---|---| | ASCII, 8 B, lower | 45 → 34 | 69 → 45 | | ASCII, 32 B, lower | 78 → 32 | 131 → 42 | | ASCII, 1 KB, lower | 1498 → 56 (27x) | 3152 → 81 (39x) | | ASCII, 1 KB, upper | 1265 → 56 (23x) | 2757 → 82 (34x) | | ASCII, 64 KB, lower | 92406 → 1618 (57x) | 196568 → 2626 (75x) | | é at 50%, 1 KB, lower | 1546 → 897 | 3086 → 1756 | | Latin-1 text, 1 KB, upper | 1729 → 1642 | 3085 → 3130 | | é first, 1 KB, upper | 1375 → 1381 | 2765 → 2800 | | Greek, 1 KB, upper | 1682 → 1711 | 2649 → 2658 | | "Café", upper | 42 → 44 | 60 → 63 | The worst case is a short string with a non-ASCII character near the start, such as "Café": about 2–3 ns slower, from the function call and the discarded prefix work. The fast loop is kept out of line (zend_never_inline). Inlining it changed GCC's code generation for the existing loop and slowed non-ASCII uppercase by about 15% on x86. ## Testing - New test mb_convert_case_ascii_prefix.phpt. It puts the first non-ASCII character at every offset from 0 to 200, for every mode, including final-sigma cases around the 64-byte handover. - Differential fuzzing against an unpatched build, on arm64 and x86-64: random mixes of ASCII, case-ignorable runs, sigma, special-casing characters and invalid bytes, in all 8 case modes plus mb_stripos(). About 100,000 strings, identical output. - ext/mbstring tests pass on arm64 (macOS) and x86-64 (Linux).

« previous php.git-pulls (#41085) next »