Skip to content

Commit b4197da

Browse files
authored
gh-150942: Speed up BytesIO.readlines() using reference-stealing append (#155171)
* gh-150942: Use reference-stealing append in BytesIO.readlines * gh-150942: Add NEWS entry for BytesIO.readlines optimization * gh-150942: Fix NEWS cross-reference * gh-150942: Simplify BytesIO.readlines NEWS entry
1 parent 8ba1f31 commit b4197da

2 files changed

Lines changed: 4 additions & 3 deletions

File tree

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
Improve the performance of :meth:`~io.IOBase.readlines` for
2+
:class:`io.BytesIO`.

‎Modules/_io/bytesio.c‎

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
#include "Python.h"
22
#include "pycore_bytesobject.h" // _PyBytes_ResizeKeepOnError()
33
#include "pycore_critical_section.h" // Py_BEGIN_CRITICAL_SECTION()
4+
#include "pycore_list.h" // _PyList_AppendTakeRef()
45
#include "pycore_object.h"
56
#include "pycore_pyatomic_ft_wrappers.h"
67
#include "pycore_sysmodule.h" // _PySys_GetSizeOf()
@@ -664,11 +665,9 @@ _io_BytesIO_readlines_impl(bytesio *self, PyObject *arg)
664665
line = PyBytes_FromStringAndSize(output, n);
665666
if (!line)
666667
goto on_error;
667-
if (PyList_Append(result, line) == -1) {
668-
Py_DECREF(line);
668+
if (_PyList_AppendTakeRef((PyListObject *)result, line) < 0) {
669669
goto on_error;
670670
}
671-
Py_DECREF(line);
672671
size += n;
673672
if (maxsize > 0 && size >= maxsize)
674673
break;

0 commit comments

Comments
 (0)