Skip to content
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.

Commit 98e80c2

Browse files
authoredMar 6, 2017
bpo-29737: Optimize concatenating with empty tuple. (python#524)
1 parent be487a6 commit 98e80c2

File tree

1 file changed

+8
-0
lines changed

1 file changed

+8
-0
lines changed
 

‎Objects/tupleobject.c

+8
Original file line numberDiff line numberDiff line change
@@ -446,13 +446,21 @@ tupleconcat(PyTupleObject *a, PyObject *bb)
446446
Py_ssize_t i;
447447
PyObject **src, **dest;
448448
PyTupleObject *np;
449+
if (Py_SIZE(a) == 0 && PyTuple_CheckExact(bb)) {
450+
Py_INCREF(bb);
451+
return bb;
452+
}
449453
if (!PyTuple_Check(bb)) {
450454
PyErr_Format(PyExc_TypeError,
451455
"can only concatenate tuple (not \"%.200s\") to tuple",
452456
Py_TYPE(bb)->tp_name);
453457
return NULL;
454458
}
455459
#define b ((PyTupleObject *)bb)
460+
if (Py_SIZE(b) == 0 && PyTuple_CheckExact(a)) {
461+
Py_INCREF(a);
462+
return (PyObject *)a;
463+
}
456464
if (Py_SIZE(a) > PY_SSIZE_T_MAX - Py_SIZE(b))
457465
return PyErr_NoMemory();
458466
size = Py_SIZE(a) + Py_SIZE(b);

0 commit comments

Comments
 (0)
Please sign in to comment.