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 ?
There was a problem hiding this comment.
Right, I checked it on 3 GiB of ASCII. Without the guards grapheme_strpos(), grapheme_strrpos() and grapheme_strripos() return the right position, and so does grapheme_stripos() with a positive offset. With -10 it segfaults, as @ChrisAbner found. grapheme_substr() returns false either way.
I see two ways:
- Keep the guard only in
grapheme_stripos(). Then the other three search functions work on such strings, likegrapheme_strlen()andgrapheme_strstr()already do. - Compute the
grapheme_stripos()start without the cast, as @ChrisAbner suggested, and drop all the guards. I tried it: all four return the right position on that string, with positive and negative offsets. The output matches master on the short strings, and the intl tests pass, also with ASan.
Both change one line against master. I'd go with 2, since it fixes the start instead of avoiding it. What do you think? I'll wait for @LamentXU123's review too before changing anything.
|
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_strlen(), Spanish 256 Bgrapheme_strlen(), English 1 KB, a "’" every 170 Bgrapheme_substr($s, 0, 8), ASCII 64 B, "é" at the endgrapheme_substr($s, 0, 8), ASCII 4 KB, "é" at the endgrapheme_substr($s, 0, 8), English 1 KB, a "’" every 170 Bgrapheme_substr($s, 0, 8), ASCII 1 KB, "é" at a random positiongrapheme_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. "At a random position" is 1024 such strings of 1 KB, one "é" each.
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 +9 cycles per call. Calls with a needle on a 1-byte haystack cost the most, up to 15%.Text that isn't ASCII goes to ICU as before. It stays within 4% of master from 16 bytes to 4 KB. The check stops at the first non-ASCII byte, and ICU then takes the whole string. So rare non-ASCII characters in ASCII text change little. With a "’" every 170 bytes or one "é" at a random position,
grapheme_strlen()stays within 2% of master.grapheme_substr($s, 0, 8)gains more the later the first one comes, up to 20% with "é" at the end of 4 KB.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