Mercurial > cpython-lru_cache-weakref
diff Python/ceval.c @ 31630:b4e4dbd24fe7 legacy-trunk
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.
| author | Raymond Hettinger <python@rcn.com> |
|---|---|
| date | Mon, 08 Mar 2004 23:25:30 +0000 |
| parents | bc006682acb2 |
| children | 83b0382e0d36 |
line wrap: on
line diff
--- a/Python/ceval.c Mon Mar 08 22:18:57 2004 +0000 +++ b/Python/ceval.c Mon Mar 08 23:25:30 2004 +0000 @@ -1721,35 +1721,21 @@ PREDICTED_WITH_ARG(UNPACK_SEQUENCE); case UNPACK_SEQUENCE: v = POP(); - if (PyTuple_CheckExact(v)) { - if (PyTuple_Size(v) != oparg) { - PyErr_SetString(PyExc_ValueError, - "unpack tuple of wrong size"); - why = WHY_EXCEPTION; - } - else { - for (; --oparg >= 0; ) { - w = PyTuple_GET_ITEM(v, oparg); - Py_INCREF(w); - PUSH(w); - } + if (PyTuple_CheckExact(v) && PyTuple_GET_SIZE(v) == oparg) { + PyObject **items = ((PyTupleObject *)v)->ob_item; + while (oparg--) { + w = items[oparg]; + Py_INCREF(w); + PUSH(w); } - } - else if (PyList_CheckExact(v)) { - if (PyList_Size(v) != oparg) { - PyErr_SetString(PyExc_ValueError, - "unpack list of wrong size"); - why = WHY_EXCEPTION; + } else if (PyList_CheckExact(v) && PyList_GET_SIZE(v) == oparg) { + PyObject **items = ((PyListObject *)v)->ob_item; + while (oparg--) { + w = items[oparg]; + Py_INCREF(w); + PUSH(w); } - else { - for (; --oparg >= 0; ) { - w = PyList_GET_ITEM(v, oparg); - Py_INCREF(w); - PUSH(w); - } - } - } - else if (unpack_iterable(v, oparg, + } else if (unpack_iterable(v, oparg, stack_pointer + oparg)) stack_pointer += oparg; else {
