Repository navigation
Objects with both __slots__ and __dict__ have much larger size than needed (up to 4x) on Python 3.13 #135385
Description
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).
import tracemalloc
import gc
class _Point2D:
__slots__ = ("x", "y")
class Point3D_OnlySlots(_Point2D):
__slots__ = ("z",)
def __init__(self, x, y, z):
self.x, self.y, self.z = x, y, z
class Point3D_DictAndSlots(_Point2D):
def __init__(self, x, y, z):
self.x, self.y, self.z = x, y, z
class Point3D_OnlyDict:
def __init__(self, x, y, z):
self.x, self.y, self.z = x, y, z
gc.collect() # clear freelists
tracemalloc.start()
_ = [Point3D_OnlySlots(1, 2, 3) for _ in range(1_000_000)]
print(
f"1M Point3D (only __slots__) instances: {tracemalloc.get_traced_memory()[0]:_} bytes"
)
gc.collect() # clear freelists
tracemalloc.start()
_ = [Point3D_OnlyDict(1, 2, 3) for _ in range(1_000_000)]
print(
f"1M Point3D (only __dict__) instances: {tracemalloc.get_traced_memory()[0]:_} bytes"
)
gc.collect() # clear freelists
tracemalloc.start()
_ = [Point3D_DictAndSlots(1, 2, 3) for _ in range(1_000_000)]
print(
f"1M Point3D (__dict__ and __slots__) instances: {tracemalloc.get_traced_memory()[0]:_} bytes"
)On python 3.13.3, this prints:
1M Point3D (only __slots__) instances: 64_448_792 bytes
1M Point3D (only __dict__) instances: 104_451_928 bytes
1M Point3D (__dict__ and __slots__) instances: 416_448_848 bytes
On python 3.12.11, this prints:
1M Point3D (only __slots__) instances: 64_448_792 bytes
1M Point3D (only __dict__) instances: 96_451_808 bytes
1M Point3D (__dict__ and __slots__) instances: 96_452_232 bytes
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__ will
trigger __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?
CPython versions tested on:
3.13
Operating systems tested on:
macOS
Activity
[-]Objects with `__slots__` and `__dict__` have much larger size than needed (up to 4x) on Python 3.13[/-][+]Objects with both `__slots__` and `__dict__` have much larger size than needed (up to 4x) on Python 3.13[/+]Eclips4 commentedon Jun 11, 2025
Hello! Thanks for the report.
Results on current main:
ariebovenberg commentedon Jun 11, 2025
Ah, silly of me not to check the current
mainas well. Is this something that will be backported into 3.13?shuimu5418 commentedon Jun 11, 2025
File:Objects/typeobject.cFunc:type_ready_managed_dict()Lines:9060-9062Cond:if (type->tp_itemsize == 0)EDIT: Apologies, I posted my analysis before seeing Eclips4's comment above. I have since confirmed locally by compiling and testing the
mainbranch that this issue is indeed fixed there.It is too difficult for me. But I think i can link these issues:
pythongh-135385: Fix memory regression for classes with both __slots_…
pythongh-135385: Fix memory regression for classes with both __slots_…
serhiy-storchaka commentedon Aug 30, 2026
The 3.13 regression is fixed, but such instances still reserve inline values for names which are stored in slots.
__static_attributes__contains every name assigned asself.Xin the class body, and_PyDict_NewKeysForClass()inserts all of them into the shared keys, even whenXis a slot. A member descriptor is a data descriptor, so such an attribute is never stored in the instance dict, and the reserved space is never used.Skipping them in
_PyDict_NewKeysForClass()(a_PyType_Lookup()per name, at class creation only) gives for the example above:xml.dom.minidom, where all 11 names inElement.__static_attributes__are slots, goes from 328 to 232 bytes per node. The whole test suite passes and I see no difference in benchmarks.