Skip to content

Commit 0563ec9

Browse files
committed
gh-153062: Keep the default build on the simple tee iteration path
The free-threading snapshot and revalidation add per-element overhead on the default build, where the GIL already serializes access. Guard that path under Py_GIL_DISABLED so the default build keeps the original iteration and the free-threaded path is unchanged.
1 parent c7f1112 commit 0563ec9

1 file changed

Lines changed: 19 additions & 0 deletions

File tree

Modules/itertoolsmodule.c

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -951,6 +951,24 @@ tee_next(PyObject *op)
951951
teeobject *to = teeobject_CAST(op);
952952
PyObject *value;
953953

954+
#ifndef Py_GIL_DISABLED
955+
/* The GIL already serializes access, so keep the simple path without the
956+
snapshot and revalidation that the free-threaded build needs. */
957+
if (to->index >= LINKCELLS) {
958+
PyObject *link = teedataobject_jumplink(to->state, to->dataobj);
959+
if (link == NULL) {
960+
return NULL;
961+
}
962+
Py_SETREF(to->dataobj, (teedataobject *)link);
963+
to->index = 0;
964+
}
965+
value = teedataobject_getitem(to->dataobj, to->index);
966+
if (value == NULL) {
967+
return NULL;
968+
}
969+
to->index++;
970+
return value;
971+
#else
954972
for (;;) {
955973
teedataobject *dataobj;
956974
int index;
@@ -991,6 +1009,7 @@ tee_next(PyObject *op)
9911009
Py_XDECREF(link);
9921010
Py_DECREF(dataobj);
9931011
}
1012+
#endif
9941013
}
9951014

9961015
static int

0 commit comments

Comments
 (0)