From 0fa5b975832510aa9838a3f8c87aa691dd437576 Mon Sep 17 00:00:00 2001 From: Kumar Aditya <59607654+kumaraditya303@users.noreply.github.com> Date: Mon, 14 Mar 2022 10:29:25 +0000 Subject: [PATCH 1/6] speed up iteration of bytes and bytearray --- Include/internal/pycore_long.h | 5 +++++ Objects/bytearrayobject.c | 8 +++----- Objects/bytesobject.c | 2 +- 3 files changed, 9 insertions(+), 6 deletions(-) diff --git a/Include/internal/pycore_long.h b/Include/internal/pycore_long.h index 436bf084685997..8decfe0621ee51 100644 --- a/Include/internal/pycore_long.h +++ b/Include/internal/pycore_long.h @@ -37,6 +37,11 @@ static inline PyObject* _PyLong_GetZero(void) static inline PyObject* _PyLong_GetOne(void) { return (PyObject *)&_PyLong_SMALL_INTS[_PY_NSMALLNEGINTS+1]; } +static inline PyObject* _PyLong_FromUnsignedChar(unsigned char i) +{ + return Py_NewRef((PyObject *)&_PyLong_SMALL_INTS[_PY_NSMALLNEGINTS+i]); +} + PyObject *_PyLong_Add(PyLongObject *left, PyLongObject *right); PyObject *_PyLong_Multiply(PyLongObject *left, PyLongObject *right); PyObject *_PyLong_Subtract(PyLongObject *left, PyLongObject *right); diff --git a/Objects/bytearrayobject.c b/Objects/bytearrayobject.c index 0ebb2ece39d5d3..d4b5e33b950d89 100644 --- a/Objects/bytearrayobject.c +++ b/Objects/bytearrayobject.c @@ -6,6 +6,7 @@ #include "pycore_bytes_methods.h" #include "pycore_object.h" // _PyObject_GC_UNTRACK() #include "pycore_strhex.h" // _Py_strhex_with_sep() +#include "pycore_long.h" // _PyLong_FromUnsignedChar() #include "bytesobject.h" /*[clinic input] @@ -2396,11 +2397,8 @@ bytearrayiter_next(bytesiterobject *it) assert(PyByteArray_Check(seq)); if (it->it_index < PyByteArray_GET_SIZE(seq)) { - item = PyLong_FromLong( - (unsigned char)PyByteArray_AS_STRING(seq)[it->it_index]); - if (item != NULL) - ++it->it_index; - return item; + return _PyLong_FromUnsignedChar( + (unsigned char)PyByteArray_AS_STRING(seq)[it->it_index++]); } it->it_seq = NULL; diff --git a/Objects/bytesobject.c b/Objects/bytesobject.c index fd1c58c2f233eb..003953244a885a 100644 --- a/Objects/bytesobject.c +++ b/Objects/bytesobject.c @@ -3133,7 +3133,7 @@ striter_next(striterobject *it) assert(PyBytes_Check(seq)); if (it->it_index < PyBytes_GET_SIZE(seq)) { - return PyLong_FromLong( + return _PyLong_FromUnsignedChar( (unsigned char)seq->ob_sval[it->it_index++]); } From 012f0832f271a4c43a7285a7875b5a8b61ec0ae6 Mon Sep 17 00:00:00 2001 From: "blurb-it[bot]" <43283697+blurb-it[bot]@users.noreply.github.com> Date: Mon, 14 Mar 2022 11:15:12 +0000 Subject: [PATCH 2/6] =?UTF-8?q?=F0=9F=93=9C=F0=9F=A4=96=20Added=20by=20blu?= =?UTF-8?q?rb=5Fit.?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../Core and Builtins/2022-03-14-11-15-11.bpo-47012.5L6NoE.rst | 1 + 1 file changed, 1 insertion(+) create mode 100644 Misc/NEWS.d/next/Core and Builtins/2022-03-14-11-15-11.bpo-47012.5L6NoE.rst diff --git a/Misc/NEWS.d/next/Core and Builtins/2022-03-14-11-15-11.bpo-47012.5L6NoE.rst b/Misc/NEWS.d/next/Core and Builtins/2022-03-14-11-15-11.bpo-47012.5L6NoE.rst new file mode 100644 index 00000000000000..f85487f8d3ae79 --- /dev/null +++ b/Misc/NEWS.d/next/Core and Builtins/2022-03-14-11-15-11.bpo-47012.5L6NoE.rst @@ -0,0 +1 @@ +Speed up iteration of :class:`bytes` and :class:`bytearray` by 30%. Patch by Kumar Aditya. From d738245e3457978b26e2a8f2aa8619f291c92d41 Mon Sep 17 00:00:00 2001 From: Kumar Aditya <59607654+kumaraditya303@users.noreply.github.com> Date: Mon, 14 Mar 2022 21:41:09 +0530 Subject: [PATCH 3/6] Update bytearrayobject.c --- Objects/bytearrayobject.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Objects/bytearrayobject.c b/Objects/bytearrayobject.c index d4b5e33b950d89..01f9759dc4eb8a 100644 --- a/Objects/bytearrayobject.c +++ b/Objects/bytearrayobject.c @@ -2388,7 +2388,7 @@ static PyObject * bytearrayiter_next(bytesiterobject *it) { PyByteArrayObject *seq; - PyObject *item; + assert(it != NULL); seq = it->it_seq; From 4fcc8bbc4ed9d99d4409095fd296c5daea4b68d7 Mon Sep 17 00:00:00 2001 From: Kumar Aditya <59607654+kumaraditya303@users.noreply.github.com> Date: Mon, 14 Mar 2022 21:41:39 +0530 Subject: [PATCH 4/6] Update Objects/bytearrayobject.c --- Objects/bytearrayobject.c | 1 - 1 file changed, 1 deletion(-) diff --git a/Objects/bytearrayobject.c b/Objects/bytearrayobject.c index 01f9759dc4eb8a..6d9321b60307ab 100644 --- a/Objects/bytearrayobject.c +++ b/Objects/bytearrayobject.c @@ -2389,7 +2389,6 @@ bytearrayiter_next(bytesiterobject *it) { PyByteArrayObject *seq; - assert(it != NULL); seq = it->it_seq; if (seq == NULL) From 9e1978e6b4d9667691df72940366a59253b5138b Mon Sep 17 00:00:00 2001 From: Kumar Aditya <59607654+kumaraditya303@users.noreply.github.com> Date: Wed, 23 Mar 2022 07:14:23 +0000 Subject: [PATCH 5/6] add compile guard --- Include/internal/pycore_long.h | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/Include/internal/pycore_long.h b/Include/internal/pycore_long.h index 8decfe0621ee51..ec1d7923109039 100644 --- a/Include/internal/pycore_long.h +++ b/Include/internal/pycore_long.h @@ -23,8 +23,8 @@ extern void _PyLong_FiniTypes(PyInterpreterState *interp); #define _PyLong_SMALL_INTS _Py_SINGLETON(small_ints) // _PyLong_GetZero() and _PyLong_GetOne() must always be available -#if _PY_NSMALLPOSINTS < 2 -# error "_PY_NSMALLPOSINTS must be greater than 1" +#if _PY_NSMALLPOSINTS < 257 +# error "_PY_NSMALLPOSINTS must be greater than or equal to 257" #endif // Return a borrowed reference to the zero singleton. From 921e422c14cdb61d44ceba9525bf3d5b8b4a3647 Mon Sep 17 00:00:00 2001 From: Kumar Aditya <59607654+kumaraditya303@users.noreply.github.com> Date: Wed, 23 Mar 2022 07:15:19 +0000 Subject: [PATCH 6/6] add comment --- Include/internal/pycore_long.h | 1 + 1 file changed, 1 insertion(+) diff --git a/Include/internal/pycore_long.h b/Include/internal/pycore_long.h index ec1d7923109039..a33762402491b7 100644 --- a/Include/internal/pycore_long.h +++ b/Include/internal/pycore_long.h @@ -23,6 +23,7 @@ extern void _PyLong_FiniTypes(PyInterpreterState *interp); #define _PyLong_SMALL_INTS _Py_SINGLETON(small_ints) // _PyLong_GetZero() and _PyLong_GetOne() must always be available +// _PyLong_FromUnsignedChar must always be available #if _PY_NSMALLPOSINTS < 257 # error "_PY_NSMALLPOSINTS must be greater than or equal to 257" #endif