Skip to content

gh-135385: Do not reserve inline values for attributes stored in slots - #156656

Open
serhiy-storchaka wants to merge 1 commit into
python:mainfrom
serhiy-storchaka:gh-135385-slots-inline-values
Open

gh-135385: Do not reserve inline values for attributes stored in slots#156656
serhiy-storchaka wants to merge 1 commit into
python:mainfrom
serhiy-storchaka:gh-135385-slots-inline-values

Conversation

@serhiy-storchaka

@serhiy-storchaka serhiy-storchaka commented Aug 30, 2026

Copy link
Copy Markdown
Member

__static_attributes__ contains every name assigned as self.X in the class body, and _PyDict_NewKeysForClass() inserted all of them 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.

For the example in the issue:

only __slots__              64_449_113 bytes
only __dict__              104_452_257 bytes
__dict__ and __slots__     104_452_697 bytes   (120_452_257 before)

An xml.dom.minidom element node, where all 11 names in __static_attributes__ are slots, goes from 328 to 232 bytes.

The lookup is done once per attribute name at class creation, and I see no difference in benchmarks, including creating 100000 classes.

…n 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.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant