classification
Title: bytes(x) is slow when x is bytearray
Type: performance Stage: resolved
Components: Interpreter Core Versions: Python 3.6
process
Status: closed Resolution: fixed
Dependencies: Superseder:
Assigned To: serhiy.storchaka Nosy List: inada.naoki, matrixise, python-dev, serhiy.storchaka, vstinner
Priority: normal Keywords: patch

Created on 2016-08-07 19:22 by inada.naoki, last changed 2016-08-15 08:55 by serhiy.storchaka. This issue is now closed.

Files
File name Uploaded Description Edit
fast-bytearray-fromobject.patch inada.naoki, 2016-08-07 19:22 review
fast-bytearray-fromobject.patch inada.naoki, 2016-08-09 18:46 review
Messages (8)
msg272130 - (view) Author: Inada Naoki (inada.naoki) * (Python committer) Date: 2016-08-07 19:22
When bytes(x), bytes_new checks if x is integer via PyNumber_AsSize_t(x).
It cause TypeError internally.

When x is not an integer, especially bytearray or memoryview, the internal
exception cause significant overhead.

# HEAD
$ ./python -m timeit -s 'data=bytearray(b"xyz")' 'bytes(data)'
1000000 loops, best of 3: 0.696 usec per loop
$ ./python -m timeit -s 'data=bytearray(b"xyz")' 'bytes(data)'
1000000 loops, best of 3: 0.699 usec per loop
$ ./python -m timeit -s 'data=bytearray(b"xyz")' 'bytes(data)'
1000000 loops, best of 3: 0.701 usec per loop

# this patch
$ ./python -m timeit -s 'data=bytearray(b"xyz")' 'bytes(data)'
1000000 loops, best of 3: 0.265 usec per loop
$ ./python -m timeit -s 'data=bytearray(b"xyz")' 'bytes(data)'
1000000 loops, best of 3: 0.265 usec per loop
$ ./python -m timeit -s 'data=bytearray(b"xyz")' 'bytes(data)'
1000000 loops, best of 3: 0.267 usec per loop
msg272131 - (view) Author: Stéphane Wirtel (matrixise) * (Python committer) Date: 2016-08-07 20:02
the patch seems to be fine.
msg272249 - (view) Author: Serhiy Storchaka (serhiy.storchaka) * (Python committer) Date: 2016-08-09 13:47
bytearray suffers from the same issue. It would be nice to optimize it too.
msg272258 - (view) Author: Stéphane Wirtel (matrixise) * (Python committer) Date: 2016-08-09 16:11
Hi Serhiy

Thank you for your feedback.

Stephane
msg272260 - (view) Author: Inada Naoki (inada.naoki) * (Python committer) Date: 2016-08-09 18:46
Thanks for comments.
msg272261 - (view) Author: Serhiy Storchaka (serhiy.storchaka) * (Python committer) Date: 2016-08-09 19:18
LGTM.
msg272721 - (view) Author: Roundup Robot (python-dev) (Python triager) Date: 2016-08-15 06:46
New changeset 789a42401009 by Serhiy Storchaka in branch 'default':
Issue #27704: Optimized creating bytes and bytearray from byte-like objects
https://hg.python.org/cpython/rev/789a42401009
msg272739 - (view) Author: Serhiy Storchaka (serhiy.storchaka) * (Python committer) Date: 2016-08-15 08:55
Thank you for your contribution Naoki.
History
Date User Action Args
2016-08-15 08:55:51serhiy.storchakasetmessages: + msg272739
2016-08-15 08:55:08serhiy.storchakasetstatus: open -> closed
resolution: fixed
stage: commit review -> resolved
2016-08-15 06:46:35python-devsetnosy: + python-dev
messages: + msg272721
2016-08-09 19:18:45serhiy.storchakasetassignee: serhiy.storchaka
messages: + msg272261
stage: commit review
2016-08-09 18:46:08inada.naokisetfiles: + fast-bytearray-fromobject.patch

messages: + msg272260
2016-08-09 16:11:13matrixisesetmessages: + msg272258
2016-08-09 13:47:45serhiy.storchakasetnosy: + serhiy.storchaka
messages: + msg272249
2016-08-07 20:02:49matrixisesetnosy: + vstinner, matrixise
messages: + msg272131
2016-08-07 19:22:27inada.naokicreate