Objects with both __slots__
and __dict__
have much larger size than needed (up to 4x) on Python 3.13
#135385
Labels
3.13
bugs and security fixes
3.14
bugs and security fixes
3.15
new features, bugs and security fixes
interpreter-core
(Objects, Python, Grammar, and Parser dirs)
performance
Performance or resource usage
type-bug
An unexpected behavior, bug, or error
Uh oh!
There was an error while loading. Please reload this page.
Bug report
Bug description:
On Python 3.13, instances that have both
__slots__
and__dict__
defined (e.g. through inheritance)can have a size that is more than four times of that in previous Python versions (e.g. 3.12).
On python 3.13.3, this prints:
On python 3.12.11, this prints:
The apparent cause seems to be the object layout changes (see here)
which introduced inline values. It appears that these don't mesh well with
__slots__
.Could this be because the inline values need to have a fixed offset?
What appears to cause the dramatic increase above is that having (nonempty)
__slots__
willtrigger
__dict__
materialization (size 30) as soon as a non-slot attribute is set. In contrast to the optimized "no slots" case, this dict doesn't shrink as more instances are created.Of course, mixing
__slots__
and__dict__
is not recommended, but it's a trap that can be easily fallen into.For example, if forgetting to define
__slots__
anywhere in a complex class hierarchy.I'm unsure if I'm understanding exactly what's going on though. @markshannon perhaps you can shed some light on this?
Related: #115776, #115822
CPython versions tested on:
3.13
Operating systems tested on:
macOS
Linked PRs
The text was updated successfully, but these errors were encountered: