]> git.ipfire.org Git - thirdparty/Python/cpython.git/commit
gh-139871: Add `bytearray.take_bytes([n])` to efficiently extract `bytes` (GH-140128)
authorCody Maloney <cmaloney@users.noreply.github.com>
Thu, 13 Nov 2025 13:19:44 +0000 (05:19 -0800)
committerGitHub <noreply@github.com>
Thu, 13 Nov 2025 13:19:44 +0000 (13:19 +0000)
commit732224e1139f7ed4fe0259a2dad900f84910949e
tree757774f078acf13628a65fd9ed433561283f9475
parent2fbd39666663cb5ca1c0e3021ce2e7bc72331020
gh-139871: Add `bytearray.take_bytes([n])` to efficiently extract `bytes` (GH-140128)

Update `bytearray` to contain a `bytes` and provide a zero-copy path to
"extract" the `bytes`. This allows making several code paths more efficient.

This does not move any codepaths to make use of this new API. The documentation
changes include common code patterns which can be made more efficient with
this API.

---

When just changing `bytearray` to contain `bytes` I ran pyperformance on a
`--with-lto --enable-optimizations --with-static-libpython` build and don't see
any major speedups or slowdowns with this; all seems to be in the noise of
my machine (Generally changes under 5% or benchmarks that don't touch
bytes/bytearray).

Co-authored-by: Victor Stinner <vstinner@python.org>
Co-authored-by: Maurycy Pawłowski-Wieroński <5383+maurycy@users.noreply.github.com>
Doc/library/stdtypes.rst
Doc/whatsnew/3.15.rst
Include/cpython/bytearrayobject.h
Include/internal/pycore_bytesobject.h
Lib/test/test_bytes.py
Lib/test/test_capi/test_bytearray.py
Lib/test/test_sys.py
Misc/NEWS.d/next/Core_and_Builtins/2025-10-14-18-24-16.gh-issue-139871.SWtuUz.rst [new file with mode: 0644]
Objects/bytearrayobject.c
Objects/bytesobject.c
Objects/clinic/bytearrayobject.c.h