Skip to content
Open
Show file tree
Hide file tree
Changes from all 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
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
Skip the array reallocation when shrinking a small :class:`list`. Patch by
Donghee Na.
13 changes: 12 additions & 1 deletion Objects/listobject.c
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,8 @@ ensure_shared_on_resize(PyListObject *self)
#endif
}

#define LIST_SMALL_ALLOCATED 32

/* Ensure ob_item has room for at least newsize elements, and set
* ob_size to newsize. If newsize > ob_size on entry, the content
* of the new slots at exit is undefined heap trash; it's the caller's
Expand Down Expand Up @@ -136,6 +138,15 @@ list_resize(PyListObject *self, Py_ssize_t newsize)
if (newsize == 0)
new_allocated = 0;

// gh-158602: when shrinking, do not reallocate the array of a small list.
if (newsize < allocated) {
if (allocated <= LIST_SMALL_ALLOCATED) {
Py_SET_SIZE(self, newsize);
return 0;
}
assert(new_allocated < (size_t)allocated);
}

ensure_shared_on_resize(self);

#ifdef Py_GIL_DISABLED
Expand Down Expand Up @@ -1148,7 +1159,7 @@ list_ass_item_lock_held(PyListObject *a, Py_ssize_t i, PyObject *v)
for (Py_ssize_t idx = i; idx < size - 1; idx++) {
FT_ATOMIC_STORE_PTR_RELEASE(a->ob_item[idx], a->ob_item[idx + 1]);
}
Py_SET_SIZE(a, size - 1);
list_resize(a, size - 1); // NB: shrinking a list can't fail

@x42005e1f x42005e1f Oct 4, 2026 •

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Here, you are doing the same thing I did in my PR. Please do not do this: to me, it does not seem very ethical. Keep exactly what the title says: the list_resize() tuning.

@corona10 corona10 Oct 4, 2026 •

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

@x42005e1f This is the reason why I added you as co-authored of this patch. But your initial proposal would be very difficult to accept because of performance issue. To supplement your patch, I spent most of time for the data analysis.

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

But your initial proposal would be very difficult to accept because of performance issue.

In that case, why not merge this PR without the fix first, and then mine, or vice versa? What is the problem? You do not even have a NEWS entry about the fix. I even mentioned a possible tuning of list_resize() in the description of my PR, but I did not make the corresponding changes because that goes beyond the scope of this specific fix.

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

We can do that if you want. But for the performance optimization, we need to understand how it worth to do for especially for this kind of performance regression issue.

I am fine with merging this PR after your PR is merged.

This is part of effort to pursuade your issue is worth to fix.

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

Better communication for this kind of issue is that

  1. Simply request to add more detail or your credit to NEWS.d (Sorry I forgot to mention this even I added you as co-author in commit log)
    or
  2. Request to merge this PR after your PR is merged.

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

To me, the second option seems preferable, so I am requesting it.

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

In anycase, we need to wait until other core devs like this approach. So please be patient.

}
else {
FT_ATOMIC_STORE_PTR_RELEASE(a->ob_item[i], Py_NewRef(v));
Expand Down
Loading