Skip to content

gh-129813, PEP 782: Use PyBytesWriter in ssl.MemoryBIO - #139113

Merged
vstinner merged 1 commit into
python:mainfrom
vstinner:pybyteswriter_memorybio
Sep 18, 2025
Merged

vstinner merged 1 commit into
python:mainfrom
vstinner:pybyteswriter_memorybio

Conversation

@vstinner

@vstinner vstinner commented Sep 18, 2025 •

Copy link
Copy Markdown
Member

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
Copy Markdown
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
@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

1 participant