Skip to content

Commit

Permalink
Use freelist for rangeiter object
Browse files Browse the repository at this point in the history
  • Loading branch information
eendebakpt committed Nov 11, 2024
1 parent 2e0280b commit ffde646
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 4 deletions.
2 changes: 2 additions & 0 deletions Include/internal/pycore_freelist_state.h
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ extern "C" {
# define Py_floats_MAXFREELIST 100
# define Py_slices_MAXFREELIST 1
# define Py_ranges_MAXFREELIST 10
# define Py_rangeiters_MAXFREELIST 10
# define Py_contexts_MAXFREELIST 255
# define Py_async_gens_MAXFREELIST 80
# define Py_async_gen_asends_MAXFREELIST 80
Expand All @@ -42,6 +43,7 @@ struct _Py_freelists {
struct _Py_freelist dictkeys;
struct _Py_freelist slices;
struct _Py_freelist ranges;
struct _Py_freelist rangeiters;
struct _Py_freelist contexts;
struct _Py_freelist async_gens;
struct _Py_freelist async_gen_asends;
Expand Down
17 changes: 13 additions & 4 deletions Objects/rangeobject.c
Original file line number Diff line number Diff line change
Expand Up @@ -885,6 +885,12 @@ rangeiter_setstate(_PyRangeIterObject *r, PyObject *state)
Py_RETURN_NONE;
}

static void
rangeiter_dealloc(_PyRangeIterObject *r)
{
_Py_FREELIST_FREE(rangeiters, r, PyObject_Free);
}

PyDoc_STRVAR(reduce_doc, "Return state information for pickling.");
PyDoc_STRVAR(setstate_doc, "Set state information for unpickling.");

Expand All @@ -904,7 +910,7 @@ PyTypeObject PyRangeIter_Type = {
sizeof(_PyRangeIterObject), /* tp_basicsize */
0, /* tp_itemsize */
/* methods */
(destructor)PyObject_Free, /* tp_dealloc */
(destructor)rangeiter_dealloc, /* tp_dealloc */
0, /* tp_vectorcall_offset */
0, /* tp_getattr */
0, /* tp_setattr */
Expand Down Expand Up @@ -965,9 +971,12 @@ get_len_of_range(long lo, long hi, long step)
static PyObject *
fast_range_iter(long start, long stop, long step, long len)
{
_PyRangeIterObject *it = PyObject_New(_PyRangeIterObject, &PyRangeIter_Type);
if (it == NULL)
return NULL;
_PyRangeIterObject *it = _Py_FREELIST_POP(_PyRangeIterObject, rangeiters);
if (it == NULL) {
it = PyObject_New(_PyRangeIterObject, &PyRangeIter_Type);
if (it == NULL)
return NULL;
}
it->start = start;
it->step = step;
it->len = len;
Expand Down

0 comments on commit ffde646

Please sign in to comment.