@@ -94,65 +94,75 @@ framelocalsproxy_hasval(_PyInterpreterFrame *frame, PyCodeObject *co, int i)
9494 return true;
9595}
9696
97- /* *seen is NULL when dedup is known to be unnecessary.
98- Otherwise it maps each localsplus name to Py_True if a value was already
99- kept, or Py_False if the name was only seen on an empty slot. An empty
100- slot must not count as the kept value, but it still proves the name is
101- repeated, so it is recorded while _co_localsplus_dedup is unknown.
102- Returns -1 if the map cannot be created. */
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+
118+ /* *seen is NULL when dedup is unnecessary. Otherwise it is the set of bound
119+ names already kept. Returns -1 if the set cannot be created. */
103120static int
104121framelocalsproxy_prepare_dedup (PyCodeObject * co , PyObject * * seen )
105122{
106123 * seen = NULL ;
107- if (FT_ATOMIC_LOAD_CHAR_RELAXED (co -> _co_localsplus_dedup ) ==
108- _PYCODE_LOCALSPLUS_DEDUP_UNIQUE ) {
124+ 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+ }
131+ if (state == _PYCODE_LOCALSPLUS_DEDUP_UNIQUE ) {
109132 return 0 ;
110133 }
111- * seen = PyDict_New ( );
134+ * seen = PySet_New ( NULL );
112135 if (* seen == NULL ) {
113136 return -1 ;
114137 }
115138 return 0 ;
116139}
117140
118141/* Returns 1 if this slot's value should be kept, 0 if not, -1 on error.
119- An empty slot is skipped once _co_localsplus_dedup is known. A repeated
120- name stores _PYCODE_LOCALSPLUS_DEDUP_NEEDED. UNIQUE is stored by the
121- caller only after the walk finishes without one. */
142+ Empty slots are not kept. When dedup is required, the first bound value
143+ for a name wins. */
122144static int
123145framelocalsproxy_visit_name (PyCodeObject * co , int i , PyObject * seen ,
124146 int has_value )
125147{
126- if (seen == NULL ) {
127- return has_value ;
128- }
129- if (!has_value &&
130- FT_ATOMIC_LOAD_CHAR_RELAXED (co -> _co_localsplus_dedup ) !=
131- _PYCODE_LOCALSPLUS_DEDUP_UNKNOWN ) {
148+ if (!has_value ) {
132149 return 0 ;
133150 }
151+ if (seen == NULL ) {
152+ return 1 ;
153+ }
134154 PyObject * name = PyTuple_GET_ITEM (co -> co_localsplusnames , i );
135- PyObject * prev = PyDict_GetItemWithError (seen , name );
136- if (prev == NULL ) {
137- if (PyErr_Occurred ()) {
138- return -1 ;
139- }
140- if (PyDict_SetItem (seen , name , has_value ? Py_True : Py_False ) < 0 ) {
141- return -1 ;
142- }
143- return has_value ;
155+ int found = PySet_Contains (seen , name );
156+ if (found < 0 ) {
157+ return -1 ;
144158 }
145- else {
146- FT_ATOMIC_STORE_CHAR_RELAXED (co -> _co_localsplus_dedup ,
147- _PYCODE_LOCALSPLUS_DEDUP_NEEDED );
148- if (!has_value || prev == Py_True ) {
149- return 0 ;
150- }
151- if (PyDict_SetItem (seen , name , Py_True ) < 0 ) {
152- return -1 ;
153- }
154- return 1 ;
159+ if (found ) {
160+ return 0 ;
155161 }
162+ if (PySet_Add (seen , name ) < 0 ) {
163+ return -1 ;
164+ }
165+ return 1 ;
156166}
157167
158168static int
@@ -441,8 +451,7 @@ framelocalsproxy_keys(PyObject *self, PyObject *Py_UNUSED(ignored))
441451 if (names == NULL ) {
442452 return NULL ;
443453 }
444- // An inlined comprehension cell can share a name with a free var.
445- // Assume dedup may be needed until a walk of seen shows otherwise.
454+ // An inlined comprehension local can share a name with a free var.
446455 PyObject * seen = NULL ;
447456 if (framelocalsproxy_prepare_dedup (co , & seen ) < 0 ) {
448457 Py_DECREF (names );
@@ -462,11 +471,6 @@ framelocalsproxy_keys(PyObject *self, PyObject *Py_UNUSED(ignored))
462471 }
463472 }
464473 }
465- if (FT_ATOMIC_LOAD_CHAR_RELAXED (co -> _co_localsplus_dedup ) ==
466- _PYCODE_LOCALSPLUS_DEDUP_UNKNOWN ) {
467- FT_ATOMIC_STORE_CHAR_RELAXED (co -> _co_localsplus_dedup ,
468- _PYCODE_LOCALSPLUS_DEDUP_UNIQUE );
469- }
470474 Py_XDECREF (seen );
471475
472476 // Iterate through the extra locals
@@ -692,11 +696,6 @@ framelocalsproxy_values(PyObject *self, PyObject *Py_UNUSED(ignored))
692696 }
693697 Py_XDECREF (value );
694698 }
695- if (FT_ATOMIC_LOAD_CHAR_RELAXED (co -> _co_localsplus_dedup ) ==
696- _PYCODE_LOCALSPLUS_DEDUP_UNKNOWN ) {
697- FT_ATOMIC_STORE_CHAR_RELAXED (co -> _co_localsplus_dedup ,
698- _PYCODE_LOCALSPLUS_DEDUP_UNIQUE );
699- }
700699 Py_XDECREF (seen );
701700
702701 // Iterate through the extra locals
@@ -756,11 +755,6 @@ framelocalsproxy_items(PyObject *self, PyObject *Py_UNUSED(ignored))
756755 goto error ;
757756 }
758757 }
759- if (FT_ATOMIC_LOAD_CHAR_RELAXED (co -> _co_localsplus_dedup ) ==
760- _PYCODE_LOCALSPLUS_DEDUP_UNKNOWN ) {
761- FT_ATOMIC_STORE_CHAR_RELAXED (co -> _co_localsplus_dedup ,
762- _PYCODE_LOCALSPLUS_DEDUP_UNIQUE );
763- }
764758 Py_XDECREF (seen );
765759 seen = NULL ;
766760
@@ -816,11 +810,6 @@ framelocalsproxy_length(PyObject *self)
816810 size ++ ;
817811 }
818812 }
819- if (FT_ATOMIC_LOAD_CHAR_RELAXED (co -> _co_localsplus_dedup ) ==
820- _PYCODE_LOCALSPLUS_DEDUP_UNKNOWN ) {
821- FT_ATOMIC_STORE_CHAR_RELAXED (co -> _co_localsplus_dedup ,
822- _PYCODE_LOCALSPLUS_DEDUP_UNIQUE );
823- }
824813 Py_XDECREF (seen );
825814 return size ;
826815}
0 commit comments