33#include "Python.h"
44#include "pycore_abstract.h" // _PyIndex_Check()
55#include "pycore_ceval.h" // _PyEval_GetBuiltin()
6+ #include "pycore_freelist.h"
67#include "pycore_long.h" // _PyLong_GetZero()
78#include "pycore_modsupport.h" // _PyArg_NoKwnames()
89#include "pycore_range.h"
@@ -51,16 +52,18 @@ static rangeobject *
5152make_range_object (PyTypeObject * type , PyObject * start ,
5253 PyObject * stop , PyObject * step )
5354{
54- rangeobject * obj = NULL ;
5555 PyObject * length ;
5656 length = compute_range_length (start , stop , step );
5757 if (length == NULL ) {
5858 return NULL ;
5959 }
60- obj = PyObject_New (rangeobject , type );
60+ rangeobject * obj = _Py_FREELIST_POP (rangeobject , ranges );
6161 if (obj == NULL ) {
62- Py_DECREF (length );
63- return NULL ;
62+ obj = PyObject_New (rangeobject , type );
63+ if (obj == NULL ) {
64+ Py_DECREF (length );
65+ return NULL ;
66+ }
6467 }
6568 obj -> start = start ;
6669 obj -> stop = stop ;
@@ -171,7 +174,7 @@ range_dealloc(PyObject *op)
171174 Py_DECREF (r -> stop );
172175 Py_DECREF (r -> step );
173176 Py_DECREF (r -> length );
174- PyObject_Free ( r );
177+ _Py_FREELIST_FREE ( ranges , r , PyObject_Free );
175178}
176179
177180static unsigned long
@@ -895,6 +898,12 @@ rangeiter_setstate(PyObject *op, PyObject *state)
895898 Py_RETURN_NONE ;
896899}
897900
901+ static void
902+ rangeiter_dealloc (PyObject * self )
903+ {
904+ _Py_FREELIST_FREE (range_iters , (_PyRangeIterObject * )self , PyObject_Free );
905+ }
906+
898907PyDoc_STRVAR (reduce_doc , "Return state information for pickling." );
899908PyDoc_STRVAR (setstate_doc , "Set state information for unpickling." );
900909
@@ -911,7 +920,7 @@ PyTypeObject PyRangeIter_Type = {
911920 sizeof (_PyRangeIterObject ), /* tp_basicsize */
912921 0 , /* tp_itemsize */
913922 /* methods */
914- 0 , /* tp_dealloc */
923+ rangeiter_dealloc , /* tp_dealloc */
915924 0 , /* tp_vectorcall_offset */
916925 0 , /* tp_getattr */
917926 0 , /* tp_setattr */
@@ -972,9 +981,14 @@ get_len_of_range(long lo, long hi, long step)
972981static PyObject *
973982fast_range_iter (long start , long stop , long step , long len )
974983{
975- _PyRangeIterObject * it = PyObject_New (_PyRangeIterObject , & PyRangeIter_Type );
976- if (it == NULL )
977- return NULL ;
984+ _PyRangeIterObject * it = _Py_FREELIST_POP (_PyRangeIterObject , range_iters );
985+ if (it == NULL ) {
986+ it = PyObject_New (_PyRangeIterObject , & PyRangeIter_Type );
987+ if (it == NULL ) {
988+ return NULL ;
989+ }
990+ }
991+ assert (Py_IS_TYPE (it , & PyRangeIter_Type ));
978992 it -> start = start ;
979993 it -> step = step ;
980994 it -> len = len ;
0 commit comments