Skip to content

Conversation

@JukkaL
Copy link
Collaborator

@JukkaL JukkaL commented Jan 13, 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)

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
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
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
Copy link
Collaborator Author

JukkaL commented Jan 13, 2026

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.

4 participants