From 7a54d01ce9bd4d55306d66ba1d46c926b20d5a10 Mon Sep 17 00:00:00 2001 From: Kumar Aditya <59607654+kumaraditya303@users.noreply.github.com> Date: Sat, 12 Mar 2022 08:49:41 +0000 Subject: [PATCH 1/5] fast bytearray for list and tuple --- Objects/bytearrayobject.c | 17 ++++++++++++++++- 1 file changed, 16 insertions(+), 1 deletion(-) diff --git a/Objects/bytearrayobject.c b/Objects/bytearrayobject.c index 0ebb2ece39d5d3..71690f48d7a777 100644 --- a/Objects/bytearrayobject.c +++ b/Objects/bytearrayobject.c @@ -844,8 +844,23 @@ bytearray___init___impl(PyByteArrayObject *self, PyObject *arg, return -1; } - /* XXX Optimize this if the arguments is a list, tuple */ + if (PyList_CheckExact(arg) || PyTuple_CheckExact(arg)) { + Py_ssize_t size = PySequence_Fast_GET_SIZE(arg); + if (PyByteArray_Resize((PyObject *)self, size) < 0) { + return -1; + } + PyObject **items = PySequence_Fast_ITEMS(arg); + for (Py_ssize_t i = 0; i < size; i++) { + int value; + int rc = _getbytevalue(items[i], &value); + if (!rc) { + return -1; + } + PyByteArray_AS_STRING(self)[i] = value; + } + return 0; + } /* Get the iterator */ it = PyObject_GetIter(arg); if (it == NULL) { From ce8d0cfaadf498513da498794ac5d118b51a5bbb Mon Sep 17 00:00:00 2001 From: "blurb-it[bot]" <43283697+blurb-it[bot]@users.noreply.github.com> Date: Sat, 12 Mar 2022 09:44:32 +0000 Subject: [PATCH 2/5] =?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-12-09-44-31.bpo-46993.-13hGo.rst | 1 + 1 file changed, 1 insertion(+) create mode 100644 Misc/NEWS.d/next/Core and Builtins/2022-03-12-09-44-31.bpo-46993.-13hGo.rst diff --git a/Misc/NEWS.d/next/Core and Builtins/2022-03-12-09-44-31.bpo-46993.-13hGo.rst b/Misc/NEWS.d/next/Core and Builtins/2022-03-12-09-44-31.bpo-46993.-13hGo.rst new file mode 100644 index 00000000000000..b7f7078856bd53 --- /dev/null +++ b/Misc/NEWS.d/next/Core and Builtins/2022-03-12-09-44-31.bpo-46993.-13hGo.rst @@ -0,0 +1 @@ +Speed up :class:`bytearray` creation from :class:`list` and :class:`tuple` by 40%. Patch by Kumar Aditya. From 391840bf822b347ba4cce2bb3b9f1f87fb74acd2 Mon Sep 17 00:00:00 2001 From: Kumar Aditya <59607654+kumaraditya303@users.noreply.github.com> Date: Sun, 13 Mar 2022 06:13:59 +0000 Subject: [PATCH 3/5] code review --- Objects/bytearrayobject.c | 8 ++++++-- 1 file changed, 6 insertions(+), 2 deletions(-) diff --git a/Objects/bytearrayobject.c b/Objects/bytearrayobject.c index 71690f48d7a777..a69d24f38cd709 100644 --- a/Objects/bytearrayobject.c +++ b/Objects/bytearrayobject.c @@ -850,17 +850,21 @@ bytearray___init___impl(PyByteArrayObject *self, PyObject *arg, return -1; } PyObject **items = PySequence_Fast_ITEMS(arg); - + char *s = PyByteArray_AS_STRING(self); for (Py_ssize_t i = 0; i < size; i++) { int value; + if (!PyLong_CheckExact(items[i])) { + goto slowpath; + } int rc = _getbytevalue(items[i], &value); if (!rc) { return -1; } - PyByteArray_AS_STRING(self)[i] = value; + s[i] = value; } return 0; } +slowpath: /* Get the iterator */ it = PyObject_GetIter(arg); if (it == NULL) { From c937642f127c939a36e5db417bd2f73eef0f61fc Mon Sep 17 00:00:00 2001 From: Kumar Aditya <59607654+kumaraditya303@users.noreply.github.com> Date: Sun, 13 Mar 2022 06:58:28 +0000 Subject: [PATCH 4/5] resize to 0 with slowpath --- Objects/bytearrayobject.c | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/Objects/bytearrayobject.c b/Objects/bytearrayobject.c index a69d24f38cd709..618a5d74656ab8 100644 --- a/Objects/bytearrayobject.c +++ b/Objects/bytearrayobject.c @@ -865,6 +865,10 @@ bytearray___init___impl(PyByteArrayObject *self, PyObject *arg, return 0; } slowpath: + if (Py_SIZE(self) != 0) { + if (PyByteArray_Resize((PyObject *)self, 0) < 0) + return -1; + } /* Get the iterator */ it = PyObject_GetIter(arg); if (it == NULL) { From 18e41d576a519ded5d564c3de80b63cb8bcfda3e Mon Sep 17 00:00:00 2001 From: Kumar Aditya <59607654+kumaraditya303@users.noreply.github.com> Date: Sun, 13 Mar 2022 07:07:10 +0000 Subject: [PATCH 5/5] add a comment --- Objects/bytearrayobject.c | 10 ++++++---- 1 file changed, 6 insertions(+), 4 deletions(-) diff --git a/Objects/bytearrayobject.c b/Objects/bytearrayobject.c index 618a5d74656ab8..3493ff046ae139 100644 --- a/Objects/bytearrayobject.c +++ b/Objects/bytearrayobject.c @@ -854,6 +854,12 @@ bytearray___init___impl(PyByteArrayObject *self, PyObject *arg, for (Py_ssize_t i = 0; i < size; i++) { int value; if (!PyLong_CheckExact(items[i])) { + /* Resize to 0 and go through slowpath */ + if (Py_SIZE(self) != 0) { + if (PyByteArray_Resize((PyObject *)self, 0) < 0) { + return -1; + } + } goto slowpath; } int rc = _getbytevalue(items[i], &value); @@ -865,10 +871,6 @@ bytearray___init___impl(PyByteArrayObject *self, PyObject *arg, return 0; } slowpath: - if (Py_SIZE(self) != 0) { - if (PyByteArray_Resize((PyObject *)self, 0) < 0) - return -1; - } /* Get the iterator */ it = PyObject_GetIter(arg); if (it == NULL) {