8000 gh-128213: fast path for bytes creation from list and tuple by eendebakpt · Pull Request #132590 · python/cpython · GitHub
[go: up one dir, main page]

Skip to content

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
wants to merge 19 commits into
base: main
Choose a base branch
from
Open
Changes from 1 commit
Commits
Show all changes
19 commits
Select commit Hold shift + click to select a range
6f699b5
gh-128213: fast path for bytes creation from list and tuple
blhsing Dec 24, 2024
18c8e4a
coerce long to char after validation of integer in byte range
blhsing Dec 24, 2024
406fbdb
📜🤖 Added by blurb_it.
blurb-it[bot] Dec 24, 2024
4912a05
Update 2024-12-24-08-44-49.gh-issue-128213.Y71jDi.rst
blhsing Dec 24, 2024
56f802e
updated for thread-safety, style choices, function choices and benchm…
blhsing Dec 25, 2024
4e1e3e6
revert to PyLong_AsLongAndOverflow for easier overflow handling
blhsing Dec 25, 2024
bf96d06
fixed issue of a label at the end of a compound statment; revert to u…
blhsing Dec 26, 2024
f3a9423
Add FT test for bytes from list
eendebakpt Mar 26, 2025
8bbc021
Merge branch 'main' into fast-bytes-creation-from-list-tuple-2
eendebakpt Apr 16, 2025
8260c7a
Merge branch 'test_bytes_from_list' into fast-bytes-creation-from-lis…
eendebakpt Apr 16, 2025
bc6f8f2
refactor
eendebakpt Apr 16, 2025
970c10b
refactor
eendebakpt Apr 16, 2025
cb664fe
refactor
eendebakpt Apr 16, 2025
c357217
Update Misc/NEWS.d/next/Core_and_Builtins/2024-12-24-08-44-49.gh-issu…
eendebakpt Apr 16, 2025
5d53346
Merge branch 'main' into fast-bytes-creation-from-list-tuple-2
eendebakpt Apr 16, 2025
739987e
Merge branch 'main' into fast-bytes-creation-from-list-tuple-2
eendebakpt May 23, 2025
8b7c5e6
Merge branch 'main' into fast-bytes-creation-from-list-tuple-2
eendebakpt Jun 18, 2025
ab82e24
lint
eendebakpt Jul 1, 2025
2e5c3c1
Merge branch 'main' into fast-bytes-creation-from-list-tuple-2
eendebakpt Jul 1, 2025
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Next Next commit
gh-128213: fast path for bytes creation from list and tuple
  • Loading branch information
blhsing committed Dec 24, 2024
commit 6f699b565626556aaf255884cbe20871fc27e760
89 changes: 22 additions & 67 deletions Objects/bytesobject.c
Original file line number Diff line number Diff line change
Expand Up @@ -2810,79 +2810,33 @@
}

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)
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);
Py_ssize_t size = PySequence_Fast_GET_SIZE(x);
PyObject *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 *s = PyBytes_AS_STRING(bytes);
PyObject **items = PySequence_Fast_ITEMS(x);
for (Py_ssize_t i = 0; i < size; i++) {
if (!PyLong_CheckExact(items[i])) {
Py_DECREF(bytes);
return Py_None; // None as fallback sentinel to the slow path
}
int overflow;
long value = PyLong_AsLongAndOverflow(items[i], &overflow);
if (value == -1 && PyErr_Occurred()) {
goto error;

}
if (value < 0 || value >= 256) {
/* this includes an overflow in converting to C long */
PyErr_SetString(PyExc_ValueError,
"bytes must be in range(0, 256)");
goto error;
}
*str++ = (char) value;
s[i] = value;

Check warning on line 2837 in Objects/bytesobject.c

View workflow job for this annotation

GitHub Actions / Windows / build and test (arm64)

'=': conversion from 'long' to 'char', possible loss of data [D:\a\cpython\cpython\PCbuild\_freeze_module.vcxproj]

Check warning on line 2837 in Objects/bytesobject.c

View workflow job for this annotation

GitHub Actions / Windows / build and test (arm64)

'=': conversion from 'long' to 'char', possible loss of data [D:\a\cpython\cpython\PCbuild\pythoncore.vcxproj]

Check warning on line 2837 in Objects/bytesobject.c

View workflow job for this annotation

GitHub Actions / Windows (free-threading) / build and test (arm64)

'=': conversion from 'long' to 'char', possible loss of data [D:\a\cpython\cpython\PCbuild\_freeze_module.vcxproj]

Check warning on line 2837 in Objects/bytesobject.c

View workflow job for this annotation

GitHub Actions / Windows (free-threading) / build and test (arm64)

'=': conversion from 'long' to 'char', possible loss of data [D:\a\cpython\cpython\PCbuild\pythoncore.vcxproj]

Check warning on line 2837 in Objects/bytesobject.c

View workflow job for this annotation

GitHub Actions / Windows / build and test (x64)

'=': conversion from 'long' to 'char', possible loss of data [D:\a\cpython\cpython\PCbuild\_freeze_module.vcxproj]

Check warning on line 2837 in Objects/bytesobject.c

View workflow job for this annotation

GitHub Actions / Windows / build and test (x64)

'=': conversion from 'long' to 'char', possible loss of data [D:\a\cpython\cpython\PCbuild\pythoncore.vcxproj]

Check warning on line 2837 in Objects/bytesobject.c

View workflow job for this annota 9C87 tion

GitHub Actions / Windows (free-threading) / build and test (x64)

'=': conversion from 'long' to 'char', possible loss of data [D:\a\cpython\cpython\PCbuild\_freeze_module.vcxproj]

Check warning on line 2837 in Objects/bytesobject.c

View workflow job for this annotation

GitHub Actions / Windows (free-threading) / build and test (x64)

'=': conversion from 'long' to 'char', possible loss of data [D:\a\cpython\cpython\PCbuild\pythoncore.vcxproj]
}
return bytes;

error:
Py_DECREF(bytes);
return NULL;
Expand Down Expand Up @@ -2968,11 +2922,12 @@
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);
if (bytes != Py_None) {
return bytes;
}
}

if (!PyUnicode_Check(x)) {
it = PyObject_GetIter(x);
Expand Down
Loading
0