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).
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.

1 participant