-
-
Notifications
You must be signed in to change notification settings - Fork 32.1k
gh-128762: Include inline values in sys.getsizeof()
#128763
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
base: main
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change | ||||||||||||||||||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
|
@@ -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' | ||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||
|
@@ -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): | ||||||||||||||||||||||||||||
vsize = test.support.calcvobjsize | ||||||||||||||||||||||||||||
gc_header_size = self.gc_headsize | ||||||||||||||||||||||||||||
inline_values_size = vsize('P') | ||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||
linked_list = None | ||||||||||||||||||||||||||||
for i in range(28): | ||||||||||||||||||||||||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I happend to encounter the same issue. What seems to happen is that the size of the inline values starts at 30, and then decreases by 1 for each new instance—until it reaches the minimum possible (i.e. the number of unique keys encountered so far). If more than 30 different keys are encountered, values are no longer inlined. I wasn't able the find the exact reason why, but it seems to be a simple way of 'right-sizing' the inline values. You can imagine when creating 10k instances it all amortizes to the 'right' size to cover the attributes set in the Relevant source: cpython/Include/internal/pycore_dict.h Lines 314 to 326 in b90ecea
|
||||||||||||||||||||||||||||
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): | ||||||||||||||||||||||||||||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
:func:`sys.getsizeof` now accounts for inline values stored alongside the object |
There was a problem hiding this comment.
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.
Uh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'm not sure if it's what you're looking for, but here's the code I've been using to inspect the inline values from Python: