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 ;
@@ -170,7 +173,7 @@ range_dealloc(rangeobject *r)
170173 Py_DECREF (r -> stop );
171174 Py_DECREF (r -> step );
172175 Py_DECREF (r -> length );
173- PyObject_Free ( r );
176+ _Py_FREELIST_FREE ( ranges , r , PyObject_Free );
174177}
175178
176179static unsigned long
@@ -880,6 +883,12 @@ rangeiter_setstate(_PyRangeIterObject *r, PyObject *state)
880883 Py_RETURN_NONE ;
881884}
882885
886+ static void
887+ rangeiter_dealloc (PyObject * self )
888+ {
889+ _Py_FREELIST_FREE (range_iters , (_PyRangeIterObject * )self , PyObject_Free );
890+ }
891+
883892PyDoc_STRVAR (reduce_doc , "Return state information for pickling." );
884893PyDoc_STRVAR (setstate_doc , "Set state information for unpickling." );
885894
@@ -899,7 +908,7 @@ PyTypeObject PyRangeIter_Type = {
899908 sizeof (_PyRangeIterObject ), /* tp_basicsize */
900909 0 , /* tp_itemsize */
901910 /* methods */
902- ( destructor ) PyObject_Free , /* tp_dealloc */
911+ rangeiter_dealloc , /* tp_dealloc */
903912 0 , /* tp_vectorcall_offset */
904913 0 , /* tp_getattr */
905914 0 , /* tp_setattr */
@@ -960,9 +969,14 @@ get_len_of_range(long lo, long hi, long step)
960969static PyObject *
961970fast_range_iter (long start , long stop , long step , long len )
962971{
963- _PyRangeIterObject * it = PyObject_New (_PyRangeIterObject , & PyRangeIter_Type );
964- if (it == NULL )
965- return NULL ;
972+ _PyRangeIterObject * it = _Py_FREELIST_POP (_PyRangeIterObject , range_iters );
973+ if (it == NULL ) {
974+ it = PyObject_New (_PyRangeIterObject , & PyRangeIter_Type );
975+ if (it == NULL ) {
976+ return NULL ;
977+ }
978+ }
979+ assert (Py_IS_TYPE (it , & PyRangeIter_Type ));
966980 it -> start = start ;
967981 it -> step = step ;
968982 it -> len = len ;
0 commit comments