Commit 35ed965f347 for php
commit 35ed965f3475232e49b7862d8c72524c17de7cb7
Author: Daniel Lemire <daniel@lemire.me>
Date: Thu Nov 26 16:29:10 2026 -0500
ext/mbstring: Convert the case of leading ASCII characters with SIMD
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 or NEON,
with a scalar fallback), and stops at the first non-ASCII byte. The general
loop handles the rest of the string. Title case is left to the general loop.
There is no separate scan of the whole string: the fast loop does the
conversion as it goes, and a non-ASCII string costs at most 63 bytes of work
that the general loop redoes.
The general loop converts 64 codepoints at a time, and the special casing of
the final Greek sigma looks back into the previous block. So the handover
happens at a multiple of 64 bytes, with the last 64 converted characters
already in converted_buf, and the output is the same as before.
This speeds up mb_strtolower(), mb_strtoupper(), mb_convert_case() and the
case-insensitive searches (mb_stripos() and co.) on ASCII and mostly-ASCII
UTF-8 strings.
diff --git a/UPGRADING b/UPGRADING
index 49eb47ea6de..de3f6c52f30 100644
--- a/UPGRADING
+++ b/UPGRADING
@@ -250,6 +250,9 @@ PHP 8.7 UPGRADE NOTES
. Improved performance of UTF-8 validation, used by mb_check_encoding() and
by the UTF-8 fast paths of mb_strlen(), mb_substr() and other functions.
It now uses AVX-512 on x86-64 when available, and NEON on ARM64.
+ . Improved performance of mb_strtolower(), mb_strtoupper(), mb_convert_case()
+ and the case-insensitive search functions (mb_stripos() and others) for
+ UTF-8 strings that are ASCII or start with ASCII characters.
- PDO:
. Improved performance of fetching rows with PDO::FETCH_BOTH, the default
diff --git a/ext/mbstring/php_unicode.c b/ext/mbstring/php_unicode.c
index ce255e40136..e113b4db353 100644
--- a/ext/mbstring/php_unicode.c
+++ b/ext/mbstring/php_unicode.c
@@ -279,6 +279,47 @@ static bool scan_back_for_cased_letter(uint32_t *begin, uint32_t *end)
return false;
}
+/* Copy the longest ASCII prefix of `in` to `out`, flipping the case of the
+ * letters from `first` to `first + 25`. Return the length of that prefix,
+ * which ends at the first byte >= 0x80 or at the end of the input. */
+static zend_never_inline size_t convert_case_ascii_prefix(const unsigned char *in, size_t len, unsigned char *out, unsigned char first)
+{
+ size_t i = 0;
+#if defined(__SSE2__)
+ const __m128i offset = _mm_set1_epi8((signed char)(SCHAR_MIN - first));
+ const __m128i threshold = _mm_set1_epi8(SCHAR_MIN + 26);
+ const __m128i flip = _mm_set1_epi8(0x20);
+ for (; i + 16 <= len; i += 16) {
+ const __m128i v = _mm_loadu_si128((const __m128i *)(in + i));
+ if (_mm_movemask_epi8(v)) {
+ break;
+ }
+ const __m128i is_letter = _mm_cmplt_epi8(_mm_add_epi8(v, offset), threshold);
+ _mm_storeu_si128((__m128i *)(out + i), _mm_xor_si128(v, _mm_and_si128(is_letter, flip)));
+ }
+#elif defined(__aarch64__) || defined(_M_ARM64)
+ const uint8x16_t first_v = vdupq_n_u8(first);
+ const uint8x16_t last_offset = vdupq_n_u8(25);
+ const uint8x16_t flip = vdupq_n_u8(0x20);
+ for (; i + 16 <= len; i += 16) {
+ const uint8x16_t v = vld1q_u8(in + i);
+ if (vmaxvq_u8(v) >= 0x80) {
+ break;
+ }
+ const uint8x16_t is_letter = vcleq_u8(vsubq_u8(v, first_v), last_offset);
+ vst1q_u8(out + i, veorq_u8(v, vandq_u8(is_letter, flip)));
+ }
+#endif
+ for (; i < len; i++) {
+ unsigned char c = in[i];
+ if (c >= 0x80) {
+ break;
+ }
+ out[i] = c ^ (((unsigned char)(c - first) < 26) << 5);
+ }
+ return i;
+}
+
MBSTRING_API zend_string *php_unicode_convert_case(php_case_mode case_mode, const char *srcstr, size_t in_len, const mbfl_encoding *src_encoding, const mbfl_encoding *dst_encoding, int illegal_mode, uint32_t illegal_substchar)
{
/* A Unicode codepoint can expand out to up to 3 codepoints when uppercased, lowercased, or title cased
@@ -293,6 +334,48 @@ MBSTRING_API zend_string *php_unicode_convert_case(php_case_mode case_mode, cons
mb_convert_buf buf;
mb_convert_buf_init(&buf, in_len + 1, illegal_substchar, illegal_mode);
+ /* For UTF-8, optimistically convert the leading ASCII characters with a fast
+ * loop, then let the general loop below handle the rest of the string.
+ * The general loop converts 64 codepoints at a time, and the special casing
+ * of the Greek letter sigma looks back at the previous 64 codepoints. So we
+ * hand over at a multiple of 64 bytes, with the last 64 converted characters
+ * in converted_buf: the result is exactly what the general loop alone gives.
+ * Title case is left to the general loop. */
+ if (src_encoding == &mbfl_encoding_utf8 && dst_encoding == &mbfl_encoding_utf8) {
+ unsigned char first = 0;
+ switch (case_mode) {
+ case PHP_UNICODE_CASE_UPPER:
+ case PHP_UNICODE_CASE_UPPER_SIMPLE:
+ first = 'a';
+ break;
+ case PHP_UNICODE_CASE_LOWER:
+ case PHP_UNICODE_CASE_LOWER_SIMPLE:
+ case PHP_UNICODE_CASE_FOLD:
+ case PHP_UNICODE_CASE_FOLD_SIMPLE:
+ first = 'A';
+ break;
+ default:
+ break;
+ }
+ if (first) {
+ size_t ascii_len = convert_case_ascii_prefix(in, in_len, buf.out, first);
+ if (ascii_len == in_len) {
+ buf.out += ascii_len;
+ return mb_convert_buf_result(&buf, dst_encoding);
+ }
+ ascii_len &= ~(size_t)63;
+ if (ascii_len) {
+ buf.out += ascii_len;
+ for (size_t i = 0; i < 64; i++) {
+ converted_buf[i] = buf.out[(ptrdiff_t)i - 64];
+ }
+ converted_end = converted_buf + 64;
+ in += ascii_len;
+ in_len -= ascii_len;
+ }
+ }
+ }
+
while (in_len) {
size_t out_len = src_encoding->to_wchar(&in, &in_len, wchar_buf, 64, &state);
ZEND_ASSERT(out_len <= 64);
diff --git a/ext/mbstring/tests/mb_convert_case_ascii_prefix.phpt b/ext/mbstring/tests/mb_convert_case_ascii_prefix.phpt
new file mode 100644
index 00000000000..5a0ae20e11f
--- /dev/null
+++ b/ext/mbstring/tests/mb_convert_case_ascii_prefix.phpt
@@ -0,0 +1,50 @@
+--TEST--
+mb_convert_case() with UTF-8 strings that start with ASCII characters
+--EXTENSIONS--
+mbstring
+--FILE--
+<?php
+/* UTF-8 case conversion handles a leading run of ASCII characters separately
+ * and hands over to the general code at a multiple of 64 bytes. Check
+ * strings whose first non-ASCII character falls on either side of that
+ * boundary. */
+mb_substitute_character(0x3F);
+$ascii = "The Quick Brown Fox Jumps Over the Lazy Dog 0123456789 '.:^`[]{}@~";
+for ($len = 0; $len <= 200; $len++) {
+ $prefix = substr(str_repeat($ascii, 4), 0, $len);
+ $upper = strtoupper($prefix);
+ $lower = strtolower($prefix);
+ $checks = [
+ [MB_CASE_UPPER, $prefix . "éß\xFFz", $upper . "ÉSS?Z"],
+ [MB_CASE_UPPER_SIMPLE, $prefix . "éß\xFFz", $upper . "Éß?Z"],
+ [MB_CASE_LOWER, $prefix . "ÉİZ", $lower . "éi̇z"],
+ [MB_CASE_LOWER_SIMPLE, $prefix . "ÉİZ", $lower . "éiz"],
+ [MB_CASE_FOLD, $prefix . "ÉßZ", $lower . "éssz"],
+ [MB_CASE_FOLD_SIMPLE, $prefix . "ÉßZ", $lower . "éßz"],
+ /* Final sigma depends on the character before it */
+ [MB_CASE_LOWER, $prefix . "a''Σ", $lower . "a''ς"],
+ [MB_CASE_LOWER, $prefix . " ''Σ", $lower . " ''σ"],
+ [MB_CASE_LOWER, $prefix . "a''ΣA", $lower . "a''σa"],
+ ];
+ foreach ($checks as [$mode, $in, $expected]) {
+ $out = mb_convert_case($in, $mode, 'UTF-8');
+ if ($out !== $expected) {
+ echo "Mode $mode, length $len: got ", bin2hex($out), ", expected ", bin2hex($expected), "\n";
+ }
+ }
+ if (mb_strtoupper($prefix, 'UTF-8') !== $upper || mb_strtolower($prefix, 'UTF-8') !== $lower) {
+ echo "ASCII string of length $len\n";
+ }
+}
+
+/* The sigma check looks back up to 64 characters into the ASCII prefix */
+var_dump(mb_strtolower(str_repeat("A", 64) . "Σ", 'UTF-8') === str_repeat("a", 64) . "ς");
+var_dump(mb_strtolower(str_repeat("A", 60) . str_repeat(".", 4) . "Σ", 'UTF-8') === str_repeat("a", 60) . "....ς");
+var_dump(mb_strtolower(str_repeat(" ", 127) . "Σ", 'UTF-8') === str_repeat(" ", 127) . "σ");
+echo "Done\n";
+?>
+--EXPECT--
+bool(true)
+bool(true)
+bool(true)
+Done