Skip to content

Use PyBytesWriter API in io.RawIOBase.readall #140135

Description

@maurycy

Feature or enhancement

Proposal:

There's new PyBytesWriter() API:

There's an ongoing effort of introducing it in various places:

With its PyList_Append

r = PyList_Append(chunks, data);

and PyBytes_Join:

https://github.com/python/cpython/blob/2ca3c85054b231505c5e6f5f3209eb5f63942dce/Modules/_io/iobase.c#L1010C14-L1010C26

the io.RawIOBase.readall looks like the ideal candidate for PyBytesWriter.

Notably, io.RawIOBase is used internally across many modules:

https://github.com/search?q=repo%3Apython%2Fcpython+io.RawIOBase+path%3A%2F%5ELib%5C%2F%2F&type=code

and some even call readall explicitly sometimes:

return self.readall()

Has this already been discussed elsewhere?

No response given

Links to previous discussion of this feature:

No response

Linked PRs

Activity

  1. changed the title [-]Use PyBytesWriter API in `io.RawIOBase.readall`[/-] [+]Use `PyBytesWriter` API in `io.RawIOBase.readall`[/+] on Oct 14, 2025
  2. cmaloney commented on Oct 14, 2025

    @cmaloney
    Contributor

    cc: @vstinner, @emmatyping . This a very similar optimization to gh-139877.

    This has definitely shown up in user profiles although most the time FileIO.readall (which is an implementation of RawIOBase.readall) ends up getting called

    I like this as an optimization although to me is an intermediate state. I really want to update this to use bytearray + readinto + take_bytes() which does the same thing with less allocations + copies (self.read() allocates and returns a new bytes which is copied into the PyBytesWriter here). I don't think that should block this optimization though. Changing io.RawIOBase to defaulting to .readinto from .read is unfortunately complicated to do without breaking compatibility or making infinite loops.

  3. added
    type-refactorCode refactoring (with no changes in behavior)
    and removed
    type-featureA feature request or enhancement
    on Oct 15, 2025
  4. added a commit that references this issue on Oct 15, 2025
  5. added a commit that references this issue on Dec 6, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions