Skip to content

Commit 71defb6

Browse files
authored
gh-129813, PEP 782: Use PyBytesWriter in _io.FileIO.readall (#138901)
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 ```
1 parent c5fb72e commit 71defb6

File tree

1 file changed

+11
-15
lines changed

1 file changed

+11
-15
lines changed

Modules/_io/fileio.c

Lines changed: 11 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -739,7 +739,7 @@ _io_FileIO_readall_impl(fileio *self)
739739
/*[clinic end generated code: output=faa0292b213b4022 input=10d8b2ec403302dc]*/
740740
{
741741
Py_off_t pos, end;
742-
PyObject *result;
742+
PyBytesWriter *writer;
743743
Py_ssize_t bytes_read = 0;
744744
Py_ssize_t n;
745745
size_t bufsize;
@@ -794,10 +794,10 @@ _io_FileIO_readall_impl(fileio *self)
794794
}
795795
}
796796

797-
798-
result = PyBytes_FromStringAndSize(NULL, bufsize);
799-
if (result == NULL)
797+
writer = PyBytesWriter_Create(bufsize);
798+
if (writer == NULL) {
800799
return NULL;
800+
}
801801

802802
while (1) {
803803
if (bytes_read >= (Py_ssize_t)bufsize) {
@@ -806,18 +806,18 @@ _io_FileIO_readall_impl(fileio *self)
806806
PyErr_SetString(PyExc_OverflowError,
807807
"unbounded read returned more bytes "
808808
"than a Python bytes object can hold");
809-
Py_DECREF(result);
809+
PyBytesWriter_Discard(writer);
810810
return NULL;
811811
}
812812

813-
if (PyBytes_GET_SIZE(result) < (Py_ssize_t)bufsize) {
814-
if (_PyBytes_Resize(&result, bufsize) < 0)
813+
if (PyBytesWriter_GetSize(writer) < (Py_ssize_t)bufsize) {
814+
if (PyBytesWriter_Resize(writer, bufsize) < 0)
815815
return NULL;
816816
}
817817
}
818818

819819
n = _Py_read(self->fd,
820-
PyBytes_AS_STRING(result) + bytes_read,
820+
(char*)PyBytesWriter_GetData(writer) + bytes_read,
821821
bufsize - bytes_read);
822822

823823
if (n == 0)
@@ -827,20 +827,16 @@ _io_FileIO_readall_impl(fileio *self)
827827
PyErr_Clear();
828828
if (bytes_read > 0)
829829
break;
830-
Py_DECREF(result);
830+
PyBytesWriter_Discard(writer);
831831
Py_RETURN_NONE;
832832
}
833-
Py_DECREF(result);
833+
PyBytesWriter_Discard(writer);
834834
return NULL;
835835
}
836836
bytes_read += n;
837837
}
838838

839-
if (PyBytes_GET_SIZE(result) > bytes_read) {
840-
if (_PyBytes_Resize(&result, bytes_read) < 0)
841-
return NULL;
842-
}
843-
return result;
839+
return PyBytesWriter_FinishWithSize(writer, bytes_read);
844840
}
845841

846842
/*[clinic input]

0 commit comments

Comments
 (0)