[php-src] master: ext/mbstring: Align AVX2 loads for long strings

From: Date: Thu, 26 Nov 2026 01:05:48 +0000
Subject: [php-src] master: ext/mbstring: Align AVX2 loads for long strings
Groups: php.cvs 
Request: Send a blank email to php-cvs+get-144197@lists.php.net to get a copy of this message
Author: Daniel Lemire (lemire) Committer: T Pusher: alexdowad Date: 2026-11-26T10:01:09+09:00 Commit: https://github.com/php/php-src/commit/5a4d76b9bf38d8eabf4004f21f168c6aad76fe2c Raw diff: https://github.com/php/php-src/commit/5a4d76b9bf38d8eabf4004f21f168c6aad76fe2c.diff ext/mbstring: Align AVX2 loads for long strings The content of a long zend_string starts 24 bytes past a 64-byte boundary, so half of the 32-byte loads crossed a cache line. As in the AVX-512 kernel, check one unaligned 64-byte block, then continue from the next 64-byte boundary with the 3 preceding bytes as context. On an Ice Lake server, long ASCII strings were about 7% slower than with the old AVX2 code; they are now about 20% faster. Changed paths: M ext/mbstring/mbstring.c Diff: diff --git a/ext/mbstring/mbstring.c b/ext/mbstring/mbstring.c index 4277f081a6ef..a04e941c11c3 100644 --- a/ext/mbstring/mbstring.c +++ b/ext/mbstring/mbstring.c @@ -5708,6 +5708,20 @@ static bool mb_fast_check_utf8_avx2(zend_string *str) __m256i prev_input = _mm256_setzero_si256(); __m256i prev_incomplete = _mm256_setzero_si256(); + /* Align the loads on 64-byte boundaries for long strings, as in mb_fast_check_utf8_avx512 */ + uintptr_t misalignment = (uintptr_t)p % 64; + if (e - p >= 2048 && misalignment != 0 && misalignment <= 61) { + __m256i in0 = _mm256_loadu_si256((const __m256i*)p); + __m256i in1 = _mm256_loadu_si256((const __m256i*)(p + 32)); + MB_UTF8_CHECK_AVX2(in0, prev_input); + MB_UTF8_CHECK_AVX2(in1, in0); + p += 64 - misalignment; + uint32_t ctx = 0; + memcpy(&ctx, p - 3, 3); + prev_input = _mm256_inserti128_si256(_mm256_setzero_si256(), _mm_slli_si128(_mm_cvtsi32_si128(ctx), 13), 1); + prev_incomplete = _mm256_subs_epu8(prev_input, max_value); + } + while (e - p >= 64) { /* The error vector is sticky, so it suffices to test it once in a while */ const unsigned char *stop = (e - p >= 1024 + 64) ? p + 1024 : e - 63;

« previous php.cvs (#144197) next »