Skip to content

gh-87613: Use AC vectorcall for bytes and bytearray - #158891

Open
cmaloney wants to merge 2 commits into
python:mainfrom
cmaloney:ac_vc_bytes_take
Open

cmaloney wants to merge 2 commits into
python:mainfrom
cmaloney:ac_vc_bytes_take

Conversation

@cmaloney

@cmaloney cmaloney commented Oct 6, 2026 •

Copy link
Copy Markdown
Contributor

Speeds up common construction of both objects.

edit: Dropped the no-copy unique object path as it is not sound as noted by PIeter

Benchmark

Generally faster: the fixed per-call overhead drops by about 3–9 ns, which shows most on small inputs. Copy-dominated sizes (1MiB) are unchanged.

Run on an Apple M3 (macOS): PGO+LTO, Apple clang 21, --with-tail-call-interp. Base is 85c03d4 (merge-base with main).
(note: AI Written, but I think sufficiently representative)

"""bytes() and bytearray() construction.

temp/ passes a unique temporary (adopted, no copy); named/ binds the same
source to a name first so it is always copied.
"""
import pyperf

runner = pyperf.Runner()

runner.timeit('call/bytes()', 'bytes()')
runner.timeit('call/bytes(int)', 'bytes(16)')
runner.timeit('call/bytes(bytes)', "bytes(b'abcdefghabcdefgh')")
runner.timeit("call/bytes(str, 'ascii')", "bytes('abcdefgh', 'ascii')")
runner.timeit('call/bytearray()', 'bytearray()')
runner.timeit('call/bytearray(int)', 'bytearray(16)')
runner.timeit('call/bytearray(bytes)', "bytearray(b'abcdefghabcdefgh')")
runner.timeit("call/bytearray(str, 'ascii')",
              "bytearray('abcdefgh', 'ascii')")

for size_name, size in (('16B', 16), ('4KiB', 4096), ('1MiB', 1 << 20)):
    # Slicing in the statement makes a fresh unique temporary.
    setup = f"b = b'x' * {size}; ba = bytearray(b)"
    runner.timeit(f'temp/bytes(bytearray) {size_name}',
                  'bytes(ba[1:])', setup)
    runner.timeit(f'named/bytes(bytearray) {size_name}',
                  'x = ba[1:]; bytes(x); del x', setup)
    runner.timeit(f'temp/bytearray(bytes) {size_name}',
                  'bytearray(b[1:])', setup)
    runner.timeit(f'named/bytearray(bytes) {size_name}',
                  'x = b[1:]; bytearray(x); del x', setup)
    runner.timeit(f'temp/bytearray(bytearray) {size_name}',
                  'bytearray(ba[1:])', setup)
    runner.timeit(f'named/bytearray(bytearray) {size_name}',
                  'x = ba[1:]; bytearray(x); del x', setup)
Benchmark main patch
call/bytes() 11.5 ns 8.26 ns: 1.39x faster
call/bytes(bytes) 32.4 ns 23.3 ns: 1.39x faster
call/bytes(str, 'ascii') 34.9 ns 25.5 ns: 1.37x faster
call/bytes(int) 31.8 ns 23.7 ns: 1.34x faster
call/bytearray(int) 38.1 ns 29.9 ns: 1.27x faster
call/bytearray(bytes) 40.7 ns 32.6 ns: 1.25x faster
call/bytearray(str, 'ascii') 44.0 ns 36.3 ns: 1.21x faster
temp/bytes(bytearray) 16B 62.1 ns 52.9 ns: 1.17x faster
temp/bytearray(bytes) 16B 57.1 ns 49.2 ns: 1.16x faster
named/bytes(bytearray) 16B 63.7 ns 55.0 ns: 1.16x faster
named/bytearray(bytes) 16B 59.2 ns 51.1 ns: 1.16x faster
call/bytearray() 20.9 ns 18.2 ns: 1.15x faster
temp/bytearray(bytearray) 16B 63.5 ns 55.4 ns: 1.15x faster
named/bytearray(bytearray) 16B 65.5 ns 57.3 ns: 1.14x faster
named/bytes(bytearray) 4KiB 186 ns 173 ns: 1.08x faster
temp/bytes(bytearray) 4KiB 185 ns 173 ns: 1.07x faster
named/bytearray(bytearray) 4KiB 191 ns 179 ns: 1.07x faster
temp/bytearray(bytearray) 4KiB 187 ns 178 ns: 1.05x faster
temp/bytearray(bytes) 4KiB 183 ns 176 ns: 1.04x faster
named/bytearray(bytearray) 1MiB 29.5 us 28.4 us: 1.04x faster
named/bytearray(bytes) 4KiB 185 ns 179 ns: 1.04x faster
named/bytes(bytearray) 1MiB 30.7 us 29.7 us: 1.03x faster
Geometric mean (ref) 1.14x faster

Benchmark hidden because not significant (4): named/bytearray(bytes) 1MiB, temp/bytes(bytearray) 1MiB, temp/bytearray(bytes) 1MiB, temp/bytearray(bytearray) 1MiB

The unique-temporary adoption (no copy) was split out to a follow-up after review found it unsafe when a C caller still holds a borrowed reference (see review thread). The temp/ rows now match named/.

Speeds up common construction of both objects. Enables optimizing
construction off uniquely referenced temporary objects.

Previously the argument tuples / dictionaries required by the call
protocol resulted in arguments never being unique temporaries. With
vectorcall they can be. Avoid copying by adopting the existing
allocation when possible.

Benchmark
Run on my linux dev machine: PGO+LTO, clang 23, --with-tail-call-interp

(note: AI Written, but I think sufficiently representative)
```python
"""bytes() and bytearray() construction.

temp/ passes a unique temporary (adopted, no copy); named/ binds the same
source to a name first so it is always copied.
"""
import pyperf

runner = pyperf.Runner()

runner.timeit('call/bytes()', 'bytes()')
runner.timeit('call/bytes(int)', 'bytes(16)')
runner.timeit('call/bytes(bytes)', "bytes(b'abcdefghabcdefgh')")
runner.timeit("call/bytes(str, 'ascii')", "bytes('abcdefgh', 'ascii')")
runner.timeit('call/bytearray()', 'bytearray()')
runner.timeit('call/bytearray(int)', 'bytearray(16)')
runner.timeit('call/bytearray(bytes)', "bytearray(b'abcdefghabcdefgh')")
runner.timeit("call/bytearray(str, 'ascii')",
              "bytearray('abcdefgh', 'ascii')")

for size_name, size in (('16B', 16), ('4KiB', 4096), ('1MiB', 1 << 20)):
    # Slicing in the statement makes a fresh unique temporary.
    setup = f"b = b'x' * {size}; ba = bytearray(b)"
    runner.timeit(f'temp/bytes(bytearray) {size_name}',
                  'bytes(ba[1:])', setup)
    runner.timeit(f'named/bytes(bytearray) {size_name}',
                  'x = ba[1:]; bytes(x); del x', setup)
    runner.timeit(f'temp/bytearray(bytes) {size_name}',
                  'bytearray(b[1:])', setup)
    runner.timeit(f'named/bytearray(bytes) {size_name}',
                  'x = b[1:]; bytearray(x); del x', setup)
    runner.timeit(f'temp/bytearray(bytearray) {size_name}',
                  'bytearray(ba[1:])', setup)
    runner.timeit(f'named/bytearray(bytearray) {size_name}',
                  'x = ba[1:]; bytearray(x); del x', setup)
```

| Benchmark                       | main    | patch                  |
|---------------------------------|:-------:|:----------------------:|
| temp/bytearray(bytearray) 1MiB  | 515 us  | 14.9 us: 34.53x faster |
| temp/bytearray(bytes) 1MiB      | 508 us  | 14.8 us: 34.27x faster |
| temp/bytes(bytearray) 1MiB      | 507 us  | 14.9 us: 34.05x faster |
| temp/bytearray(bytes) 4KiB      | 144 ns  | 79.5 ns: 1.81x faster  |
| temp/bytearray(bytearray) 4KiB  | 155 ns  | 94.7 ns: 1.63x faster  |
| temp/bytearray(bytes) 16B       | 56.4 ns | 36.1 ns: 1.56x faster  |
| temp/bytes(bytearray) 4KiB      | 150 ns  | 96.4 ns: 1.55x faster  |
| temp/bytearray(bytearray) 16B   | 64.1 ns | 47.3 ns: 1.35x faster  |
| call/bytes()                    | 14.7 ns | 11.1 ns: 1.32x faster  |
| call/bytes(int)                 | 31.5 ns | 24.3 ns: 1.29x faster  |
| call/bytes(str, 'ascii')        | 36.9 ns | 28.7 ns: 1.28x faster  |
| call/bytes(bytes)               | 34.3 ns | 27.3 ns: 1.26x faster  |
| temp/bytes(bytearray) 16B       | 59.4 ns | 49.5 ns: 1.20x faster  |
| call/bytearray(str, 'ascii')    | 45.6 ns | 39.1 ns: 1.17x faster  |
| call/bytearray(bytes)           | 44.3 ns | 38.9 ns: 1.14x faster  |
| call/bytearray(int)             | 41.7 ns | 37.3 ns: 1.12x faster  |
| named/bytearray(bytes) 16B      | 59.6 ns | 54.4 ns: 1.10x faster  |
| named/bytes(bytearray) 16B      | 61.3 ns | 56.2 ns: 1.09x faster  |
| named/bytearray(bytearray) 16B  | 65.3 ns | 60.9 ns: 1.07x faster  |
| named/bytes(bytearray) 4KiB     | 154 ns  | 148 ns: 1.04x faster   |
| call/bytearray()                | 22.1 ns | 21.4 ns: 1.03x faster  |
| named/bytearray(bytearray) 4KiB | 156 ns  | 151 ns: 1.03x faster   |
| named/bytearray(bytes) 4KiB     | 146 ns  | 143 ns: 1.02x faster   |
| Geometric mean                  | (ref)   | 1.77x faster           |

Benchmark hidden because not significant (3): named/bytes(bytearray) 1MiB,
named/bytearray(bytes) 1MiB, named/bytearray(bytearray) 1MiB
Comment thread Objects/bytesobject.c Outdated
/* Adopt unique temporary bytearray rather than copying. */
else if (PyByteArray_CheckExact(x)
&& PyUnstable_Object_IsUniqueReferencedTemporary(x)) {
bytes = PyObject_CallMethodNoArgs(x, &_Py_ID(take_bytes));

@cmaloney cmaloney Oct 7, 2026 •

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

note: bytearray is an immutable type so I don't think this can be overridden easily to do arbitrary other things. Can export and direct call into the C if that is preferred.

>>> a = bytearray()
>>> def tb_test(self):
...     return b"abc"
...     
>>> a.take_bytes = tb_test
Traceback (most recent call last):
  File "<python-input-4>", line 1, in <module>
    a.take_bytes = tb_test
    ^^^^^^^^^^^^
AttributeError: 'bytearray' object attribute 'take_bytes' is read-only

@@ -0,0 +1,2 @@
Speed up :class:`bytes` and :class:`bytearray` construction by adding

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The vectorcall changes look good. But the optimizations need a second look. For example:

>>> import bisect
>>> lst = []
>>> bisect.insort(lst, bytearray(b'abc')[1:], key=bytes)
>>> lst
[bytearray(b'')]      # main: [bytearray(b'bc')]

For that reason maybe split the PR?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Splitting the PR

It looks like this gets broken because a lot of the stack uses "borrowed" references which make it look like it's uniquely referenced when it is still in use in multiple paths.... Still want to do the no-copy optimization and the similar one with iconcat but clearly still need some different tools.

PyUnstable_Object_IsUniqueReferencedTemporary() can't tell when a C
caller still holds a borrowed reference to the argument (for example
bisect.insort(lst, bytearray(b'abc')[1:], key=bytes)), so adopting the
buffer empties an object still in use. Leave the optimization for a
follow-up and keep this PR to the vectorcall change.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>

@vstinner vstinner left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

awaiting merge type-feature A feature request or enhancement

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants