Skip to content

[mypyc] Use smaller default buffer for BytesWriter#20574

Merged
JukkaL merged 1 commit into
masterfrom
bytes-writer-size
Jan 13, 2026
Merged

[mypyc] Use smaller default buffer for BytesWriter#20574
JukkaL merged 1 commit into
masterfrom
bytes-writer-size

Conversation

@JukkaL

@JukkaL JukkaL commented Jan 13, 2026

Copy link
Copy Markdown
Collaborator

This seems to make allocations and/or frees a lot faster, at least in this microbenchmark (on 3.14 it's over 2x faster with the smaller buffer):

def bench(b: bytes, n: int) -> None:
    for x in range(n):
        w = BytesWriter()
        w.write(b)
        w.getvalue()

n = 30 * 1000 * 1000
b = b"foo"

bench(b, n)

This seems to make allocations and/or frees a lot faster, at least
in this microbenchmark (on 3.14 it's over 2x faster with the smaller
buffer):
```
def bench(b: bytes, n: int) -> None:
    for x in range(n):
        w = BytesWriter()
        w.write(b)
        w.getvalue()

n = 30 * 1000 * 1000
b = b"foo"

bench(b, n)
```
@ilevkivskyi

Copy link
Copy Markdown
Member

Hm, I am curious would it make sense to make the same change in librt.internal? (I mean in #define START_SIZE 512)

@ilevkivskyi

Copy link
Copy Markdown
Member

Oh, nvm, your benchmark is for very small bytes object, I guess typical cache files are usually at least 1-2 KB. So maybe it is not relevant for librt.internal.

@JukkaL

JukkaL commented Jan 13, 2026

Copy link
Copy Markdown
Collaborator Author

Yeah, this only helps with very small bytes objects.

@JukkaL JukkaL merged commit ae16cff into master Jan 13, 2026
16 checks passed
@JukkaL JukkaL deleted the bytes-writer-size branch January 13, 2026 13:00
michaelm-openai pushed a commit to michaelm-openai/mypy that referenced this pull request Jan 16, 2026
This seems to make allocations and/or frees a lot faster, at least in
this microbenchmark (on 3.14 it's over 2x faster with the smaller
buffer):
```
def bench(b: bytes, n: int) -> None:
    for x in range(n):
        w = BytesWriter()
        w.write(b)
        w.getvalue()

n = 30 * 1000 * 1000
b = b"foo"

bench(b, n)
```
michaelm-openai pushed a commit to michaelm-openai/mypy that referenced this pull request Jan 16, 2026
This seems to make allocations and/or frees a lot faster, at least in
this microbenchmark (on 3.14 it's over 2x faster with the smaller
buffer):
```
def bench(b: bytes, n: int) -> None:
    for x in range(n):
        w = BytesWriter()
        w.write(b)
        w.getvalue()

n = 30 * 1000 * 1000
b = b"foo"

bench(b, n)
```
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.

3 participants