Skip to content

perf: ext/standard: Optimize strtr() array mode bitset and prefix scan - #25847

Open
iliaal wants to merge 1 commit into
php:masterfrom
iliaal:opt/strtr-array-bitset-scan
Open

iliaal wants to merge 1 commit into
php:masterfrom
iliaal:opt/strtr-array-bitset-scan

Conversation

@iliaal

@iliaal iliaal commented Nov 27, 2026

Copy link
Copy Markdown
Member

strtr() with an array of replacements sized its key-length bitset by the input length and tested every input byte against the set of first bytes. This keeps the bitsets on the stack, finds candidates with memchr() or a 16-byte SIMD compare when the keys start with 1 to 4 distinct bytes, and skips candidates whose second byte starts no key. That removes most of the per-byte work. Benchmarks are in the commit message.

strtr() with an array of replacements allocated a key-length bitset sized by
the input length, eight times larger than needed because it used one bit per
byte, and tested every input byte against the set of first bytes. Keep the
bitsets on the stack, allocating only when a key is 256 bytes or longer and
then sized by the longest key. Find candidates with memchr() when all keys
share one first byte and with a 16-byte SIMD compare for 2 to 4 first bytes,
and skip candidates whose second byte starts no key. Check ZEND_HAS_OOM()
before each appended replacement, which is where the loop allocates.

Neoverse-N1, GCC 13.3 -O2 NTS: 1 MB input with no match 32.7x, 20 word
replacements 1.25x (-36.6% instructions), 3 placeholders in a 250 KB
template 1.04x (-22.0% instructions), 4 single-byte escapes on a 20-byte
string unchanged (-1.9% instructions).

@ArtUkrainskiy ArtUkrainskiy left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

I measured this on master 43a7692 with GCC 15 and Clang 21 (Ryzen 9 5950X, flags of the official Docker image). Rare matches on long inputs are much faster: 64× fewer cycles on 1 MB without a match. Two input classes got slower though.

Change in CPU cycles per call against master, negative = fewer cycles (faster). The last two columns add the loop from my line comment:

call master, cycles (GCC 15) GCC 15 Clang 21 + loop, GCC 15 + loop, Clang 21
1 MB English text, {{name}} and {{city}}, no match 2,759,838 −98% −98% −98% −98%
1 KB English, &<>" → entities, 1 in 4 spaces replaced 5,760 −38% −38% −38% −35%
1 KB Chinese, 2 Chinese words 9,091 −46% −47% −46% −46%
1 MB English, 6 keys with 6 first bytes, no match 2,781,578 +14% +92% −21% −4.3%
64 KB English, &<>"' → entities 443,429 +8.0% +32% −8.7% +3.0%
10 KB English, 20 words 318,105 +2.4% +14% 0% +11%
64 KB Japanese, 17 kana (all E3) 1,254,029 +7.6% +12% +7.1% +10.0%
64 KB Russian, transliteration 2,234,978 +13% +13% +9.2% +14%

The last two rows have a candidate every few bytes, and each rejected one reloads the 16-byte block or calls memchr() again. Iterating the mask with mask &= mask - 1 might help, I haven't tried it. Output matches master on 60k random cases, also under ASan. Details: https://github.com/ArtUkrainskiy/php-src-bench/tree/main/reports/pr-25847-strtr-scan

Comment thread ext/standard/string.c
Comment on lines +3215 to 3220
} else {
if (!ZEND_BIT_TEST(bitset, (unsigned char) str[pos])) {
pos++;
continue;
}
}

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

With more than 4 first bytes every byte goes back through the mode checks at the top of the loop. Clang 21 makes that about 4 taken branches per byte. A tight loop here turns +92% into −4% on 1 MB without a match:

Suggested change
} else {
if (!ZEND_BIT_TEST(bitset, (unsigned char) str[pos])) {
pos++;
continue;
}
}
} else {
while (pos <= scan_end && !ZEND_BIT_TEST(bitset, (unsigned char) str[pos])) {
pos++;
}
if (pos > scan_end) {
break;
}
}

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.

2 participants