[php-src] PR #25850: ext/intl: Check 8 bytes at a time in grapheme_ascii_check()

From: Date: Fri, 27 Nov 2026 18:44:19 +0000
Subject: [php-src] PR #25850: ext/intl: Check 8 bytes at a time in grapheme_ascii_check()
Groups: php.git-pulls 
Request: Send a blank email to git-pulls+get-41136@lists.php.net to get a copy of this message
Pull Request: https://github.com/php/php-src/pull/25850 Author: ArtUkrainskiy grapheme_strlen(), grapheme_substr(), grapheme_strpos() and five other grapheme functions first call grapheme_ascii_check(). When it says the string is ASCII without CR LF, they can work on bytes without ICU. The check read the string one byte at a time, and most callers run it on the whole string. Now it reads 8 bytes at a time. Bytes above 0x7f show up in the 0x80 bits of the word. For CR LF it also loads the word one byte later: byte i of the two words is p[i] and p[i + 1], so (w ^ 0x0d..) | (next ^ 0x0a..) has a zero byte exactly at a CR LF. The last 0–8 bytes go through a byte loop. AMD Ryzen 9 5950X, negative = fewer cycles (faster): | call | master, CPU cycles per call (GCC 15.2) | GCC 15.2 | clang 21.1 | |---|--:|--:|--:| | grapheme_strlen(), ASCII 1 B | 36 | +9% | +4% | | grapheme_strlen(), ASCII 5 B | 44 | −2% | −1% | | grapheme_strlen(), ASCII 16 B | 69 | −28% | −25% | | grapheme_strlen(), ASCII 64 B | 179 | −63% | −61% | | grapheme_strlen(), ASCII 256 B | 564 | −75% | −75% | | grapheme_strlen(), ASCII 1 KB | 2,105 | −77% | −78% | | grapheme_strlen(), ASCII 4 KB | 8,264 | −80% | −80% | | grapheme_strlen(), ASCII, mixed lengths 1–24 B | 100 | −29% | −31% | | grapheme_strlen(), ASCII 1 KB, CR LF every 80 B | 63,529 | 0% | 0% | | grapheme_strlen(), Chinese 16 B | 1,564 | −4% | +4% | | grapheme_strlen(), Chinese 4 KB | 99,615 | 0% | 0% | | grapheme_substr($s, 0, 8), ASCII 64 B, "é" at the end | 2,447 | −5% | −3% | | grapheme_substr($s, 0, 8), ASCII 4 KB, "é" at the end | 32,876 | −20% | −20% | | grapheme_substr($s, 0, 8), ASCII 5 B | 103 | −9% | −3% | | grapheme_substr($s, 0, 8), ASCII, mixed lengths 1–24 B | 159 | −7% | −8% | | grapheme_strpos($s, 'zz'), ASCII 1 B | 56 | +13% | +12% | | grapheme_strpos($s, 'zz'), ASCII 4 KB | 9,319 | −70% | −71% | | grapheme_strpos($s, 'zz'), ASCII, mixed lengths 1–24 B | 145 | −11% | −12% | | grapheme_extract($s, 8), ASCII 5 B | 110 | +1% | 0% | "Mixed lengths" is 1024 ASCII strings of 1–24 bytes, the next one in each call. On ASCII the gain grows with the length. For grapheme_strlen() it levels off at 75–80% from 256 bytes. Very short strings can get slower. When the same 1–8 byte string is checked in every call, it's −11 to +8 cycles per call. Calls with a needle on a 1-byte haystack cost the most, up to 13%. Text that isn't ASCII goes to ICU as before. It stays within 4% of master from 16 bytes to 4 KB. A non-ASCII byte at the end of ASCII is found sooner, so grapheme_substr() gains more on longer strings there. grapheme_extract() checks only size + 1 bytes, so from 9 bytes on its gain doesn't depend on the length. The check kept the length in an int. It now returns it as zend_long. grapheme_strpos(), grapheme_stripos(), grapheme_strrpos(), grapheme_strripos() and grapheme_substr() use int32_t offsets on their ASCII path, so they skip it for strings over INT32_MAX bytes, as they did on master for 2–4 GiB. On 2³¹ + 8 bytes of ASCII, grapheme_strlen() and grapheme_strstr() now return the right result instead of an error. The other functions answer as on master. The old loop also read the byte after the length. That never changed a result. For grapheme_extract() that byte sits past the bytes it returns. The output matches master on 26,470 strings in all eight functions, 20,195 of them for grapheme_extract(), on GCC, clang, ZTS and ASan + UBSan. The ext/intl tests pass, and on ASan the same seven oom_* tests fail on master too. The existing grapheme tests caught only one of the six bugs I put into the code, so I added a test that catches all of them. I haven't measured on Intel or tested on Windows or big-endian. Full numbers and scripts: https://github.com/ArtUkrainskiy/php-src-bench/tree/main/reports/grapheme-ascii-check

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