mirror of
https://github.com/python/cpython.git
synced 2025-12-23 09:19:18 +00:00
gh-140135: Use PyBytesWriter in io.RawIOBase.readall(); 4x faster (#140139)
This commit is contained in:
parent
0c66da8de4
commit
d301587369
2 changed files with 16 additions and 16 deletions
|
|
@ -0,0 +1,2 @@
|
|||
Speed up :meth:`io.RawIOBase.readall` by using PyBytesWriter API (about 4x
|
||||
faster)
|
||||
|
|
@ -962,12 +962,10 @@ static PyObject *
|
|||
_io__RawIOBase_readall_impl(PyObject *self)
|
||||
/*[clinic end generated code: output=1987b9ce929425a0 input=688874141213622a]*/
|
||||
{
|
||||
int r;
|
||||
PyObject *chunks = PyList_New(0);
|
||||
PyObject *result;
|
||||
|
||||
if (chunks == NULL)
|
||||
PyBytesWriter *writer = PyBytesWriter_Create(0);
|
||||
if (writer == NULL) {
|
||||
return NULL;
|
||||
}
|
||||
|
||||
while (1) {
|
||||
PyObject *data = _PyObject_CallMethod(self, &_Py_ID(read),
|
||||
|
|
@ -978,21 +976,21 @@ _io__RawIOBase_readall_impl(PyObject *self)
|
|||
if (_PyIO_trap_eintr()) {
|
||||
continue;
|
||||
}
|
||||
Py_DECREF(chunks);
|
||||
PyBytesWriter_Discard(writer);
|
||||
return NULL;
|
||||
}
|
||||
if (data == Py_None) {
|
||||
if (PyList_GET_SIZE(chunks) == 0) {
|
||||
Py_DECREF(chunks);
|
||||
if (PyBytesWriter_GetSize(writer) == 0) {
|
||||
PyBytesWriter_Discard(writer);
|
||||
return data;
|
||||
}
|
||||
Py_DECREF(data);
|
||||
break;
|
||||
}
|
||||
if (!PyBytes_Check(data)) {
|
||||
Py_DECREF(chunks);
|
||||
Py_DECREF(data);
|
||||
PyErr_SetString(PyExc_TypeError, "read() should return bytes");
|
||||
PyBytesWriter_Discard(writer);
|
||||
return NULL;
|
||||
}
|
||||
if (PyBytes_GET_SIZE(data) == 0) {
|
||||
|
|
@ -1000,16 +998,16 @@ _io__RawIOBase_readall_impl(PyObject *self)
|
|||
Py_DECREF(data);
|
||||
break;
|
||||
}
|
||||
r = PyList_Append(chunks, data);
|
||||
Py_DECREF(data);
|
||||
if (r < 0) {
|
||||
Py_DECREF(chunks);
|
||||
if (PyBytesWriter_WriteBytes(writer,
|
||||
PyBytes_AS_STRING(data),
|
||||
PyBytes_GET_SIZE(data)) < 0) {
|
||||
Py_DECREF(data);
|
||||
PyBytesWriter_Discard(writer);
|
||||
return NULL;
|
||||
}
|
||||
Py_DECREF(data);
|
||||
}
|
||||
result = PyBytes_Join((PyObject *)&_Py_SINGLETON(bytes_empty), chunks);
|
||||
Py_DECREF(chunks);
|
||||
return result;
|
||||
return PyBytesWriter_Finish(writer);
|
||||
}
|
||||
|
||||
static PyObject *
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue