3
3
#include "Python.h"
4
4
#include "pycore_abstract.h" // _PyIndex_Check()
5
5
#include "pycore_ceval.h" // _PyEval_GetBuiltin()
6
+ #include "pycore_freelist.h"
6
7
#include "pycore_long.h" // _PyLong_GetZero()
7
8
#include "pycore_modsupport.h" // _PyArg_NoKwnames()
8
9
#include "pycore_range.h"
@@ -51,16 +52,18 @@ static rangeobject *
51
52
make_range_object (PyTypeObject * type , PyObject * start ,
52
53
PyObject * stop , PyObject * step )
53
54
{
54
- rangeobject * obj = NULL ;
55
55
PyObject * length ;
56
56
length = compute_range_length (start , stop , step );
57
57
if (length == NULL ) {
58
58
return NULL ;
59
59
}
60
- obj = PyObject_New (rangeobject , type );
60
+ rangeobject * obj = _Py_FREELIST_POP (rangeobject , ranges );
61
61
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
+ }
64
67
}
65
68
obj -> start = start ;
66
69
obj -> stop = stop ;
@@ -171,7 +174,7 @@ range_dealloc(PyObject *op)
171
174
Py_DECREF (r -> stop );
172
175
Py_DECREF (r -> step );
173
176
Py_DECREF (r -> length );
174
- PyObject_Free ( r );
177
+ _Py_FREELIST_FREE ( ranges , r , PyObject_Free );
175
178
}
176
179
177
180
static unsigned long
@@ -895,6 +898,12 @@ rangeiter_setstate(PyObject *op, PyObject *state)
895
898
Py_RETURN_NONE ;
896
899
}
897
900
901
+ static void
902
+ rangeiter_dealloc (PyObject * self )
903
+ {
904
+ _Py_FREELIST_FREE (range_iters , (_PyRangeIterObject * )self , PyObject_Free );
905
+ }
906
+
898
907
PyDoc_STRVAR (reduce_doc , "Return state information for pickling." );
899
908
PyDoc_STRVAR (setstate_doc , "Set state information for unpickling." );
900
909
@@ -911,7 +920,7 @@ PyTypeObject PyRangeIter_Type = {
911
920
sizeof (_PyRangeIterObject ), /* tp_basicsize */
912
921
0 , /* tp_itemsize */
913
922
/* methods */
914
- 0 , /* tp_dealloc */
923
+ rangeiter_dealloc , /* tp_dealloc */
915
924
0 , /* tp_vectorcall_offset */
916
925
0 , /* tp_getattr */
917
926
0 , /* tp_setattr */
@@ -972,9 +981,14 @@ get_len_of_range(long lo, long hi, long step)
972
981
static PyObject *
973
982
fast_range_iter (long start , long stop , long step , long len )
974
983
{
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 ));
978
992
it -> start = start ;
979
993
it -> step = step ;
980
994
it -> len = len ;
0 commit comments