@@ -561,36 +561,25 @@ static PyGetSetDef frame_getsetlist[] = {
561561/* max value for numfree */
562562#define PyFrame_MAXFREELIST 200
563563
564- /* bpo-40521: frame free lists are shared by all interpreters. */
565- #ifdef EXPERIMENTAL_ISOLATED_SUBINTERPRETERS
566- # undef PyFrame_MAXFREELIST
567- # define PyFrame_MAXFREELIST 0
568- #endif
569-
570- #if PyFrame_MAXFREELIST > 0
571- static PyFrameObject * free_list = NULL ;
572- static int numfree = 0 ; /* number of frames currently in free_list */
573- #endif
574-
575564static void _Py_HOT_FUNCTION
576565frame_dealloc (PyFrameObject * f )
577566{
578- PyObject * * p , * * valuestack ;
579- PyCodeObject * co ;
580-
581- if (_PyObject_GC_IS_TRACKED (f ))
567+ if (_PyObject_GC_IS_TRACKED (f )) {
582568 _PyObject_GC_UNTRACK (f );
569+ }
583570
584571 Py_TRASHCAN_SAFE_BEGIN (f )
585572 /* Kill all local variables */
586- valuestack = f -> f_valuestack ;
587- for (p = f -> f_localsplus ; p < valuestack ; p ++ )
573+ PyObject * * valuestack = f -> f_valuestack ;
574+ for (PyObject * * p = f -> f_localsplus ; p < valuestack ; p ++ ) {
588575 Py_CLEAR (* p );
576+ }
589577
590578 /* Free stack */
591579 if (f -> f_stacktop != NULL ) {
592- for (p = valuestack ; p < f -> f_stacktop ; p ++ )
580+ for (PyObject * * p = valuestack ; p < f -> f_stacktop ; p ++ ) {
593581 Py_XDECREF (* p );
582+ }
594583 }
595584
596585 Py_XDECREF (f -> f_back );
@@ -599,19 +588,21 @@ frame_dealloc(PyFrameObject *f)
599588 Py_CLEAR (f -> f_locals );
600589 Py_CLEAR (f -> f_trace );
601590
602- co = f -> f_code ;
591+ PyCodeObject * co = f -> f_code ;
603592 if (co -> co_zombieframe == NULL ) {
604593 co -> co_zombieframe = f ;
605594 }
606- #if PyFrame_MAXFREELIST > 0
607- else if (numfree < PyFrame_MAXFREELIST ) {
608- ++ numfree ;
609- f -> f_back = free_list ;
610- free_list = f ;
611- }
612- #endif
613595 else {
614- PyObject_GC_Del (f );
596+ PyInterpreterState * interp = _PyInterpreterState_GET ();
597+ struct _Py_frame_state * state = & interp -> frame ;
598+ if (state -> numfree < PyFrame_MAXFREELIST ) {
599+ ++ state -> numfree ;
600+ f -> f_back = state -> free_list ;
601+ state -> free_list = f ;
602+ }
603+ else {
604+ PyObject_GC_Del (f );
605+ }
615606 }
616607
617608 Py_DECREF (co );
@@ -789,21 +780,20 @@ frame_alloc(PyCodeObject *code)
789780 Py_ssize_t ncells = PyTuple_GET_SIZE (code -> co_cellvars );
790781 Py_ssize_t nfrees = PyTuple_GET_SIZE (code -> co_freevars );
791782 Py_ssize_t extras = code -> co_stacksize + code -> co_nlocals + ncells + nfrees ;
792- #if PyFrame_MAXFREELIST > 0
793- if ( free_list == NULL )
794- #endif
783+ PyInterpreterState * interp = _PyInterpreterState_GET ();
784+ struct _Py_frame_state * state = & interp -> frame ;
785+ if ( state -> free_list == NULL )
795786 {
796787 f = PyObject_GC_NewVar (PyFrameObject , & PyFrame_Type , extras );
797788 if (f == NULL ) {
798789 return NULL ;
799790 }
800791 }
801- #if PyFrame_MAXFREELIST > 0
802792 else {
803- assert (numfree > 0 );
804- -- numfree ;
805- f = free_list ;
806- free_list = free_list -> f_back ;
793+ assert (state -> numfree > 0 );
794+ -- state -> numfree ;
795+ f = state -> free_list ;
796+ state -> free_list = state -> free_list -> f_back ;
807797 if (Py_SIZE (f ) < extras ) {
808798 PyFrameObject * new_f = PyObject_GC_Resize (PyFrameObject , f , extras );
809799 if (new_f == NULL ) {
@@ -814,7 +804,6 @@ frame_alloc(PyCodeObject *code)
814804 }
815805 _Py_NewReference ((PyObject * )f );
816806 }
817- #endif
818807
819808 f -> f_code = code ;
820809 extras = code -> co_nlocals + ncells + nfrees ;
@@ -1183,34 +1172,33 @@ PyFrame_LocalsToFast(PyFrameObject *f, int clear)
11831172
11841173/* Clear out the free list */
11851174void
1186- _PyFrame_ClearFreeList (void )
1175+ _PyFrame_ClearFreeList (PyThreadState * tstate )
11871176{
1188- #if PyFrame_MAXFREELIST > 0
1189- while (free_list != NULL ) {
1190- PyFrameObject * f = free_list ;
1191- free_list = free_list -> f_back ;
1177+ struct _Py_frame_state * state = & tstate -> interp -> frame ;
1178+ while (state -> free_list != NULL ) {
1179+ PyFrameObject * f = state -> free_list ;
1180+ state -> free_list = state -> free_list -> f_back ;
11921181 PyObject_GC_Del (f );
1193- -- numfree ;
1182+ -- state -> numfree ;
11941183 }
1195- assert (numfree == 0 );
1196- #endif
1184+ assert (state -> numfree == 0 );
11971185}
11981186
11991187void
1200- _PyFrame_Fini (void )
1188+ _PyFrame_Fini (PyThreadState * tstate )
12011189{
1202- _PyFrame_ClearFreeList ();
1190+ _PyFrame_ClearFreeList (tstate );
12031191}
12041192
12051193/* Print summary info about the state of the optimized allocator */
12061194void
12071195_PyFrame_DebugMallocStats (FILE * out )
12081196{
1209- #if PyFrame_MAXFREELIST > 0
1197+ PyInterpreterState * interp = _PyInterpreterState_GET ();
1198+ struct _Py_frame_state * state = & interp -> frame ;
12101199 _PyDebugAllocatorStats (out ,
12111200 "free PyFrameObject" ,
1212- numfree , sizeof (PyFrameObject ));
1213- #endif
1201+ state -> numfree , sizeof (PyFrameObject ));
12141202}
12151203
12161204
0 commit comments