Thanks to visit codestin.com
Credit goes to github.com

Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Next Next commit
gh-129813, PEP 782: Use PyBytesWriter in _io.FileIO.readall
Performance about the same, using the benchmark from gh-120754:

```
pyperf compare_to main.json pep782.json
read_file_small: Mean +- std dev: [main] 5.71 us +- 0.05 us -> [pep782] 5.68 us +- 0.05 us: 1.01x faster
read_file_large: Mean +- std dev: [main] 89.7 us +- 0.9 us -> [pep782] 86.9 us +- 0.8 us: 1.03x faster
read_all_rst_bytes: Mean +- std dev: [main] 926 us +- 8 us -> [pep782] 920 us +- 12 us: 1.01x faster
read_all_rst_text: Mean +- std dev: [main] 2.24 ms +- 0.02 ms -> [pep782] 2.17 ms +- 0.04 ms: 1.03x faster

Benchmark hidden because not significant (1): read_all_py_bytes

Geometric mean: 1.01x faster
```
  • Loading branch information
cmaloney committed Sep 15, 2025
commit 86b9f6b055e1fa5fc7532e878327e5d0ae5eac3f
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
Update :class:`io.FileIO` to use :pep:`782` :c:type:`PyBytesWriter` to
Comment thread
cmaloney marked this conversation as resolved.
Outdated
implement :meth:`~io.RawIOBase.readall`
26 changes: 11 additions & 15 deletions Modules/_io/fileio.c
Original file line number Diff line number Diff line change
Expand Up @@ -739,7 +739,7 @@
/*[clinic end generated code: output=faa0292b213b4022 input=10d8b2ec403302dc]*/
{
Py_off_t pos, end;
PyObject *result;
PyBytesWriter *writer;
Py_ssize_t bytes_read = 0;
Py_ssize_t n;
size_t bufsize;
Expand Down Expand Up @@ -794,10 +794,10 @@
}
}


result = PyBytes_FromStringAndSize(NULL, bufsize);
if (result == NULL)
writer = PyBytesWriter_Create(bufsize);
if (writer == NULL) {
return NULL;
}

while (1) {
if (bytes_read >= (Py_ssize_t)bufsize) {
Expand All @@ -806,18 +806,18 @@
PyErr_SetString(PyExc_OverflowError,
"unbounded read returned more bytes "
"than a Python bytes object can hold");
Py_DECREF(result);
PyBytesWriter_Discard(writer);
return NULL;
}

if (PyBytes_GET_SIZE(result) < (Py_ssize_t)bufsize) {
if (_PyBytes_Resize(&result, bufsize) < 0)
if (PyBytesWriter_GetSize(writer) < (Py_ssize_t)bufsize) {
if (PyBytesWriter_Resize(writer, bufsize) < 0)
return NULL;
}
}

n = _Py_read(self->fd,
PyBytes_AS_STRING(result) + bytes_read,
PyBytesWriter_GetData(writer) + bytes_read,

Check failure on line 820 in Modules/_io/fileio.c

View workflow job for this annotation

GitHub Actions / Windows / Build and test (x64)

'void *': unknown size [D:\a\cpython\cpython\PCbuild\_freeze_module.vcxproj]

Check failure on line 820 in Modules/_io/fileio.c

View workflow job for this annotation

GitHub Actions / Windows (free-threading) / Build and test (x64)

'void *': unknown size [D:\a\cpython\cpython\PCbuild\_freeze_module.vcxproj]

Check failure on line 820 in Modules/_io/fileio.c

View workflow job for this annotation

GitHub Actions / Windows / Build and test (arm64)

'void *': unknown size [C:\a\cpython\cpython\PCbuild\_freeze_module.vcxproj]

Check failure on line 820 in Modules/_io/fileio.c

View workflow job for this annotation

GitHub Actions / Windows (free-threading) / Build and test (arm64)

'void *': unknown size [C:\a\cpython\cpython\PCbuild\_freeze_module.vcxproj]
bufsize - bytes_read);

if (n == 0)
Expand All @@ -827,20 +827,16 @@
PyErr_Clear();
if (bytes_read > 0)
break;
Py_DECREF(result);
PyBytesWriter_Discard(writer);
Py_RETURN_NONE;
}
Py_DECREF(result);
PyBytesWriter_Discard(writer);
return NULL;
}
bytes_read += n;
}

if (PyBytes_GET_SIZE(result) > bytes_read) {
if (_PyBytes_Resize(&result, bytes_read) < 0)
return NULL;
}
return result;
return PyBytesWriter_FinishWithSize(writer, bytes_read);
}

/*[clinic input]
Expand Down
Loading