summaryrefslogtreecommitdiffstats
path: root/Python
diff options
context:
space:
mode:
authorRaymond Hettinger <python@rcn.com>2004-03-08 23:25:30 (GMT)
committerRaymond Hettinger <python@rcn.com>2004-03-08 23:25:30 (GMT)
commitf114a3ae631b0391f9ca7d631a26b7ca4e99d4f9 (patch)
tree3a909c70269bf7cdce6b1ba7381b9a011e5e80ef /Python
parent4b6b7f151501eaee0af46220553c78607474e420 (diff)
downloadcpython-f114a3ae631b0391f9ca7d631a26b7ca4e99d4f9.zip
cpython-f114a3ae631b0391f9ca7d631a26b7ca4e99d4f9.tar.gz
cpython-f114a3ae631b0391f9ca7d631a26b7ca4e99d4f9.tar.bz2
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.
Diffstat (limited to 'Python')
-rw-r--r--Python/ceval.c40
1 files changed, 13 insertions, 27 deletions
diff --git a/Python/ceval.c b/Python/ceval.c
index b20934c..1e724c5 100644
--- a/Python/ceval.c
+++ b/Python/ceval.c
@@ -1721,35 +1721,21 @@ eval_frame(PyFrameObject *f)
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);
- }
- }
- }
- else if (PyList_CheckExact(v)) {
- if (PyList_Size(v) != oparg) {
- PyErr_SetString(PyExc_ValueError,
- "unpack list of wrong size");
- why = WHY_EXCEPTION;
+ 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 {
- for (; --oparg >= 0; ) {
- w = PyList_GET_ITEM(v, oparg);
- Py_INCREF(w);
- PUSH(w);
- }
+ } 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 if (unpack_iterable(v, oparg,
+ } else if (unpack_iterable(v, oparg,
stack_pointer + oparg))
stack_pointer += oparg;
else {