Skip to content

gh-129813, PEP 782: Use PyBytesWriter in FileIO.read() - #138955

Merged
vstinner merged 1 commit into
python:mainfrom
vstinner:pybyteswriter_fileio
Sep 15, 2025
Merged

vstinner merged 1 commit into
python:mainfrom
vstinner:pybyteswriter_fileio

Conversation

@vstinner

@vstinner vstinner commented Sep 15, 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 pyperf
import os

file = open(__file__, "rb", 0)
def func():
    file.seek(0)
    for _ in range(100):
        file.read(1)

runner = pyperf.Runner()
runner.bench_func('bench', func)

file.close()

Result: Mean +- std dev: [ref] 65.8 us +- 1.0 us -> [change] 63.2 us +- 1.0 us: 1.04x faster

cc @cmaloney

@vstinner
vstinner merged commit e814e6b into python:main Sep 15, 2025
@vstinner
vstinner deleted the pybyteswriter_fileio branch September 15, 2025 21:49
@vstinner

Copy link
Copy Markdown
Member Author

Merged. Thanks for the review @cmaloney.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

2 participants