https://github.com/python/cpython/commit/b4197da019f4a77a649a17e0b9c4ddf7c137229c
commit: b4197da019f4a77a649a17e0b9c4ddf7c137229c
branch: main
author: Taeknology <[email protected]>
committer: eendebakpt <[email protected]>
date: 2026-10-02T23:01:21+02:00
summary:
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
files:
A Misc/NEWS.d/next/Library/2026-08-04-19-50-52.gh-issue-150942.B8ytes.rst
M Modules/_io/bytesio.c
diff --git
a/Misc/NEWS.d/next/Library/2026-08-04-19-50-52.gh-issue-150942.B8ytes.rst
b/Misc/NEWS.d/next/Library/2026-08-04-19-50-52.gh-issue-150942.B8ytes.rst
new file mode 100644
index 000000000000000..0ffe54b9cd2cbd3
--- /dev/null
+++ b/Misc/NEWS.d/next/Library/2026-08-04-19-50-52.gh-issue-150942.B8ytes.rst
@@ -0,0 +1,2 @@
+Improve the performance of :meth:`~io.IOBase.readlines` for
+:class:`io.BytesIO`.
diff --git a/Modules/_io/bytesio.c b/Modules/_io/bytesio.c
index ff9cc0f2166bd74..1f97450f53beec9 100644
--- a/Modules/_io/bytesio.c
+++ b/Modules/_io/bytesio.c
@@ -1,6 +1,7 @@
#include "Python.h"
#include "pycore_bytesobject.h" // _PyBytes_ResizeKeepOnError()
#include "pycore_critical_section.h" // Py_BEGIN_CRITICAL_SECTION()
+#include "pycore_list.h" // _PyList_AppendTakeRef()
#include "pycore_object.h"
#include "pycore_pyatomic_ft_wrappers.h"
#include "pycore_sysmodule.h" // _PySys_GetSizeOf()
@@ -664,11 +665,9 @@ _io_BytesIO_readlines_impl(bytesio *self, PyObject *arg)
line = PyBytes_FromStringAndSize(output, n);
if (!line)
goto on_error;
- if (PyList_Append(result, line) == -1) {
- Py_DECREF(line);
+ if (_PyList_AppendTakeRef((PyListObject *)result, line) < 0) {
goto on_error;
}
- Py_DECREF(line);
size += n;
if (maxsize > 0 && size >= maxsize)
break;
_______________________________________________
Python-checkins mailing list -- [email protected]
To unsubscribe send an email to [email protected]
https://mail.python.org/mailman3//lists/python-checkins.python.org
Member address: [email protected]