Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

gh-126703: Add freelist for range and range_iter objects #128619

Open
wants to merge 5 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions Include/internal/pycore_freelist_state.h
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@ extern "C" {
# define Py_floats_MAXFREELIST 100
# define Py_ints_MAXFREELIST 100
# define Py_slices_MAXFREELIST 1
# define Py_ranges_MAXFREELIST 6
# define Py_range_iters_MAXFREELIST 6
# define Py_contexts_MAXFREELIST 255
# define Py_async_gens_MAXFREELIST 80
# define Py_async_gen_asends_MAXFREELIST 80
Expand All @@ -42,6 +44,8 @@ struct _Py_freelists {
struct _Py_freelist dicts;
struct _Py_freelist dictkeys;
struct _Py_freelist slices;
struct _Py_freelist ranges;
struct _Py_freelist range_iters;
struct _Py_freelist contexts;
struct _Py_freelist async_gens;
struct _Py_freelist async_gen_asends;
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Improve performance of :class:`range` by using a freelist.
2 changes: 2 additions & 0 deletions Objects/object.c
Original file line number Diff line number Diff line change
Expand Up @@ -926,6 +926,8 @@ _PyObject_ClearFreeLists(struct _Py_freelists *freelists, int is_finalization)
clear_freelist(&freelists->dicts, is_finalization, free_object);
clear_freelist(&freelists->dictkeys, is_finalization, PyMem_Free);
clear_freelist(&freelists->slices, is_finalization, free_object);
clear_freelist(&freelists->ranges, is_finalization, free_object);
clear_freelist(&freelists->range_iters, is_finalization, free_object);
clear_freelist(&freelists->contexts, is_finalization, free_object);
clear_freelist(&freelists->async_gens, is_finalization, free_object);
clear_freelist(&freelists->async_gen_asends, is_finalization, free_object);
Expand Down
32 changes: 23 additions & 9 deletions Objects/rangeobject.c
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
#include "Python.h"
#include "pycore_abstract.h" // _PyIndex_Check()
#include "pycore_ceval.h" // _PyEval_GetBuiltin()
#include "pycore_freelist.h"
#include "pycore_long.h" // _PyLong_GetZero()
#include "pycore_modsupport.h" // _PyArg_NoKwnames()
#include "pycore_range.h"
Expand Down Expand Up @@ -51,16 +52,18 @@ static rangeobject *
make_range_object(PyTypeObject *type, PyObject *start,
PyObject *stop, PyObject *step)
{
rangeobject *obj = NULL;
PyObject *length;
length = compute_range_length(start, stop, step);
if (length == NULL) {
return NULL;
}
obj = PyObject_New(rangeobject, type);
rangeobject *obj = _Py_FREELIST_POP(rangeobject, ranges);
if (obj == NULL) {
Py_DECREF(length);
return NULL;
obj = PyObject_New(rangeobject, type);
if (obj == NULL) {
Py_DECREF(length);
return NULL;
}
}
obj->start = start;
obj->stop = stop;
Expand Down Expand Up @@ -170,7 +173,7 @@ range_dealloc(rangeobject *r)
Py_DECREF(r->stop);
Py_DECREF(r->step);
Py_DECREF(r->length);
PyObject_Free(r);
_Py_FREELIST_FREE(ranges, r, PyObject_Free);
}

static unsigned long
Expand Down Expand Up @@ -880,6 +883,12 @@ rangeiter_setstate(_PyRangeIterObject *r, PyObject *state)
Py_RETURN_NONE;
}

static void
rangeiter_dealloc(PyObject *self)
{
_Py_FREELIST_FREE(range_iters, (_PyRangeIterObject *)self, PyObject_Free);
}

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

Expand All @@ -899,7 +908,7 @@ PyTypeObject PyRangeIter_Type = {
sizeof(_PyRangeIterObject), /* tp_basicsize */
0, /* tp_itemsize */
/* methods */
(destructor)PyObject_Free, /* tp_dealloc */
rangeiter_dealloc, /* tp_dealloc */
0, /* tp_vectorcall_offset */
0, /* tp_getattr */
0, /* tp_setattr */
Expand Down Expand Up @@ -960,9 +969,14 @@ 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, range_iters);
if (it == NULL) {
it = PyObject_New(_PyRangeIterObject, &PyRangeIter_Type);
if (it == NULL) {
return NULL;
}
}
assert(Py_IS_TYPE(it, &PyRangeIter_Type));
it->start = start;
it->step = step;
it->len = len;
Expand Down
Loading