Skip to content

Conversation

@vstinner
Copy link
Member

@vstinner vstinner commented Sep 18, 2025

Replace PyBytes_FromStringAndSize(NULL, size) and _PyBytes_Resize() with the new public PyBytesWriter API.

Replace PyBytes_FromStringAndSize(NULL, size) and _PyBytes_Resize()
with the new public PyBytesWriter API.
@vstinner
Copy link
Member Author

Benchmark:

import ssl
import pyperf

bio = ssl.MemoryBIO()

def bench(size):
    bio.write(b'x' * size)
    data = bio.read(size)
    if len(data) != size:
        raise Exception

runner = pyperf.Runner()
for size in (1, 10, 100, 1_000, 10_000):
    runner.bench_func(f'read {size:,}B', bench, size)

Result:

Benchmark ref pep782
read 1B 267 ns 239 ns: 1.12x faster
read 10B 281 ns 276 ns: 1.02x faster
read 100B 276 ns 286 ns: 1.04x slower
read 1,000B 380 ns 374 ns: 1.02x faster
Geometric mean (ref) 1.02x faster

Benchmark hidden because not significant (1): read 10,000B

@vstinner vstinner merged commit d6a6fe2 into python:main Sep 18, 2025
47 checks passed
@vstinner vstinner deleted the pybyteswriter_memorybio branch September 18, 2025 12:25
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant