Repository navigation
[mypyc] Use CPython bytes 'in' search if first byte gets many hits - #22528
Merged
Merged
Conversation
The memchr + memcmp search that was added in #22409 is fast on typical data, but when the first byte to search for is frequent, each iteration makes little progress. This seems fairly common, but in the worst case this was ~9x slower than CPython. Fall back to CPython search algorithm if there are many first byte hits that don't match the whole search string. On Python 3.10 we need to fall back to PySequence_Contains, but on newer versions there is a private CPython API we can use. Benchmark results on Linux x86-64 (time relative to master, lower is better): 1 MB Python source, b" raise NotImplemented!" (miss) 0.12x b"a" * 100_000, b"ab" (miss) 0.11x Short inputs (8-80 bytes) 0.86-0.99x The worst case is a dense region followed by a long sparse one, e.g. b"a" * 200 + b"x" * 1_000_000 with b"ab", which is now about as slow as the generic CPython search (~35x slower than master).
p-sawicki
approved these changes
Nov 20, 2026
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.
The memchr + memcmp search that was added in #22409 is fast on typical data, but when the first byte to search for is frequent, each iteration makes little progress. This seems fairly common, but in the worst case this was ~9x slower than CPython.
Fall back to CPython search algorithm if there are many first byte hits that don't match the whole search string. On Python 3.10 we need to fall back to PySequence_Contains, but on newer versions there is a private CPython API we can use.
Benchmark results on Linux x86-64 (time relative to master, lower is better):
b" raise NotImplemented!"(miss): 0.12xb"a" * 100_000,b"ab"(miss): 0.11xThe worst case is a dense region followed by a long sparse one, e.g.
b"a" * 200 + b"x" * 1_000_000andb"ab", which is now about as slow as the generic CPython search (~35x slower than master).