Skip to content

Commit dedffa7

Browse files
committed
Use __static_attributes__ to initialize shared keys
1 parent 21c09d9 commit dedffa7

File tree

3 files changed

+16
-3
lines changed

3 files changed

+16
-3
lines changed

Include/internal/pycore_dict.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -81,7 +81,7 @@ typedef struct {
8181
PyObject *me_value; /* This field is only meaningful for combined tables */
8282
} PyDictUnicodeEntry;
8383

84-
extern PyDictKeysObject *_PyDict_NewKeysForClass(void);
84+
extern PyDictKeysObject *_PyDict_NewKeysForClass(PyHeapTypeObject *);
8585
extern PyObject *_PyDict_FromKeys(PyObject *, PyObject *, PyObject *);
8686

8787
/* Gets a version number unique to the current state of the keys of dict, if possible.

Objects/dictobject.c

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6586,9 +6586,10 @@ dictvalues_reversed(PyObject *self, PyObject *Py_UNUSED(ignored))
65866586
/* Returns NULL if cannot allocate a new PyDictKeysObject,
65876587
but does not set an error */
65886588
PyDictKeysObject *
6589-
_PyDict_NewKeysForClass(void)
6589+
_PyDict_NewKeysForClass(PyHeapTypeObject *cls)
65906590
{
65916591
PyInterpreterState *interp = _PyInterpreterState_GET();
6592+
65926593
PyDictKeysObject *keys = new_keys_object(
65936594
interp, NEXT_LOG2_SHARED_KEYS_MAX_SIZE, 1);
65946595
if (keys == NULL) {
@@ -6600,6 +6601,18 @@ _PyDict_NewKeysForClass(void)
66006601
keys->dk_usable = SHARED_KEYS_MAX_SIZE;
66016602
keys->dk_kind = DICT_KEYS_SPLIT;
66026603
}
6604+
if (cls->ht_type.tp_dict) {
6605+
PyObject *attrs = PyDict_GetItem(cls->ht_type.tp_dict, &_Py_ID(__static_attributes__));
6606+
if (attrs != NULL && PyTuple_Check(attrs)) {
6607+
for (Py_ssize_t i; i < PyTuple_GET_SIZE(attrs); i++) {
6608+
PyObject *key = PyTuple_GET_ITEM(attrs, i);
6609+
Py_hash_t hash;
6610+
if (PyUnicode_CheckExact(key) && (hash = unicode_get_hash(key)) != -1) {
6611+
insert_split_key(keys, key, hash);
6612+
}
6613+
}
6614+
}
6615+
}
66036616
return keys;
66046617
}
66056618

Objects/typeobject.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7883,7 +7883,7 @@ type_ready_managed_dict(PyTypeObject *type)
78837883
}
78847884
PyHeapTypeObject* et = (PyHeapTypeObject*)type;
78857885
if (et->ht_cached_keys == NULL) {
7886-
et->ht_cached_keys = _PyDict_NewKeysForClass();
7886+
et->ht_cached_keys = _PyDict_NewKeysForClass(et);
78877887
if (et->ht_cached_keys == NULL) {
78887888
PyErr_NoMemory();
78897889
return -1;

0 commit comments

Comments
 (0)