-
-
Notifications
You must be signed in to change notification settings - Fork 34k
gh-128213: fast path for bytes creation from list and tuple #132590
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Changes from 24 commits
6f699b5
18c8e4a
406fbdb
4912a05
56f802e
4e1e3e6
bf96d06
f3a9423
8bbc021
8260c7a
bc6f8f2
970c10b
cb664fe
c357217
5d53346
739987e
8b7c5e6
ab82e24
2e5c3c1
a6f74ad
fd28e25
c282610
f0ab624
c90ae60
3db5577
647975c
85497fb
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,37 @@ | ||
| import unittest | ||
| from threading import Thread, Barrier | ||
| from test.support import threading_helper | ||
|
|
||
| threading_helper.requires_working_threading(module=True) | ||
|
|
||
|
|
||
| class BytesThreading(unittest.TestCase): | ||
| @threading_helper.reap_threads | ||
| def test_conversion_from_list(self): | ||
| number_of_threads = 10 | ||
| number_of_iterations = 10 | ||
| barrier = Barrier(number_of_threads) | ||
|
|
||
| x = [1, 2, 3, 4, 5] | ||
| extends = [(ii,) * (2 + ii) for ii in range(number_of_threads)] | ||
|
|
||
| def work(ii): | ||
| barrier.wait() | ||
| for _ in range(1000): | ||
| bytes(x) | ||
| x.extend(extends[ii]) | ||
| if len(x) > 10: | ||
| x[:] = [0] | ||
|
|
||
| for it in range(number_of_iterations): | ||
| worker_threads = [] | ||
| for ii in range(number_of_threads): | ||
| worker_threads.append(Thread(target=work, args=[ii])) | ||
| with threading_helper.start_threads(worker_threads): | ||
| pass | ||
|
|
||
| barrier.reset() | ||
|
|
||
|
|
||
| if __name__ == "__main__": | ||
| unittest.main() | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,3 @@ | ||
| Speed up :class:`bytes` creation from :class:`list` and :class:`tuple` of integers by 27-31%. | ||
|
|
||
| Patch by Ben Hsing and Pieter Eendebak |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -6,6 +6,7 @@ | |
| #include "pycore_bytesobject.h" // _PyBytes_Find(), _PyBytes_Repeat() | ||
| #include "pycore_call.h" // _PyObject_CallNoArgs() | ||
| #include "pycore_ceval.h" // _PyEval_GetBuiltin() | ||
| #include "pycore_critical_section.h" // Py_BEGIN_CRITICAL_SECTION_SEQUENCE_FAST() | ||
| #include "pycore_format.h" // F_LJUST | ||
| #include "pycore_freelist.h" // _Py_FREELIST_FREE() | ||
| #include "pycore_global_objects.h"// _Py_GET_GLOBAL_OBJECT() | ||
|
|
@@ -2885,77 +2886,38 @@ _PyBytes_FromBuffer(PyObject *x) | |
| } | ||
|
|
||
| static PyObject* | ||
| _PyBytes_FromList(PyObject *x) | ||
| _PyBytes_FromSequence_lock_held(PyObject *x) | ||
| { | ||
| Py_ssize_t size = PyList_GET_SIZE(x); | ||
| Py_ssize_t size = PySequence_Fast_GET_SIZE(x); | ||
| PyBytesWriter *writer = PyBytesWriter_Create(size); | ||
| if (writer == NULL) { | ||
| return NULL; | ||
| } | ||
| char *str = PyBytesWriter_GetData(writer); | ||
| size = _PyBytesWriter_GetAllocated(writer); | ||
| assert(_PyBytesWriter_GetAllocated(writer) >= size); | ||
|
|
||
| for (Py_ssize_t i = 0; i < PyList_GET_SIZE(x); i++) { | ||
| PyObject *item = PyList_GET_ITEM(x, i); | ||
| Py_INCREF(item); | ||
| Py_ssize_t value = PyNumber_AsSsize_t(item, NULL); | ||
| Py_DECREF(item); | ||
| if (value == -1 && PyErr_Occurred()) | ||
| goto error; | ||
|
|
||
| if (value < 0 || value >= 256) { | ||
| PyErr_SetString(PyExc_ValueError, | ||
| "bytes must be in range(0, 256)"); | ||
| goto error; | ||
| PyObject *const *items = PySequence_Fast_ITEMS(x); | ||
| for (Py_ssize_t i = 0; i < size; i++) { | ||
| if (!PyLong_Check(items[i])) { | ||
|
||
| PyBytesWriter_Discard(writer); | ||
| /* Py_None as a fallback sentinel to the slow path */ | ||
| Py_RETURN_NONE; | ||
| } | ||
|
|
||
| if (i >= size) { | ||
| str = _PyBytesWriter_ResizeAndUpdatePointer(writer, size + 1, str); | ||
| if (str == NULL) { | ||
| goto error; | ||
| } | ||
| size = _PyBytesWriter_GetAllocated(writer); | ||
| Py_ssize_t value = PyNumber_AsSsize_t(items[i], NULL); | ||
| if (value == -1 && PyErr_Occurred()) { | ||
| PyBytesWriter_Discard(writer); | ||
| return NULL; | ||
| } | ||
| *str++ = (char) value; | ||
| } | ||
| return PyBytesWriter_FinishWithPointer(writer, str); | ||
|
|
||
| error: | ||
| PyBytesWriter_Discard(writer); | ||
| return NULL; | ||
| } | ||
|
|
||
| static PyObject* | ||
| _PyBytes_FromTuple(PyObject *x) | ||
| { | ||
| Py_ssize_t i, size = PyTuple_GET_SIZE(x); | ||
| Py_ssize_t value; | ||
| PyObject *item; | ||
|
|
||
| PyBytesWriter *writer = PyBytesWriter_Create(size); | ||
| if (writer == NULL) { | ||
| return NULL; | ||
| } | ||
| char *str = PyBytesWriter_GetData(writer); | ||
|
|
||
| for (i = 0; i < size; i++) { | ||
| item = PyTuple_GET_ITEM(x, i); | ||
| value = PyNumber_AsSsize_t(item, NULL); | ||
| if (value == -1 && PyErr_Occurred()) | ||
| goto error; | ||
|
|
||
| if (value < 0 || value >= 256) { | ||
| PyErr_SetString(PyExc_ValueError, | ||
| "bytes must be in range(0, 256)"); | ||
| goto error; | ||
| PyBytesWriter_Discard(writer); | ||
| return NULL; | ||
| } | ||
| *str++ = (char) value; | ||
| } | ||
| return PyBytesWriter_Finish(writer); | ||
|
|
||
| error: | ||
| PyBytesWriter_Discard(writer); | ||
| return NULL; | ||
| return PyBytesWriter_FinishWithPointer(writer, str); | ||
| } | ||
|
|
||
| static PyObject * | ||
|
|
@@ -3036,11 +2998,15 @@ PyBytes_FromObject(PyObject *x) | |
| if (PyObject_CheckBuffer(x)) | ||
| return _PyBytes_FromBuffer(x); | ||
|
|
||
| if (PyList_CheckExact(x)) | ||
| return _PyBytes_FromList(x); | ||
|
|
||
| if (PyTuple_CheckExact(x)) | ||
| return _PyBytes_FromTuple(x); | ||
| if (PyList_CheckExact(x) || PyTuple_CheckExact(x)) { | ||
| Py_BEGIN_CRITICAL_SECTION_SEQUENCE_FAST(x); | ||
| result = _PyBytes_FromSequence_lock_held(x); | ||
| Py_END_CRITICAL_SECTION_SEQUENCE_FAST(); | ||
| /* Py_None as a fallback sentinel to the slow path */ | ||
| if (result != Py_None) { | ||
| return result; | ||
| } | ||
| } | ||
|
|
||
| if (!PyUnicode_Check(x)) { | ||
| it = PyObject_GetIter(x); | ||
|
|
||
Uh oh!
There was an error while loading. Please reload this page.