From 90532ce2b68facca5d550e20916a0cb407d4d328 Mon Sep 17 00:00:00 2001 From: Serhiy Storchaka Date: Sun, 30 Aug 2026 15:21:42 +0300 Subject: [PATCH] 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. --- .../2026-08-30-16-30-00.gh-issue-135385.Wn7Rd4.rst | 3 +++ Objects/dictobject.c | 5 +++++ 2 files changed, 8 insertions(+) create mode 100644 Misc/NEWS.d/next/Core_and_Builtins/2026-08-30-16-30-00.gh-issue-135385.Wn7Rd4.rst 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; }