Skip to content

ext/intl: Check 8 bytes at a time in grapheme_ascii_check() - #25850

Open
ArtUkrainskiy wants to merge 1 commit into
php:masterfrom
ArtUkrainskiy:grapheme-ascii-check-words
Open

ArtUkrainskiy wants to merge 1 commit into
php:masterfrom
ArtUkrainskiy:grapheme-ascii-check-words

Conversation

@ArtUkrainskiy

@ArtUkrainskiy ArtUkrainskiy commented Nov 27, 2026 •

Copy link
Copy Markdown
Contributor

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 +11% +5%
grapheme_strlen(), ASCII 5 B 44 −2% 0%
grapheme_strlen(), ASCII 16 B 66 −27% −24%
grapheme_strlen(), ASCII 64 B 179 −63% −61%
grapheme_strlen(), ASCII 256 B 561 −75% −75%
grapheme_strlen(), ASCII 1 KB 2,100 −78% −78%
grapheme_strlen(), ASCII 4 KB 8,262 −80% −80%
grapheme_strlen(), ASCII, mixed lengths 1–24 B 100 −27% −33%
grapheme_strlen(), ASCII 1 KB, CR LF every 80 B 63,581 0% 0%
grapheme_strlen(), Chinese 16 B 1,563 −4% +4%
grapheme_strlen(), Chinese 4 KB 99,628 0% 0%
grapheme_strlen(), Spanish 256 B 16,110 0% 0%
grapheme_strlen(), English 1 KB, a "’" every 170 B 63,018 0% 0%
grapheme_substr($s, 0, 8), ASCII 64 B, "é" at the end 2,448 −5% −3%
grapheme_substr($s, 0, 8), ASCII 4 KB, "é" at the end 32,868 −20% −20%
grapheme_substr($s, 0, 8), English 1 KB, a "’" every 170 B 7,956 −4% −2%
grapheme_substr($s, 0, 8), ASCII 1 KB, "é" at a random position 8,773 −9% −9%
grapheme_substr($s, 0, 8), ASCII 5 B 104 −11% −1%
grapheme_substr($s, 0, 8), ASCII, mixed lengths 1–24 B 159 −9% −8%
grapheme_strpos($s, 'zz'), ASCII 1 B 56 +13% +15%
grapheme_strpos($s, 'zz'), ASCII 4 KB 9,333 −70% −71%
grapheme_strpos($s, 'zz'), ASCII, mixed lengths 1–24 B 144 −11% −12%
grapheme_extract($s, 8), ASCII 5 B 109 +2% +2%

"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 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

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.
@ArtUkrainskiy
ArtUkrainskiy marked this pull request as ready for review November 27, 2026 18:48
@devnexen

Copy link
Copy Markdown
Member

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;

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

seems to me your guard for INT32_MAX is only really needed here, wdyt ?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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:

  1. Keep the guard only in grapheme_stripos(). Then the other three search functions work on such strings, like grapheme_strlen() and grapheme_strstr() already do.
  2. 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.

@ChrisAbner

Copy link
Copy Markdown
Contributor

I tested the guards with a 2³¹ + 16 byte ASCII haystack (release build, no ASan):

  • Without any of the five guards, grapheme_stripos($s, 'XYZ', -10) segfaults: (int32_t)haystack_len + offset starts the search before the string. So the guard is needed there, as @devnexen said.
  • grapheme_strpos(), grapheme_strrpos() and grapheme_strripos() return the right position without the guard, with positive and negative offsets. With it they return false (U_BUFFER_OVERFLOW_ERROR from the UTF-16 path), as on master.
  • grapheme_substr() returns false either way, since grapheme_substr_ascii() already rejects lengths over INT32_MAX.

Keeping the master behavior is fine too. If you'd rather have the search functions work on such strings, like grapheme_strlen() and grapheme_strstr() now do, computing the grapheme_stripos() start without the cast is enough:

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.

@LamentXU123 LamentXU123 self-assigned this Nov 28, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants