You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
_debugmallocstats() undercounts bytes in type-specific free lists #148189
56 > 40, by 16. Same 16 missing for dicts and tuples. I suspect (but don't know) that they're missing the two pointers (before the PYObject's type pointer) used by cyclic gc to link together the members of a generation.
Labeling as "easy", because it has to 😉 be shallow. A good exercise for somene to sharpen their archeological skills.
Good! The high-order bit is to use _PyObject_SIZE(Py_TYPE(self)), which already knows total object struct sizes, gc or not. It's a macro that reads up the tp_basicsize member of a type object. To check results, make sure "bytes each" output matches sys.getsizeof() output.
But for Tuples it should be _PyObject_VAR_SIZE(Py_TYPE(self)) correct? Also view the PR for the changes I made. Right now for objects and dicts its a bit more manual, but it was just something to get started with to properly calculate the size.
Should be done anyways, I just did the same thing I did with the other list and dict objects and made it so that: _PyType_PreHeaderSize(&PyTYPE_Type) is added to the size(PyTYPEObject).
Tests are always good, but it's clumsy here: there is no programmatic interface, the function just prints English to stderr.
At a higher level, these "free list" stats didn't belong here to begin with. I wrote the initial implementation, and it was intended to report on pymalloc's own internals. Type-specific free lists have nothing to do with that. They're independent of the object allocator in use.
So better would be to refactor, so that free-list reporting would be in its own function. Then, e.g., builds that use mimalloc instead could also use it. The new function would be the place to add new tests of it.
In any case, I don't intend to pursue this. Happy to see someone else do it, though 😉.
Activity
MazinSharaf commentedon Apr 7, 2026
Isn't the tuple supposed to remain unchanged though? And also why are you using AI to write the comment?
tim-one commentedon Apr 7, 2026
I believe the analysis is correct, but the "fix" is off. The code should follow what the sys.siseof() implementation does instead. For lists, that's
_PyObject_SIZE()already knows everything, and will be updated as needed if/when things change.@MazinSharaf. yes, tuples are immutable, but that isn't relevant. They still participate in cyclic gc, because they can appear in cycles, like
So tuple objects also have a GC head prefix.
MazinSharaf commentedon Apr 7, 2026
Alright then I'll change calculation of the tuple in the PR I made.
tim-one commentedon Apr 7, 2026
Good! The high-order bit is to use
_PyObject_SIZE(Py_TYPE(self)), which already knows total object struct sizes, gc or not. It's a macro that reads up thetp_basicsizemember of a type object. To check results, make sure "bytes each" output matchessys.getsizeof()output.MazinSharaf commentedon Apr 7, 2026
But for Tuples it should be
_PyObject_VAR_SIZE(Py_TYPE(self))correct? Also view the PR for the changes I made. Right now for objects and dicts its a bit more manual, but it was just something to get started with to properly calculate the size.MazinSharaf commentedon Apr 7, 2026
Should be done anyways, I just did the same thing I did with the other list and dict objects and made it so that:
_PyType_PreHeaderSize(&PyTYPE_Type)is added to thesize(PyTYPEObject).GH-148189: Fix miscalculation of type-specific free list memory use (#…
sergey-miryanov commentedon May 6, 2026
Should we add tests for this?
tim-one commentedon May 6, 2026
Tests are always good, but it's clumsy here: there is no programmatic interface, the function just prints English to stderr.
At a higher level, these "free list" stats didn't belong here to begin with. I wrote the initial implementation, and it was intended to report on pymalloc's own internals. Type-specific free lists have nothing to do with that. They're independent of the object allocator in use.
So better would be to refactor, so that free-list reporting would be in its own function. Then, e.g., builds that use mimalloc instead could also use it. The new function would be the place to add new tests of it.
In any case, I don't intend to pursue this. Happy to see someone else do it, though 😉.