Skip to content

Commit 90532ce

Browse files
gh-135385: Do not reserve inline values for attributes stored in slots
__static_attributes__ contains every name assigned as self.X in the class body, and all of them were inserted into the shared keys of the class. An attribute stored in a slot is never stored in the instance dict, so the inline values reserved for it on every instance were never used.
1 parent ae93e42 commit 90532ce

2 files changed

Lines changed: 8 additions & 0 deletions

File tree

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
Reduce the memory consumption of instances of classes which define
2+
``__slots__`` and also have a ``__dict__``. Space for the attributes stored
3+
in slots is no longer reserved in the inline values of every instance.

Objects/dictobject.c

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7286,6 +7286,11 @@ _PyDict_NewKeysForClass(PyHeapTypeObject *cls)
72867286
PyObject *key = PyTuple_GET_ITEM(attrs, i);
72877287
Py_hash_t hash;
72887288
if (PyUnicode_CheckExact(key) && (hash = unicode_get_hash(key)) != -1) {
7289+
/* An attribute stored in a slot never uses the dict. */
7290+
PyObject *descr = _PyType_Lookup((PyTypeObject *)cls, key);
7291+
if (descr != NULL && Py_IS_TYPE(descr, &PyMemberDescr_Type)) {
7292+
continue;
7293+
}
72897294
if (insert_split_key(keys, key, hash) == DKIX_EMPTY) {
72907295
break;
72917296
}

0 commit comments

Comments
 (0)