Skip to content

Commit de49474

Browse files
committed
gh-115605 Use QSBR technique for list_new/free
1 parent 32da182 commit de49474

File tree

1 file changed

+21
-5
lines changed

1 file changed

+21
-5
lines changed

Objects/listobject.c

+21-5
Original file line numberDiff line numberDiff line change
@@ -31,12 +31,12 @@ get_list_freelist(void)
3131
}
3232
#endif
3333

34+
#ifdef Py_GIL_DISABLED
3435
static size_t
3536
list_good_size(Py_ssize_t size)
3637
{
3738
// 4, 8, 16, 24, 32, 40, 48, 64, 80, ...
3839
// NOTE: we add one here so that the rounding accounts for the "allocated"
39-
// field in _PyListArray.
4040
size_t reqsize = (size_t)size + 1;
4141
if (reqsize <= 4) {
4242
reqsize = 4;
@@ -67,6 +67,7 @@ list_allocate_items(size_t capacity)
6767
PyObject **items = PyMem_Malloc(capacity * sizeof(PyObject *));
6868
return items;
6969
}
70+
#endif
7071

7172
static PyListObject *
7273
list_new(Py_ssize_t size)
@@ -699,7 +700,7 @@ list_repeat(PyObject *aa, Py_ssize_t n)
699700
}
700701

701702
static void
702-
list_clear(PyListObject *a)
703+
list_clear_impl(PyListObject *a, bool is_resize)
703704
{
704705
PyObject **items = a->ob_item;
705706
if (items == NULL) {
@@ -716,16 +717,31 @@ list_clear(PyListObject *a)
716717
Py_XDECREF(items[i]);
717718
}
718719
// TODO: Use QSBR technique, if the list is shared between threads,
719-
PyMem_Free(items);
720-
720+
#ifdef Py_GIL_DISABLED
721+
bool use_qsbr = is_resize && _PyObject_GC_IS_SHARED(a);
722+
#else
723+
bool use_qsbr = false;
724+
#endif
725+
if (use_qsbr) {
726+
_PyMem_FreeDelayed(items);
727+
}
728+
else {
729+
PyMem_Free(items);
730+
}
721731
// Note that there is no guarantee that the list is actually empty
722732
// at this point, because XDECREF may have populated it indirectly again!
723733
}
724734

735+
static void
736+
list_clear(PyListObject *a)
737+
{
738+
list_clear_impl(a, true);
739+
}
740+
725741
static int
726742
list_clear_slot(PyObject *self)
727743
{
728-
list_clear((PyListObject *)self);
744+
list_clear_impl((PyListObject *)self, false);
729745
return 0;
730746
}
731747

0 commit comments

Comments
 (0)