GH-148189: Fix miscalculation of type-specific free list memory use (#148190)

* Fix calculation of PyListObject size in allocator

* Fix size calculation in _PyDict_DebugMallocStats

* Fix memory size calculation in tupleobject.c

Adjusted memory calculation for PyTupleObject freelist entries.

* Revert in tupleobject.c

Removed unnecessary comment regarding memory calculation and the memory calculation itself.

* 📜🤖 Added by blurb_it.

* Update tupleobject.c

---------

Co-authored-by: blurb-it[bot] <43283697+blurb-it[bot]@users.noreply.github.com>
This commit is contained in:
Mazin Sharaf 2026-05-03 13:03:13 +10:00 committed by GitHub
parent c1940bcfc8
commit 1fc2b38d63
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
4 changed files with 4 additions and 3 deletions

View file

@ -0,0 +1 @@
Repaired undercount of bytes in type-specific free lists reported by sys._debugmallocstats(). For types that participate in cyclic garbage collection, it was missing two pointers used by GC.

View file

@ -443,7 +443,7 @@ _PyDict_DebugMallocStats(FILE *out)
{
_PyDebugAllocatorStats(out, "free PyDictObject",
_Py_FREELIST_SIZE(dicts),
sizeof(PyDictObject));
_PyType_PreHeaderSize(&PyDict_Type) + sizeof(PyDictObject));
_PyDebugAllocatorStats(out, "free PyDictKeysObject",
_Py_FREELIST_SIZE(dictkeys),
sizeof(PyDictKeysObject));

View file

@ -234,7 +234,7 @@ _PyList_DebugMallocStats(FILE *out)
_PyDebugAllocatorStats(out,
"free PyListObject",
_Py_FREELIST_SIZE(lists),
sizeof(PyListObject));
_PyType_PreHeaderSize(&PyList_Type) + sizeof(PyListObject));
}
PyObject *

View file

@ -1286,6 +1286,6 @@ _PyTuple_DebugMallocStats(FILE *out)
PyOS_snprintf(buf, sizeof(buf),
"free %d-sized PyTupleObject", len);
_PyDebugAllocatorStats(out, buf, _Py_FREELIST_SIZE(tuples[i]),
_PyObject_VAR_SIZE(&PyTuple_Type, len));
_PyType_PreHeaderSize(&PyTuple_Type) + _PyObject_VAR_SIZE(&PyTuple_Type, len));
}
}