@@ -96,65 +96,59 @@ list_preallocate_exact(PyListObject *self, Py_ssize_t size)
9696 return 0 ;
9797}
9898
99- /* Empty list reuse scheme to save calls to malloc and free */
100- #ifndef PyList_MAXFREELIST
101- # define PyList_MAXFREELIST 80
102- #endif
103-
104- /* bpo-40521: list free lists are shared by all interpreters. */
105- #ifdef EXPERIMENTAL_ISOLATED_SUBINTERPRETERS
106- # undef PyList_MAXFREELIST
107- # define PyList_MAXFREELIST 0
108- #endif
109-
110- static PyListObject * free_list [PyList_MAXFREELIST ];
111- static int numfree = 0 ;
112-
11399void
114- _PyList_ClearFreeList (void )
100+ _PyList_ClearFreeList (PyThreadState * tstate )
115101{
116- while (numfree ) {
117- PyListObject * op = free_list [-- numfree ];
102+ struct _Py_list_state * state = & tstate -> interp -> list ;
103+ while (state -> numfree ) {
104+ PyListObject * op = state -> free_list [-- state -> numfree ];
118105 assert (PyList_CheckExact (op ));
119106 PyObject_GC_Del (op );
120107 }
121108}
122109
123110void
124- _PyList_Fini (void )
111+ _PyList_Fini (PyThreadState * tstate )
125112{
126- _PyList_ClearFreeList ();
113+ _PyList_ClearFreeList (tstate );
127114}
128115
129116/* Print summary info about the state of the optimized allocator */
130117void
131118_PyList_DebugMallocStats (FILE * out )
132119{
120+ PyInterpreterState * interp = _PyInterpreterState_GET ();
121+ struct _Py_list_state * state = & interp -> list ;
133122 _PyDebugAllocatorStats (out ,
134123 "free PyListObject" ,
135- numfree , sizeof (PyListObject ));
124+ state -> numfree , sizeof (PyListObject ));
136125}
137126
138127PyObject *
139128PyList_New (Py_ssize_t size )
140129{
141- PyListObject * op ;
142-
143130 if (size < 0 ) {
144131 PyErr_BadInternalCall ();
145132 return NULL ;
146133 }
147- if (numfree ) {
148- numfree -- ;
149- op = free_list [numfree ];
134+
135+ PyInterpreterState * interp = _PyInterpreterState_GET ();
136+ struct _Py_list_state * state = & interp -> list ;
137+ PyListObject * op ;
138+ if (state -> numfree ) {
139+ state -> numfree -- ;
140+ op = state -> free_list [state -> numfree ];
150141 _Py_NewReference ((PyObject * )op );
151- } else {
142+ }
143+ else {
152144 op = PyObject_GC_New (PyListObject , & PyList_Type );
153- if (op == NULL )
145+ if (op == NULL ) {
154146 return NULL ;
147+ }
155148 }
156- if (size <= 0 )
149+ if (size <= 0 ) {
157150 op -> ob_item = NULL ;
151+ }
158152 else {
159153 op -> ob_item = (PyObject * * ) PyMem_Calloc (size , sizeof (PyObject * ));
160154 if (op -> ob_item == NULL ) {
@@ -334,10 +328,14 @@ list_dealloc(PyListObject *op)
334328 }
335329 PyMem_FREE (op -> ob_item );
336330 }
337- if (numfree < PyList_MAXFREELIST && PyList_CheckExact (op ))
338- free_list [numfree ++ ] = op ;
339- else
331+ PyInterpreterState * interp = _PyInterpreterState_GET ();
332+ struct _Py_list_state * state = & interp -> list ;
333+ if (state -> numfree < PyList_MAXFREELIST && PyList_CheckExact (op )) {
334+ state -> free_list [state -> numfree ++ ] = op ;
335+ }
336+ else {
340337 Py_TYPE (op )-> tp_free ((PyObject * )op );
338+ }
341339 Py_TRASHCAN_END
342340}
343341
0 commit comments