python / python/cpython

bytearray slice assignment for bytes-like object is much slower than that for bytearray

Open
#101,444 2 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

interpreter-core performance type-bug
Dominant language
Python
Stars
77.2k
Forks
36k
PR merge metrics
PR metrics pending

Description

Bug report

bytearray slice assignment for bytes-like object is much slower than that for bytearray

On my machine:
python -m timeit -s "a=bytearray(4096);b=b'x'*1024;" "a[:1024]=b"
1000000 loops, best of 5: 296 nsec per loop
python -m timeit -s "a=bytearray(4096);b=memoryview(b'x'*1024);" "a[:1024]=b"
1000000 loops, best of 5: 293 nsec per loop
python -m timeit -s "a=bytearray(4096);b=bytearray(b'x'*1024);" "a[:1024]=b"
5000000 loops, best of 5: 80.4 nsec per loop

On official Python playground(https://www.python.org/shell/):
import timeit
timeit.main(["-s", "a=bytearray(4096);b=b'x'*1024;", "a[:1024]=b"])
1000000 loops, best of 5: 272 nsec per loop
timeit.main(["-s", "a=bytearray(4096);b=memoryview(b'x'*1024);", "a[:1024]=b"])
1000000 loops, best of 5: 268 nsec per loop
timeit.main(["-s", "a=bytearray(4096);b=bytearray(b'x'*1024);", "a[:1024]=b"])
5000000 loops, best of 5: 78.8 nsec per loop

Your environment

  • CPython versions tested on: 3.11.1
  • Operating system and architecture: Windows 10 22H2 x86-64
Linked PRs
  • gh-101445

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start by reproducing the three python -m timeit benchmarks in the issue on the stated CPython version and compare the bytearray, bytes, and memoryview cases. Review linked PR gh-101445 for the work already associated with this issue; done should address the reported performance gap without regressing the slice-assignment behavior.

Written by the indexing model from the issue text.

Assessment

Tech stack
python
Domain
backend
Issue type
Bug
Difficulty
4/5
Estimated time
3-5 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
20/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.