Skip to content

[mypyc] Use CPython bytes 'in' search if first byte gets many hits - #22528

Merged
JukkaL merged 1 commit into
masterfrom
mypyc-bytes-contains-pybytes-find-rest
Nov 20, 2026
Merged

JukkaL merged 1 commit into
masterfrom
mypyc-bytes-contains-pybytes-find-rest

Conversation

@JukkaL

@JukkaL JukkaL commented Nov 20, 2026

Copy link
Copy Markdown
Collaborator

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 and b"ab", which is now about as slow as the generic CPython search (~35x slower than master).

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).
@JukkaL
JukkaL merged commit dd73c5d into master Nov 20, 2026
18 checks passed
@JukkaL
JukkaL deleted the mypyc-bytes-contains-pybytes-find-rest branch November 20, 2026 11:38
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants