Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

gh-128762: Include inline values in sys.getsizeof() #128763

Open
wants to merge 5 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 14 additions & 0 deletions Lib/test/test_sys.py
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,10 @@ def requires_subinterpreters(meth):
return unittest.skipIf(interpreters is None,
'subinterpreters required')(meth)

class ObjectWithValue:
value: ObjectWithValue | None
def __init__(self, value):
self.value = value

DICT_KEY_STRUCT_FORMAT = 'n2BI2n'

Expand Down Expand Up @@ -1475,6 +1479,16 @@ def test_gc_head_size(self):
# but lists are
self.assertEqual(sys.getsizeof([]), vsize('Pn') + gc_header_size)

def test_inline_values(self):
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I have overall low confidence that this is the right way to test this, and I'm open to suggestions.

vsize = test.support.calcvobjsize
gc_header_size = self.gc_headsize
inline_values_size = 32

linked_list = None
for i in range(28):
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I find that it needs this many iterations to "stabilize". Is there a right way to do this?

linked_list = ObjectWithValue(linked_list)
self.assertEqual(sys.getsizeof(linked_list), vsize('P') + gc_header_size + inline_values_size)

def test_errors(self):
class BadSizeof:
def __sizeof__(self):
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
`sys.getsizeof()` now accounts for inline values stored alongside the object
9 changes: 7 additions & 2 deletions Python/sysmodule.c
Original file line number Diff line number Diff line change
Expand Up @@ -1842,10 +1842,11 @@ _PySys_GetSizeOf(PyObject *o)
PyObject *res = NULL;
PyObject *method;
Py_ssize_t size;
PyTypeObject *type = Py_TYPE(o);
PyThreadState *tstate = _PyThreadState_GET();

/* Make sure the type is initialized. float gets initialized late */
if (PyType_Ready(Py_TYPE(o)) < 0) {
if (PyType_Ready(type) < 0) {
return (size_t)-1;
}

Expand All @@ -1854,7 +1855,7 @@ _PySys_GetSizeOf(PyObject *o)
if (!_PyErr_Occurred(tstate)) {
_PyErr_Format(tstate, PyExc_TypeError,
"Type %.100s doesn't define __sizeof__",
Py_TYPE(o)->tp_name);
type->tp_name);
}
}
else {
Expand All @@ -1876,6 +1877,10 @@ _PySys_GetSizeOf(PyObject *o)
return (size_t)-1;
}

if (type->tp_flags & Py_TPFLAGS_INLINE_VALUES) {
size += _PyInlineValuesSize(type);
}

size_t presize = 0;
if (!Py_IS_TYPE(o, &PyType_Type) ||
PyType_HasFeature((PyTypeObject *)o, Py_TPFLAGS_HEAPTYPE))
Expand Down
Loading