Skip to content

Commit 74ed861

Browse files
committed
scan only bound names. Unknown until all are bound.
1 parent cb971c0 commit 74ed861

3 files changed

Lines changed: 61 additions & 42 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 scans the names once and skips \
112-
deduplication when this is 1. */ \
111+
repeated. Learned from bound locals; deduplication is skipped when \
112+
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: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -273,7 +273,8 @@ extern PyObject* _PyCode_GetCellvars(PyCodeObject *);
273273
extern PyObject* _PyCode_GetFreevars(PyCodeObject *);
274274
extern PyObject* _PyCode_GetCode(PyCodeObject *);
275275

276-
/* co->_co_localsplus_dedup. Computed once from co_localsplusnames. */
276+
/* co->_co_localsplus_dedup. Learned from bound locals. UNIQUE is stored
277+
only after a walk sees every slot bound without a repeated name. */
277278
#define _PYCODE_LOCALSPLUS_DEDUP_UNKNOWN 0
278279
#define _PYCODE_LOCALSPLUS_DEDUP_UNIQUE 1
279280
#define _PYCODE_LOCALSPLUS_DEDUP_NEEDED 2

‎Objects/frameobject.c‎

Lines changed: 57 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -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. */
12099
static int
121100
framelocalsproxy_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. */
144140
static int
145141
framelocalsproxy_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

Comments
 (0)