GH-96793: Specialize FOR_ITER for generators. (GH-98772)

This commit is contained in:
Mark Shannon 2022-11-07 06:49:51 -08:00 committed by GitHub
parent 80c08d1cd6
commit 4a1c58d504
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
13 changed files with 207 additions and 71 deletions

View file

@ -2184,7 +2184,7 @@ int
#endif
void
_Py_Specialize_ForIter(PyObject *iter, _Py_CODEUNIT *instr)
_Py_Specialize_ForIter(PyObject *iter, _Py_CODEUNIT *instr, int oparg)
{
assert(_PyOpcode_Caches[FOR_ITER] == INLINE_CACHE_ENTRIES_FOR_ITER);
_PyForIterCache *cache = (_PyForIterCache *)(instr + 1);
@ -2199,6 +2199,11 @@ _Py_Specialize_ForIter(PyObject *iter, _Py_CODEUNIT *instr)
_Py_SET_OPCODE(*instr, FOR_ITER_RANGE);
goto success;
}
else if (tp == &PyGen_Type && oparg <= SHRT_MAX) {
assert(_Py_OPCODE(instr[oparg + INLINE_CACHE_ENTRIES_FOR_ITER + 1]) == END_FOR);
_Py_SET_OPCODE(*instr, FOR_ITER_GEN);
goto success;
}
SPECIALIZATION_FAIL(FOR_ITER,
_PySpecialization_ClassifyIterator(iter));
STAT_INC(FOR_ITER, failure);