Repository navigation
ext/intl: Check 8 bytes at a time in grapheme_ascii_check() - #25850
ArtUkrainskiy wants to merge 1 commit into
Conversation
grapheme_strlen(), grapheme_substr(), grapheme_strpos() and five other grapheme functions call grapheme_ascii_check() to take their ASCII path. It read the string one byte at a time. It now reads 8 bytes at a time. A byte above 0x7f sets a bit of 0x80 in the word. A CR LF pair is a zero byte in (word ^ 0x0d..) | (word one byte later ^ 0x0a..). On ASCII strings of 9 bytes and more every measured function takes fewer cycles, and the gain grows with the length. grapheme_strlen() takes 24-27% fewer on 16 bytes, 61-63% fewer on 64 bytes, 75% fewer on 256 bytes and 80% fewer on 4 KB, and 27-33% fewer on 1-24 byte strings in a random order. Across the measured functions, repeating one string of 1-8 bytes costs -11 to +9 cycles per call (-11% to +15%). 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 longer than INT32_MAX bytes, as they did on master for 2-4 GiB. grapheme_strlen() and grapheme_strstr() now take the ASCII path on such strings and return the right result.
|
nice I just had a quick look and will review a bit later tonight, @LamentXU123 pls review since you know about optimisations :) |
| /* the offset is 'grapheme count offset' so it still might be invalid - we'll check it later */ | ||
|
|
||
| is_ascii = ( grapheme_ascii_check((unsigned char*)haystack, haystack_len) >= 0 ); | ||
| is_ascii = haystack_len <= INT32_MAX && grapheme_ascii_check((unsigned char*)haystack, haystack_len) >= 0; |
There was a problem hiding this comment.
seems to me your guard for INT32_MAX is only really needed here, wdyt ?
|
I tested the guards with a 2³¹ + 16 byte ASCII haystack (release build, no ASan):
Keeping the master behavior is fine too. If you'd rather have the search functions work on such strings, like size_t noffset = offset >= 0 ? (size_t)offset : haystack_len - (size_t)-(zend_long)offset;With that and no guards, the four search functions return the right result on that string, and the ext/intl tests pass. I also ran the old and new loops on exact-size buffers under ASan and UBSan, including every string up to 9 bytes, with the 64-bit loads byte-swapped to simulate big-endian. Same results. |
grapheme_strlen(),grapheme_substr(),grapheme_strpos()and five other grapheme functions first callgrapheme_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
0x80bits of the word. For CR LF it also loads the word one byte later: byteiof the two words isp[i]andp[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):
grapheme_strlen(), ASCII 1 Bgrapheme_strlen(), ASCII 5 Bgrapheme_strlen(), ASCII 16 Bgrapheme_strlen(), ASCII 64 Bgrapheme_strlen(), ASCII 256 Bgrapheme_strlen(), ASCII 1 KBgrapheme_strlen(), ASCII 4 KBgrapheme_strlen(), ASCII, mixed lengths 1–24 Bgrapheme_strlen(), ASCII 1 KB, CR LF every 80 Bgrapheme_strlen(), Chinese 16 Bgrapheme_strlen(), Chinese 4 KBgrapheme_substr($s, 0, 8), ASCII 64 B, "é" at the endgrapheme_substr($s, 0, 8), ASCII 4 KB, "é" at the endgrapheme_substr($s, 0, 8), ASCII 5 Bgrapheme_substr($s, 0, 8), ASCII, mixed lengths 1–24 Bgrapheme_strpos($s, 'zz'), ASCII 1 Bgrapheme_strpos($s, 'zz'), ASCII 4 KBgrapheme_strpos($s, 'zz'), ASCII, mixed lengths 1–24 Bgrapheme_extract($s, 8), ASCII 5 B"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 onlysize + 1bytes, 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 aszend_long.grapheme_strpos(),grapheme_stripos(),grapheme_strrpos(),grapheme_strripos()andgrapheme_substr()useint32_toffsets on their ASCII path, so they skip it for strings overINT32_MAXbytes, as they did on master for 2–4 GiB. On 2³¹ + 8 bytes of ASCII,grapheme_strlen()andgrapheme_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 forgrapheme_extract(), on GCC, clang, ZTS and ASan + UBSan. The ext/intl tests pass, and on ASan the same sevenoom_*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