-
-
Notifications
You must be signed in to change notification settings - Fork 32.5k
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 1 commit
4dbee9a
5b31470
172f2c0
d76ae2a
7aad246
7c20c6c
7be3ae8
7436223
657d51f
4336cc6
19d4374
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
- Loading branch information
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -224,7 +224,8 @@ def test_overallocation(self): | |
overalloc_amts = [] | ||
for literal in test_literals: | ||
# Ensure that both list literals, and lists made from an iterable | ||
# of known size use the same amount of allocation. | ||
# of known size use the same amount of allocation. It will be | ||
# verified later that no over-allocation occurs for list 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. This comment says "verified later", but I see no specific test of no-over-allocation of list literals. There is prev_literal = None
for literal in [[] + test_literals]:
if prev_literal is not None
# Allocation of literal N uses one more pointer than allocation of literal N-1
self.assertEqual(sizeof(literal), sizeof(prev_literal) + struct.calcsize('P'))
prev_literal = literal 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. Yes, I should have said "verified below", ie. in same test, after the loop. It's an implicit test, though, since with the current over-allocation strategy, the list capacity can sometimes equal list length, so it's not universally true that each list-literal will have less memory allocated than a normal list of equivalent length. So it only can test that at least sometimes the list-literals are created with less capacity then a grown list of equivalent length. I was avoiding a direct test of memory usage, like you propose, just to keep things general. But I can certainly add such a direct test, which (afaict) is really the only way to test that all the list-literals are being created without over-allocation. 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. @pitrou I ran with your idea, and added a direct test of the memory allocated for a list-literal, which also let me remove the confusing wording about verifying "later", since that's no longer the case. Thanks for the suggestion. |
||
self.assertEqual(sizeof(literal), sizeof(list(literal))) | ||
self.assertEqual(sizeof(literal), sizeof(list(tuple(literal)))) | ||
|
||
|
@@ -239,6 +240,15 @@ def test_overallocation(self): | |
# 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 |
||
|
||
# Empty lists should overallocate on initial append/insert (unlike | ||
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. Turn this bit into an additional test method. |
||
# list-literals) | ||
l1 = [] | ||
l1.append(1) | ||
self.assertGreater(sizeof(l1), sizeof([1])) | ||
l2 = [] | ||
l2.insert(0, 1) | ||
self.assertGreater(sizeof(l2), sizeof([1])) | ||
|
||
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 | ||
|
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.
no action required: meta: this is where I really wish we had an
absltest.paramtereize
orpytest.mark.parameterize
available in the stdlib. it'd run all of these as unique tests with appropriate names instead of a loop that prevents others from running upon first failure.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 think unittest.subTest() displays parameters on the failing subtests and keeps running through each even on failure.