gh-135385: Do not reserve inline values for attributes stored in slots - #156656
Open
serhiy-storchaka wants to merge 1 commit into
Open
gh-135385: Do not reserve inline values for attributes stored in slots#156656serhiy-storchaka wants to merge 1 commit into
serhiy-storchaka wants to merge 1 commit into
Conversation
…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.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
__static_attributes__contains every name assigned asself.Xin 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:
An
xml.dom.minidomelement 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.
__slots__and__dict__have much larger size than needed (up to 4x) on Python 3.13 #135385