Skip to content

[mypyc] Implement bytes.endswith - #20447

Merged
JukkaL merged 3 commits into
python:masterfrom
esarp:mypycBytesEndswith
Jan 15, 2026
Merged

JukkaL merged 3 commits into
python:masterfrom
esarp:mypycBytesEndswith

Conversation

@esarp

@esarp esarp commented Dec 19, 2025

Copy link
Copy Markdown
Collaborator

Rounding out #20387 and implementing bytes.endswith. Simple benchmark shows a ~6.4x improvement.

Tested with the following benchmark code:

import time


def bench(suffix: bytes, a: list[bytes], n: int) -> int:
    i = 0
    for x in range(n):
        for b in a:
            if b.endswith(suffix):
                i += 1
    return i


a = [b"foo", b"barasdfsf", b"foobar", b"ab", b"asrtert", b"sertyeryt"]
n = 5 * 1000 * 1000
suffix = b"foo"

bench(suffix, a, n)

t0 = time.time()
bench(suffix, a, n)
td = time.time() - t0
print(f"{td}s")

Output:

$ python bench.py         
0.9002199172973633s
$ python -c "import bench"
0.13828086853027344s
Comment thread mypyc/lib-rt/bytes_ops.c Outdated

return memcmp(self_buf + (self_len - subobj_len), subobj_buf, (size_t)subobj_len) == 0 ? 1 : 0;
}
_Py_IDENTIFIER(endswith);

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

Can you update to use the new approach introduced in #20460?

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

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

@JukkaL Should be resolved now

@esarp
esarp force-pushed the mypycBytesEndswith branch from d74ed5a to c4f5081 Compare January 9, 2026 19:24
@esarp
esarp force-pushed the mypycBytesEndswith branch from 4c55a57 to 22a2e04 Compare January 13, 2026 00:12
@JukkaL
JukkaL merged commit c0b89be into python:master Jan 15, 2026
michaelm-openai pushed a commit to michaelm-openai/mypy that referenced this pull request Jan 16, 2026
Rounding out python#20387 and implementing `bytes.endswith`. Simple benchmark
shows a ~6.4x improvement.

Tested with the following benchmark code:

```
import time


def bench(suffix: bytes, a: list[bytes], n: int) -> int:
    i = 0
    for x in range(n):
        for b in a:
            if b.endswith(suffix):
                i += 1
    return i


a = [b"foo", b"barasdfsf", b"foobar", b"ab", b"asrtert", b"sertyeryt"]
n = 5 * 1000 * 1000
suffix = b"foo"

bench(suffix, a, n)

t0 = time.time()
bench(suffix, a, n)
td = time.time() - t0
print(f"{td}s")
```

Output:

```
$ python bench.py         
0.9002199172973633s
$ python -c "import bench"
0.13828086853027344s
```
michaelm-openai pushed a commit to michaelm-openai/mypy that referenced this pull request Jan 16, 2026
Rounding out python#20387 and implementing `bytes.endswith`. Simple benchmark
shows a ~6.4x improvement.

Tested with the following benchmark code:

```
import time


def bench(suffix: bytes, a: list[bytes], n: int) -> int:
    i = 0
    for x in range(n):
        for b in a:
            if b.endswith(suffix):
                i += 1
    return i


a = [b"foo", b"barasdfsf", b"foobar", b"ab", b"asrtert", b"sertyeryt"]
n = 5 * 1000 * 1000
suffix = b"foo"

bench(suffix, a, n)

t0 = time.time()
bench(suffix, a, n)
td = time.time() - t0
print(f"{td}s")
```

Output:

```
$ python bench.py         
0.9002199172973633s
$ python -c "import bench"
0.13828086853027344s
```
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

2 participants