Skip to content

Commit cb971c0

Browse files
committed
separate scan to decide dedup from the names
1 parent 4caa222 commit cb971c0

4 files changed

Lines changed: 66 additions & 76 deletions

File tree

‎Include/cpython/code.h‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -108,8 +108,8 @@ typedef struct {
108108
_PyCode_DEF_UNIQUE_ID() \
109109
int _co_firsttraceable; /* index of first traceable instruction */ \
110110
/* 0 = not yet known, 1 = co_localsplusnames are unique, 2 = a name is \
111-
repeated. FrameLocalsProxy learns this from the seen map it builds \
112-
while collecting locals, and skips deduplication when this is 1. */ \
111+
repeated. FrameLocalsProxy scans the names once and skips \
112+
deduplication when this is 1. */ \
113113
char _co_localsplus_dedup; \
114114
/* Scratch space for extra data relating to the code object. \
115115
Type is a void* to keep the format private in codeobject.c to force \

‎Include/internal/pycore_code.h‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -273,7 +273,7 @@ extern PyObject* _PyCode_GetCellvars(PyCodeObject *);
273273
extern PyObject* _PyCode_GetFreevars(PyCodeObject *);
274274
extern PyObject* _PyCode_GetCode(PyCodeObject *);
275275

276-
/* co->_co_localsplus_dedup. Learned by FrameLocalsProxy from its seen map. */
276+
/* co->_co_localsplus_dedup. Computed once from co_localsplusnames. */
277277
#define _PYCODE_LOCALSPLUS_DEDUP_UNKNOWN 0
278278
#define _PYCODE_LOCALSPLUS_DEDUP_UNIQUE 1
279279
#define _PYCODE_LOCALSPLUS_DEDUP_NEEDED 2

‎Lib/test/test_listcomps.py‎

Lines changed: 15 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -944,32 +944,33 @@ def inner():
944944
{"snaps": [1, 2], "vals": [2, 2], "consistent": [True, True]},
945945
ns={"sys": sys}, scopes=["module", "function"])
946946

947-
def test_frame_locals_dedup_learned_before_cell_is_bound(self):
948-
# Reading f_locals before the comprehension cell is bound must not
947+
def test_frame_locals_dedup_learned_before_slot_is_bound(self):
948+
# Reading f_locals before the comprehension slot is bound must not
949949
# cache "names are unique": the repeated name is still on the code
950950
# object, and dict(**f_locals) inside the comprehension has to work.
951-
# Each API initializes the cache on its own code object. list(proxy)
952-
# goes through keys() first, so it does not cover the others.
951+
# The enclosing x is already bound, so the first read shows that
952+
# value. Each API runs on its own code object.
953953
probes = {
954-
"keys": "list(sys._getframe().f_locals.keys())",
955-
"values": "list(sys._getframe().f_locals.values())",
956-
"items": "list(sys._getframe().f_locals.items())",
957-
"len": "len(sys._getframe().f_locals)",
954+
"keys": ("list(sys._getframe().f_locals.keys())", ["x"]),
955+
"values": ("list(sys._getframe().f_locals.values())", [[7, 8]]),
956+
"items": ("list(sys._getframe().f_locals.items())",
957+
[("x", [7, 8])]),
958+
"len": ("len(sys._getframe().f_locals)", 1),
958959
}
959960
import sys
960-
for name, probe in probes.items():
961+
for name, (probe, expected) in probes.items():
961962
with self.subTest(probe=name):
962963
code = f"""
963964
def outer(x):
964965
def inner():
965-
{probe}
966-
return [dict(**sys._getframe().f_locals)['x']
967-
for x in x]
966+
probe = {probe}
967+
return probe, [dict(**sys._getframe().f_locals)['x']
968+
for x in x]
968969
return inner()
969-
result = outer([7, 8])
970+
probe, result = outer([7, 8])
970971
"""
971972
self._check_in_scopes(
972-
code, {"result": [7, 8]},
973+
code, {"probe": expected, "result": [7, 8]},
973974
ns={"sys": sys}, scopes=["module", "function"])
974975

975976
def test_frame_locals_nested_comp_cell_and_enclosing_free(self):

‎Objects/frameobject.c‎

Lines changed: 48 additions & 59 deletions
Original file line numberDiff line numberDiff line change
@@ -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. */
103120
static int
104121
framelocalsproxy_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. */
122144
static int
123145
framelocalsproxy_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

158168
static 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

Comments
 (0)