]> granicus.if.org Git - python/commitdiff
bpo-29737: Optimize concatenating with empty tuple. (#524)
authorSerhiy Storchaka <storchaka@gmail.com>
Mon, 6 Mar 2017 21:39:35 +0000 (23:39 +0200)
committerGitHub <noreply@github.com>
Mon, 6 Mar 2017 21:39:35 +0000 (23:39 +0200)
Objects/tupleobject.c

index 64db80692f07a9ce9ca2f969ee51ae900f23119a..4c6bbe71a194051efe3e0ad7e5df5e3934cc84a4 100644 (file)
@@ -446,6 +446,10 @@ tupleconcat(PyTupleObject *a, PyObject *bb)
     Py_ssize_t i;
     PyObject **src, **dest;
     PyTupleObject *np;
+    if (Py_SIZE(a) == 0 && PyTuple_CheckExact(bb)) {
+        Py_INCREF(bb);
+        return bb;
+    }
     if (!PyTuple_Check(bb)) {
         PyErr_Format(PyExc_TypeError,
              "can only concatenate tuple (not \"%.200s\") to tuple",
@@ -453,6 +457,10 @@ tupleconcat(PyTupleObject *a, PyObject *bb)
         return NULL;
     }
 #define b ((PyTupleObject *)bb)
+    if (Py_SIZE(b) == 0 && PyTuple_CheckExact(a)) {
+        Py_INCREF(a);
+        return (PyObject *)a;
+    }
     if (Py_SIZE(a) > PY_SSIZE_T_MAX - Py_SIZE(b))
         return PyErr_NoMemory();
     size = Py_SIZE(a) + Py_SIZE(b);