@@ -94,58 +94,55 @@ framelocalsproxy_hasval(_PyInterpreterFrame *frame, PyCodeObject *co, int i)
9494 return true;
9595}
9696
97- /* Scan co_localsplusnames once. A repeated name is on the code object
98- whether or not its slot is bound. Returns -1 on error. */
99- static int
100- framelocalsproxy_learn_dedup (PyCodeObject * co )
101- {
102- char dedup = _PYCODE_LOCALSPLUS_DEDUP_UNIQUE ;
103- if (co -> co_nlocalsplus > 1 ) {
104- assert (PyTuple_GET_SIZE (co -> co_localsplusnames ) == co -> co_nlocalsplus );
105- PyObject * names = PySet_New (co -> co_localsplusnames );
106- if (names == NULL ) {
107- return -1 ;
108- }
109- if (PySet_GET_SIZE (names ) != co -> co_nlocalsplus ) {
110- dedup = _PYCODE_LOCALSPLUS_DEDUP_NEEDED ;
111- }
112- Py_DECREF (names );
113- }
114- FT_ATOMIC_STORE_CHAR_RELAXED (co -> _co_localsplus_dedup , dedup );
115- return 0 ;
116- }
117-
11897/* *seen is NULL when dedup is unnecessary. Otherwise it is the set of bound
11998 names already kept. Returns -1 if the set cannot be created. */
12099static int
121100framelocalsproxy_prepare_dedup (PyCodeObject * co , PyObject * * seen )
122101{
123102 * seen = NULL ;
124103 char state = FT_ATOMIC_LOAD_CHAR_RELAXED (co -> _co_localsplus_dedup );
125- if (state == _PYCODE_LOCALSPLUS_DEDUP_UNKNOWN ) {
126- if (framelocalsproxy_learn_dedup (co ) < 0 ) {
127- return -1 ;
128- }
129- state = FT_ATOMIC_LOAD_CHAR_RELAXED (co -> _co_localsplus_dedup );
130- }
131104 if (state == _PYCODE_LOCALSPLUS_DEDUP_UNIQUE ) {
132105 return 0 ;
133106 }
107+ /* A single name cannot be repeated. More names stay UNKNOWN until a
108+ walk sees every slot bound without a repeat. */
109+ if (state == _PYCODE_LOCALSPLUS_DEDUP_UNKNOWN && co -> co_nlocalsplus <= 1 ) {
110+ FT_ATOMIC_STORE_CHAR_RELAXED (co -> _co_localsplus_dedup ,
111+ _PYCODE_LOCALSPLUS_DEDUP_UNIQUE );
112+ return 0 ;
113+ }
134114 * seen = PySet_New (NULL );
135115 if (* seen == NULL ) {
136116 return -1 ;
137117 }
138118 return 0 ;
139119}
140120
121+ /* A walk that saw every slot bound, and no repeated bound name, has shown
122+ that co_localsplusnames are unique. A sparse walk leaves the cache
123+ UNKNOWN: an unbound slot may still repeat a name. */
124+ static void
125+ framelocalsproxy_finish_dedup (PyCodeObject * co , int all_bound )
126+ {
127+ if (all_bound &&
128+ FT_ATOMIC_LOAD_CHAR_RELAXED (co -> _co_localsplus_dedup ) ==
129+ _PYCODE_LOCALSPLUS_DEDUP_UNKNOWN )
130+ {
131+ FT_ATOMIC_STORE_CHAR_RELAXED (co -> _co_localsplus_dedup ,
132+ _PYCODE_LOCALSPLUS_DEDUP_UNIQUE );
133+ }
134+ }
135+
141136/* Returns 1 if this slot's value should be kept, 0 if not, -1 on error.
142- Empty slots are not kept. When dedup is required, the first bound value
143- for a name wins. */
137+ Empty slots are not kept. The first bound value for a name wins.
138+ *all_bound is cleared when a slot is empty. A repeated bound name
139+ stores NEEDED. */
144140static int
145141framelocalsproxy_visit_name (PyCodeObject * co , int i , PyObject * seen ,
146- int has_value )
142+ int has_value , int * all_bound )
147143{
148144 if (!has_value ) {
145+ * all_bound = 0 ;
149146 return 0 ;
150147 }
151148 if (seen == NULL ) {
@@ -157,6 +154,11 @@ framelocalsproxy_visit_name(PyCodeObject *co, int i, PyObject *seen,
157154 return -1 ;
158155 }
159156 if (found ) {
157+ if (FT_ATOMIC_LOAD_CHAR_RELAXED (co -> _co_localsplus_dedup ) ==
158+ _PYCODE_LOCALSPLUS_DEDUP_UNKNOWN ) {
159+ FT_ATOMIC_STORE_CHAR_RELAXED (co -> _co_localsplus_dedup ,
160+ _PYCODE_LOCALSPLUS_DEDUP_NEEDED );
161+ }
160162 return 0 ;
161163 }
162164 if (PySet_Add (seen , name ) < 0 ) {
@@ -458,9 +460,10 @@ framelocalsproxy_keys(PyObject *self, PyObject *Py_UNUSED(ignored))
458460 return NULL ;
459461 }
460462
463+ int all_bound = 1 ;
461464 for (int i = 0 ; i < co -> co_nlocalsplus ; i ++ ) {
462465 int has = framelocalsproxy_hasval (frame -> f_frame , co , i );
463- int emit = framelocalsproxy_visit_name (co , i , seen , has );
466+ int emit = framelocalsproxy_visit_name (co , i , seen , has , & all_bound );
464467 if (emit < 0 ) {
465468 goto error ;
466469 }
@@ -471,7 +474,10 @@ framelocalsproxy_keys(PyObject *self, PyObject *Py_UNUSED(ignored))
471474 }
472475 }
473476 }
474- Py_XDECREF (seen );
477+ if (seen != NULL ) {
478+ framelocalsproxy_finish_dedup (co , all_bound );
479+ Py_DECREF (seen );
480+ }
475481
476482 // Iterate through the extra locals
477483 if (frame -> f_extra_locals ) {
@@ -682,10 +688,11 @@ framelocalsproxy_values(PyObject *self, PyObject *Py_UNUSED(ignored))
682688 return NULL ;
683689 }
684690
691+ int all_bound = 1 ;
685692 for (int i = 0 ; i < co -> co_nlocalsplus ; i ++ ) {
686693 PyObject * value = framelocalsproxy_getval (frame -> f_frame , co , i );
687694 int has = value != NULL ;
688- int emit = framelocalsproxy_visit_name (co , i , seen , has );
695+ int emit = framelocalsproxy_visit_name (co , i , seen , has , & all_bound );
689696 if (emit < 0 ) {
690697 Py_XDECREF (value );
691698 goto error ;
@@ -696,7 +703,10 @@ framelocalsproxy_values(PyObject *self, PyObject *Py_UNUSED(ignored))
696703 }
697704 Py_XDECREF (value );
698705 }
699- Py_XDECREF (seen );
706+ if (seen != NULL ) {
707+ framelocalsproxy_finish_dedup (co , all_bound );
708+ Py_DECREF (seen );
709+ }
700710
701711 // Iterate through the extra locals
702712 if (frame -> f_extra_locals ) {
@@ -734,10 +744,11 @@ framelocalsproxy_items(PyObject *self, PyObject *Py_UNUSED(ignored))
734744 return NULL ;
735745 }
736746
747+ int all_bound = 1 ;
737748 for (int i = 0 ; i < co -> co_nlocalsplus ; i ++ ) {
738749 PyObject * value = framelocalsproxy_getval (frame -> f_frame , co , i );
739750 int has = value != NULL ;
740- int emit = framelocalsproxy_visit_name (co , i , seen , has );
751+ int emit = framelocalsproxy_visit_name (co , i , seen , has , & all_bound );
741752 if (emit < 0 ) {
742753 Py_XDECREF (value );
743754 goto error ;
@@ -755,8 +766,11 @@ framelocalsproxy_items(PyObject *self, PyObject *Py_UNUSED(ignored))
755766 goto error ;
756767 }
757768 }
758- Py_XDECREF (seen );
759- seen = NULL ;
769+ if (seen != NULL ) {
770+ framelocalsproxy_finish_dedup (co , all_bound );
771+ Py_DECREF (seen );
772+ seen = NULL ;
773+ }
760774
761775 // Iterate through the extra locals
762776 if (frame -> f_extra_locals ) {
@@ -799,9 +813,10 @@ framelocalsproxy_length(PyObject *self)
799813 if (framelocalsproxy_prepare_dedup (co , & seen ) < 0 ) {
800814 return -1 ;
801815 }
816+ int all_bound = 1 ;
802817 for (int i = 0 ; i < co -> co_nlocalsplus ; i ++ ) {
803818 int has = framelocalsproxy_hasval (frame -> f_frame , co , i );
804- int emit = framelocalsproxy_visit_name (co , i , seen , has );
819+ int emit = framelocalsproxy_visit_name (co , i , seen , has , & all_bound );
805820 if (emit < 0 ) {
806821 Py_XDECREF (seen );
807822 return -1 ;
@@ -810,7 +825,10 @@ framelocalsproxy_length(PyObject *self)
810825 size ++ ;
811826 }
812827 }
813- Py_XDECREF (seen );
828+ if (seen != NULL ) {
829+ framelocalsproxy_finish_dedup (co , all_bound );
830+ Py_DECREF (seen );
831+ }
814832 return size ;
815833}
816834
0 commit comments