Skip to content

Commit 5cd80db

Browse files
committed
Address code review
1 parent 23ad656 commit 5cd80db

1 file changed

Lines changed: 6 additions & 9 deletions

File tree

‎Objects/listobject.c‎

Lines changed: 6 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -111,8 +111,12 @@ list_resize(PyListObject *self, Py_ssize_t newsize)
111111
/* Bypass realloc() when a previous overallocation is large enough
112112
to accommodate the newsize. If the newsize falls lower than half
113113
the allocated size, then proceed with the realloc() to shrink the list.
114+
gh-158602: do not shrink a small list, the realloc() cost is bigger
115+
than the memory we get back.
114116
*/
115-
if (allocated >= newsize && newsize >= (allocated >> 1)) {
117+
if (allocated >= newsize
118+
&& (newsize >= (allocated >> 1) || allocated <= LIST_SMALL_ALLOCATED))
119+
{
116120
assert(self->ob_item != NULL || newsize == 0);
117121
Py_SET_SIZE(self, newsize);
118122
return 0;
@@ -138,14 +142,7 @@ list_resize(PyListObject *self, Py_ssize_t newsize)
138142
if (newsize == 0)
139143
new_allocated = 0;
140144

141-
// gh-158602: when shrinking, do not reallocate the array of a small list.
142-
if (newsize < allocated) {
143-
if (allocated <= LIST_SMALL_ALLOCATED) {
144-
Py_SET_SIZE(self, newsize);
145-
return 0;
146-
}
147-
assert(new_allocated < (size_t)allocated);
148-
}
145+
assert(newsize > allocated || new_allocated < (size_t)allocated);
149146

150147
ensure_shared_on_resize(self);
151148

0 commit comments

Comments
 (0)