Skip to content

Commit 17100c0

Browse files
committed
set types
1 parent 94cbc7c commit 17100c0

File tree

6 files changed

+30
-14
lines changed

6 files changed

+30
-14
lines changed

Include/internal/pycore_freelist.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,7 @@ _Py_freelists_GET(void)
4040
#define _Py_FREELIST_POP(TYPE, NAME) \
4141
_Py_CAST(TYPE*, _PyFreeList_Pop(&_Py_freelists_GET()->NAME))
4242

43+
4344
// Pops a non-PyObject data structure from the freelist, returns NULL if the
4445
// freelist is empty.
4546
#define _Py_FREELIST_POP_MEM(NAME) \

Include/internal/pycore_freelist_state.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ extern "C" {
1717
# define Py_ints_MAXFREELIST 100
1818
# define Py_slices_MAXFREELIST 1
1919
# define Py_ranges_MAXFREELIST 10
20-
# define Py_rangeiters_MAXFREELIST 10
20+
# define Py_shared_iters_MAXFREELIST 24
2121
# define Py_contexts_MAXFREELIST 255
2222
# define Py_async_gens_MAXFREELIST 80
2323
# define Py_async_gen_asends_MAXFREELIST 80
@@ -45,7 +45,7 @@ struct _Py_freelists {
4545
struct _Py_freelist dictkeys;
4646
struct _Py_freelist slices;
4747
struct _Py_freelist ranges;
48-
struct _Py_freelist rangeiters;
48+
struct _Py_freelist shared_iters;
4949
struct _Py_freelist contexts;
5050
struct _Py_freelist async_gens;
5151
struct _Py_freelist async_gen_asends;

Objects/listobject.c

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -3910,15 +3910,19 @@ PyTypeObject PyListIter_Type = {
39103910
static PyObject *
39113911
list_iter(PyObject *seq)
39123912
{
3913-
_PyListIterObject *it;
3914-
39153913
if (!PyList_Check(seq)) {
39163914
PyErr_BadInternalCall();
39173915
return NULL;
39183916
}
3919-
it = PyObject_GC_New(_PyListIterObject, &PyListIter_Type);
3920-
if (it == NULL)
3921-
return NULL;
3917+
_PyListIterObject *it = _Py_FREELIST_POP(_PyListIterObject, shared_iters);
3918+
if (it == NULL) {
3919+
it = PyObject_GC_New(_PyListIterObject, &PyListIter_Type);
3920+
if (it == NULL) {
3921+
return NULL;
3922+
}
3923+
} else {
3924+
Py_SET_TYPE(it, &PyListIter_Type);
3925+
}
39223926
it->it_index = 0;
39233927
it->it_seq = (PyListObject *)Py_NewRef(seq);
39243928
_PyObject_GC_TRACK(it);
@@ -3931,7 +3935,7 @@ listiter_dealloc(PyObject *self)
39313935
_PyListIterObject *it = (_PyListIterObject *)self;
39323936
_PyObject_GC_UNTRACK(it);
39333937
Py_XDECREF(it->it_seq);
3934-
PyObject_GC_Del(it);
3938+
_Py_FREELIST_FREE(shared_iters, it, PyObject_GC_Del);
39353939
}
39363940

39373941
static int

Objects/rangeobject.c

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -886,7 +886,7 @@ rangeiter_setstate(_PyRangeIterObject *r, PyObject *state)
886886
static void
887887
rangeiter_dealloc(_PyRangeIterObject *r)
888888
{
889-
_Py_FREELIST_FREE(rangeiters, r, PyObject_Free);
889+
_Py_FREELIST_FREE(shared_iters, r, PyObject_Free);
890890
}
891891

892892
PyDoc_STRVAR(reduce_doc, "Return state information for pickling.");
@@ -969,12 +969,15 @@ get_len_of_range(long lo, long hi, long step)
969969
static PyObject *
970970
fast_range_iter(long start, long stop, long step, long len)
971971
{
972-
_PyRangeIterObject *it = _Py_FREELIST_POP(_PyRangeIterObject, rangeiters);
972+
_PyRangeIterObject *it = _Py_FREELIST_POP(_PyRangeIterObject, shared_iters);
973973
if (it == NULL) {
974974
it = PyObject_New(_PyRangeIterObject, &PyRangeIter_Type);
975975
if (it == NULL)
976976
return NULL;
977977
}
978+
else {
979+
Py_SET_TYPE(it, &PyRangeIter_Type);
980+
}
978981
it->start = start;
979982
it->step = step;
980983
it->len = len;

Objects/tupleobject.c

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -994,7 +994,8 @@ tupleiter_dealloc(_PyTupleIterObject *it)
994994
{
995995
_PyObject_GC_UNTRACK(it);
996996
Py_XDECREF(it->it_seq);
997-
PyObject_GC_Del(it);
997+
assert(sizeof(_PyTupleIterObject)==sizeof(_PyListIterObject));
998+
_Py_FREELIST_FREE(shared_iters, it, PyObject_GC_Del);
998999
}
9991000

10001001
static int
@@ -1122,9 +1123,15 @@ tuple_iter(PyObject *seq)
11221123
PyErr_BadInternalCall();
11231124
return NULL;
11241125
}
1125-
it = PyObject_GC_New(_PyTupleIterObject, &PyTupleIter_Type);
1126-
if (it == NULL)
1127-
return NULL;
1126+
it = _Py_FREELIST_POP(_PyTupleIterObject, shared_iters);
1127+
1128+
if (it == NULL) {
1129+
it = PyObject_GC_New(_PyTupleIterObject, &PyTupleIter_Type);
1130+
if (it == NULL)
1131+
return NULL;
1132+
} else {
1133+
Py_SET_TYPE(it, &PyTupleIter_Type);
1134+
}
11281135
it->it_index = 0;
11291136
it->it_seq = (PyTupleObject *)Py_NewRef(seq);
11301137
_PyObject_GC_TRACK(it);

Objects/typeobject.c

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
#include "pycore_code.h" // CO_FAST_FREE
77
#include "pycore_dict.h" // _PyDict_KeysSize()
88
#include "pycore_frame.h" // _PyInterpreterFrame
9+
#include "pycore_freelist.h" // _Py_FREELIST_FREE(), _Py_FREELIST_POP()
910
#include "pycore_lock.h" // _PySeqLock_*
1011
#include "pycore_long.h" // _PyLong_IsNegative(), _PyLong_GetOne()
1112
#include "pycore_memoryobject.h" // _PyMemoryView_FromBufferProc()

0 commit comments

Comments
 (0)