Repository navigation
Conversation
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).
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
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 withmemchr()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.