Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit f114a3a

Browse files
committed
Refactor and optimize code for UNPACK_SEQUENCE.
* Defer error handling for wrong number of arguments to the unpack_iterable() function. Cuts the code size almost in half. * Replace function calls to PyList_Size() and PyTuple_Size() with their smaller and faster macro counterparts. * Move the constant structure references outside of the inner loops.
1 parent 4b6b7f1 commit f114a3a

1 file changed

Lines changed: 13 additions & 27 deletions

File tree

Python/ceval.c

Lines changed: 13 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -1721,35 +1721,21 @@ eval_frame(PyFrameObject *f)
17211721
PREDICTED_WITH_ARG(UNPACK_SEQUENCE);
17221722
case UNPACK_SEQUENCE:
17231723
v = POP();
1724-
if (PyTuple_CheckExact(v)) {
1725-
if (PyTuple_Size(v) != oparg) {
1726-
PyErr_SetString(PyExc_ValueError,
1727-
"unpack tuple of wrong size");
1728-
why = WHY_EXCEPTION;
1729-
}
1730-
else {
1731-
for (; --oparg >= 0; ) {
1732-
w = PyTuple_GET_ITEM(v, oparg);
1733-
Py_INCREF(w);
1734-
PUSH(w);
1735-
}
1736-
}
1737-
}
1738-
else if (PyList_CheckExact(v)) {
1739-
if (PyList_Size(v) != oparg) {
1740-
PyErr_SetString(PyExc_ValueError,
1741-
"unpack list of wrong size");
1742-
why = WHY_EXCEPTION;
1724+
if (PyTuple_CheckExact(v) && PyTuple_GET_SIZE(v) == oparg) {
1725+
PyObject **items = ((PyTupleObject *)v)->ob_item;
1726+
while (oparg--) {
1727+
w = items[oparg];
1728+
Py_INCREF(w);
1729+
PUSH(w);
17431730
}
1744-
else {
1745-
for (; --oparg >= 0; ) {
1746-
w = PyList_GET_ITEM(v, oparg);
1747-
Py_INCREF(w);
1748-
PUSH(w);
1749-
}
1731+
} else if (PyList_CheckExact(v) && PyList_GET_SIZE(v) == oparg) {
1732+
PyObject **items = ((PyListObject *)v)->ob_item;
1733+
while (oparg--) {
1734+
w = items[oparg];
1735+
Py_INCREF(w);
1736+
PUSH(w);
17501737
}
1751-
}
1752-
else if (unpack_iterable(v, oparg,
1738+
} else if (unpack_iterable(v, oparg,
17531739
stack_pointer + oparg))
17541740
stack_pointer += oparg;
17551741
else {

0 commit comments

Comments
 (0)