Commit 3af61fe9 authored by Raymond Hettinger's avatar Raymond Hettinger

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.
parent a4b8b3c8
...@@ -1721,35 +1721,21 @@ eval_frame(PyFrameObject *f) ...@@ -1721,35 +1721,21 @@ eval_frame(PyFrameObject *f)
PREDICTED_WITH_ARG(UNPACK_SEQUENCE); PREDICTED_WITH_ARG(UNPACK_SEQUENCE);
case UNPACK_SEQUENCE: case UNPACK_SEQUENCE:
v = POP(); v = POP();
if (PyTuple_CheckExact(v)) { if (PyTuple_CheckExact(v) && PyTuple_GET_SIZE(v) == oparg) {
if (PyTuple_Size(v) != oparg) { PyObject **items = ((PyTupleObject *)v)->ob_item;
PyErr_SetString(PyExc_ValueError, while (oparg--) {
"unpack tuple of wrong size"); w = items[oparg];
why = WHY_EXCEPTION; Py_INCREF(w);
} PUSH(w);
else {
for (; --oparg >= 0; ) {
w = PyTuple_GET_ITEM(v, 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 { } else if (PyList_CheckExact(v) && PyList_GET_SIZE(v) == oparg) {
for (; --oparg >= 0; ) { PyObject **items = ((PyListObject *)v)->ob_item;
w = PyList_GET_ITEM(v, oparg); while (oparg--) {
Py_INCREF(w); w = items[oparg];
PUSH(w); Py_INCREF(w);
} PUSH(w);
} }
} } else if (unpack_iterable(v, oparg,
else if (unpack_iterable(v, oparg,
stack_pointer + oparg)) stack_pointer + oparg))
stack_pointer += oparg; stack_pointer += oparg;
else { else {
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment