-
-
Notifications
You must be signed in to change notification settings - Fork 32.4k
bpo-43574: Dont overallocate list literals #24954
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
Changes from 7 commits
4dbee9a
5b31470
172f2c0
d76ae2a
7aad246
7c20c6c
7be3ae8
7436223
657d51f
4336cc6
19d4374
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 |
---|---|---|
|
@@ -196,6 +196,49 @@ def test_preallocation(self): | |
self.assertEqual(iter_size, sys.getsizeof(list([0] * 10))) | ||
self.assertEqual(iter_size, sys.getsizeof(list(range(10)))) | ||
|
||
@cpython_only | ||
def test_overallocation(self): | ||
# bpo-33234: Don't overallocate when initialized from known lengths | ||
# bpo-38373: Allows list over-allocation to be zero for some lengths | ||
# bpo-43574: Don't overallocate for list-literals | ||
sizeof = sys.getsizeof | ||
|
||
# First handle empty list and empty list-literal cases. Should have no | ||
# overallocation, including init from iterable of unknown length. | ||
self.assertEqual(sizeof([]), sizeof(list())) | ||
self.assertEqual(sizeof([]), sizeof(list(tuple()))) | ||
self.assertEqual(sizeof([]), sizeof(list(x for x in []))) | ||
|
||
# Must use actual list-literals to test the overallocation behavior of | ||
# compiled list-literals as well as those initialized from them. | ||
test_literals = [ | ||
[1], | ||
[1,2], | ||
[1,2,3], # Literals of length > 2 are special-cased in compile | ||
[1,2,3,4], | ||
[1,2,3,4,5,6,7], | ||
[1,2,3,4,5,6,7,8], # bpo-38373: Length 8 init won't over-alloc | ||
[1,2,3,4,5,6,7,8,9], | ||
] | ||
|
||
overalloc_amts = [] | ||
for literal in test_literals: | ||
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. no action required: meta: this is where I really wish we had an 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 think unittest.subTest() displays parameters on the failing subtests and keeps running through each even on failure. |
||
# Ensure that both list literals, and lists made from an iterable | ||
# of known size use the same amount of allocation. | ||
self.assertEqual(sizeof(literal), sizeof(list(literal))) | ||
self.assertEqual(sizeof(literal), sizeof(list(tuple(literal)))) | ||
|
||
# By contrast, confirm that non-empty lists initialized from an | ||
# iterable where the length is unknown at the time of | ||
# initialization, can be overallocated. | ||
iterated_list = list(x for x in literal) | ||
overalloc_amts.append(sizeof(iterated_list) - sizeof(literal)) | ||
self.assertGreaterEqual(sizeof(iterated_list), sizeof(literal)) | ||
|
||
# bpo-38373: initialized or grown lists are not always over-allocated. | ||
# Confirm that over-allocation occurs at least some of the time. | ||
self.assertEqual(True, any(x>0 for x in overalloc_amts)) | ||
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. add |
||
|
||
def test_count_index_remove_crashes(self): | ||
# bpo-38610: The count(), index(), and remove() methods were not | ||
# holding strong references to list elements while calling | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
Restores previous list memory behavior where lists initialized from literals | ||
chadnetzer marked this conversation as resolved.
Show resolved
Hide resolved
|
||
aren't over-allocated. |
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.
make this set of initial assertions into its own test method.