-
-
Notifications
You must be signed in to change notification settings - Fork 32k
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
Open
eendebakpt
wants to merge
16
commits into
python:main
Choose a base branch
from
eendebakpt:fast-bytes-creation-from-list-tuple-2
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+68
−70
Open
Changes from 10 commits
Commits
Show all changes
16 commits
Select commit
Hold shift + click to select a range
6f699b5
gh-128213: fast path for bytes creation from list and tuple
blhsing 18c8e4a
coerce long to char after validation of integer in byte range
blhsing 406fbdb
📜🤖 Added by blurb_it.
blurb-it[bot] 4912a05
Update 2024-12-24-08-44-49.gh-issue-128213.Y71jDi.rst
blhsing 56f802e
updated for thread-safety, style choices, function choices and benchm…
blhsing 4e1e3e6
revert to PyLong_AsLongAndOverflow for easier overflow handling
blhsing bf96d06
fixed issue of a label at the end of a compound statment; revert to u…
blhsing f3a9423
Add FT test for bytes from list
eendebakpt 8bbc021
Merge branch 'main' into fast-bytes-creation-from-list-tuple-2
eendebakpt 8260c7a
Merge branch 'test_bytes_from_list' into fast-bytes-creation-from-lis…
eendebakpt bc6f8f2
refactor
eendebakpt 970c10b
refactor
eendebakpt cb664fe
refactor
eendebakpt c357217
Update Misc/NEWS.d/next/Core_and_Builtins/2024-12-24-08-44-49.gh-issu…
eendebakpt 5d53346
Merge branch 'main' into fast-bytes-creation-from-list-tuple-2
eendebakpt 739987e
Merge branch 'main' into fast-bytes-creation-from-list-tuple-2
eendebakpt File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
import unittest | ||
import sys | ||
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] | ||
e = [ (ii,)*(2+4*ii) for ii in range(number_of_threads)] # range of sizes to extend | ||
def work(ii): | ||
barrier.wait() | ||
for _ in range(1000): | ||
bytes(x) | ||
x.extend(e[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() |
3 changes: 3 additions & 0 deletions
3
Misc/NEWS.d/next/Core_and_Builtins/2024-12-24-08-44-49.gh-issue-128213.Y71jDi.rst
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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. Benchmarks show that from a list with 1000000 random numbers the time to create a bytes object is reduced by around 31%, or 30% with 10000 numbers, or 27% with 100 numbers. | ||
eendebakpt marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
Patch by Ben Hsing |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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_global_objects.h"// _Py_GET_GLOBAL_OBJECT() | ||||||||||||
#include "pycore_initconfig.h" // _PyStatus_OK() | ||||||||||||
|
@@ -2854,82 +2855,46 @@ _PyBytes_FromBuffer(PyObject *x) | |||||||||||
} | ||||||||||||
|
||||||||||||
static PyObject* | ||||||||||||
_PyBytes_FromList(PyObject *x) | ||||||||||||
_PyBytes_FromSequence(PyObject *x) | ||||||||||||
{ | ||||||||||||
Py_ssize_t i, size = PyList_GET_SIZE(x); | ||||||||||||
Py_ssize_t value; | ||||||||||||
char *str; | ||||||||||||
PyObject *item; | ||||||||||||
_PyBytesWriter writer; | ||||||||||||
|
||||||||||||
_PyBytesWriter_Init(&writer); | ||||||||||||
str = _PyBytesWriter_Alloc(&writer, size); | ||||||||||||
if (str == NULL) | ||||||||||||
Py_ssize_t size = PySequence_Fast_GET_SIZE(x); | ||||||||||||
PyObject *bytes = _PyBytes_FromSize(size, 0); | ||||||||||||
if (bytes == NULL) { | ||||||||||||
return NULL; | ||||||||||||
writer.overallocate = 1; | ||||||||||||
size = writer.allocated; | ||||||||||||
|
||||||||||||
for (i = 0; i < PyList_GET_SIZE(x); i++) { | ||||||||||||
item = PyList_GET_ITEM(x, i); | ||||||||||||
Py_INCREF(item); | ||||||||||||
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; | ||||||||||||
} | ||||||||||||
|
||||||||||||
if (i >= size) { | ||||||||||||
str = _PyBytesWriter_Resize(&writer, str, size+1); | ||||||||||||
if (str == NULL) | ||||||||||||
return NULL; | ||||||||||||
size = writer.allocated; | ||||||||||||
} | ||||||||||||
*str++ = (char) value; | ||||||||||||
} | ||||||||||||
return _PyBytesWriter_Finish(&writer, str); | ||||||||||||
|
||||||||||||
error: | ||||||||||||
_PyBytesWriter_Dealloc(&writer); | ||||||||||||
return NULL; | ||||||||||||
} | ||||||||||||
|
||||||||||||
static PyObject* | ||||||||||||
_PyBytes_FromTuple(PyObject *x) | ||||||||||||
{ | ||||||||||||
PyObject *bytes; | ||||||||||||
Py_ssize_t i, size = PyTuple_GET_SIZE(x); | ||||||||||||
Py_ssize_t value; | ||||||||||||
char *str; | ||||||||||||
PyObject *item; | ||||||||||||
|
||||||||||||
bytes = PyBytes_FromStringAndSize(NULL, size); | ||||||||||||
if (bytes == NULL) | ||||||||||||
return NULL; | ||||||||||||
str = ((PyBytesObject *)bytes)->ob_sval; | ||||||||||||
|
||||||||||||
for (i = 0; i < size; i++) { | ||||||||||||
item = PyTuple_GET_ITEM(x, i); | ||||||||||||
value = PyNumber_AsSsize_t(item, NULL); | ||||||||||||
if (value == -1 && PyErr_Occurred()) | ||||||||||||
char *str = PyBytes_AS_STRING(bytes); | ||||||||||||
PyObject *const *items = PySequence_Fast_ITEMS(x); | ||||||||||||
Py_BEGIN_CRITICAL_SECTION_SEQUENCE_FAST(x); | ||||||||||||
for (Py_ssize_t i = 0; i < size; i++) { | ||||||||||||
if (!PyLong_Check(items[i])) { | ||||||||||||
Py_DECREF(bytes); | ||||||||||||
/* Py_None as a fallback sentinel to the slow path */ | ||||||||||||
bytes = Py_None; | ||||||||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
Is needed, no? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Py_None is immortal, so not needed |
||||||||||||
goto done; | ||||||||||||
} | ||||||||||||
Py_ssize_t value = PyNumber_AsSsize_t(items[i], 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; | ||||||||||||
} | ||||||||||||
*str++ = (char) value; | ||||||||||||
} | ||||||||||||
return bytes; | ||||||||||||
|
||||||||||||
goto done; | ||||||||||||
error: | ||||||||||||
Py_DECREF(bytes); | ||||||||||||
return NULL; | ||||||||||||
bytes = NULL; | ||||||||||||
done: | ||||||||||||
/* some C parsers require a label not to be at the end of a compound | ||||||||||||
statement, which the ending macro of a critical section introduces, so | ||||||||||||
we need an empty statement here to satisfy that syntax rule */ | ||||||||||||
; | ||||||||||||
/* both success and failure need to end the critical section */ | ||||||||||||
Py_END_CRITICAL_SECTION_SEQUENCE_FAST(); | ||||||||||||
return bytes; | ||||||||||||
} | ||||||||||||
|
||||||||||||
static PyObject * | ||||||||||||
|
@@ -3012,11 +2977,13 @@ 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)) { | ||||||||||||
PyObject *bytes = _PyBytes_FromSequence(x); | ||||||||||||
/* Py_None as a fallback sentinel to the slow path */ | ||||||||||||
if (bytes != Py_None) { | ||||||||||||
return bytes; | ||||||||||||
} | ||||||||||||
} | ||||||||||||
|
||||||||||||
if (!PyUnicode_Check(x)) { | ||||||||||||
it = PyObject_GetIter(x); | ||||||||||||
|
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.