diff --git a/Misc/NEWS.d/next/Core_and_Builtins/2026-08-30-16-30-00.gh-issue-135385.Wn7Rd4.rst b/Misc/NEWS.d/next/Core_and_Builtins/2026-08-30-16-30-00.gh-issue-135385.Wn7Rd4.rst new file mode 100644 index 000000000000000..43b72921fe9e45c --- /dev/null +++ b/Misc/NEWS.d/next/Core_and_Builtins/2026-08-30-16-30-00.gh-issue-135385.Wn7Rd4.rst @@ -0,0 +1,3 @@ +Reduce the memory consumption of instances of classes which define +``__slots__`` and also have a ``__dict__``. Space for the attributes stored +in slots is no longer reserved in the inline values of every instance. diff --git a/Objects/dictobject.c b/Objects/dictobject.c index 74b6d5d779a064c..cc75aa83e0b06a5 100644 --- a/Objects/dictobject.c +++ b/Objects/dictobject.c @@ -7286,6 +7286,11 @@ _PyDict_NewKeysForClass(PyHeapTypeObject *cls) PyObject *key = PyTuple_GET_ITEM(attrs, i); Py_hash_t hash; if (PyUnicode_CheckExact(key) && (hash = unicode_get_hash(key)) != -1) { + /* An attribute stored in a slot never uses the dict. */ + PyObject *descr = _PyType_Lookup((PyTypeObject *)cls, key); + if (descr != NULL && Py_IS_TYPE(descr, &PyMemberDescr_Type)) { + continue; + } if (insert_split_key(keys, key, hash) == DKIX_EMPTY) { break; }