GH-96793: Change FOR_ITER to not pop the iterator on exhaustion. (GH-96801)

Change FOR_ITER to have the same stack effect regardless of whether it branches or not.
Performance is unchanged as FOR_ITER (and specialized forms jump over the cleanup code).
This commit is contained in:
Mark Shannon 2022-10-27 03:55:03 -07:00 committed by GitHub
parent e60892f9db
commit 22863df7ca
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
14 changed files with 277 additions and 247 deletions

View file

@ -1298,6 +1298,14 @@ handle_eval_breaker:
DISPATCH();
}
TARGET(END_FOR) {
PyObject *value = POP();
Py_DECREF(value);
value = POP();
Py_DECREF(value);
DISPATCH();
}
TARGET(UNARY_POSITIVE) {
PyObject *value = TOP();
PyObject *res = PyNumber_Positive(value);
@ -3844,9 +3852,11 @@ handle_eval_breaker:
_PyErr_Clear(tstate);
}
/* iterator ended normally */
assert(_Py_OPCODE(next_instr[INLINE_CACHE_ENTRIES_FOR_ITER + oparg] == END_FOR));
STACK_SHRINK(1);
Py_DECREF(iter);
JUMPBY(INLINE_CACHE_ENTRIES_FOR_ITER + oparg);
/* Skip END_FOR */
JUMPBY(INLINE_CACHE_ENTRIES_FOR_ITER + oparg + 1);
DISPATCH();
}
@ -3884,7 +3894,7 @@ handle_eval_breaker:
}
STACK_SHRINK(1);
Py_DECREF(it);
JUMPBY(INLINE_CACHE_ENTRIES_FOR_ITER + oparg);
JUMPBY(INLINE_CACHE_ENTRIES_FOR_ITER + oparg + 1);
DISPATCH();
}
@ -3898,7 +3908,7 @@ handle_eval_breaker:
if (r->index >= r->len) {
STACK_SHRINK(1);
Py_DECREF(r);
JUMPBY(INLINE_CACHE_ENTRIES_FOR_ITER + oparg);
JUMPBY(INLINE_CACHE_ENTRIES_FOR_ITER + oparg + 1);
DISPATCH();
}
long value = (long)(r->start +