@@ -91,15 +91,17 @@ ensure_shared_on_resize(PyListObject *self)
9191#endif
9292}
9393
94- #define LIST_SMALL_ALLOCATED 32
94+ #define LIST_SMALL_ALLOCATED _Py_LIST_SMALL_ALLOCATED
9595
9696static int py_list_resize (PyListObject * self , Py_ssize_t newsize );
9797
98- /* Ensure ob_item has room for at least newsize elements, and set
98+ /* Grow a list.
99+ *
100+ * Ensure ob_item has room for at least newsize elements, and set
99101 * ob_size to newsize. If newsize > ob_size on entry, the content
100102 * of the new slots at exit is undefined heap trash; it's the caller's
101103 * responsibility to overwrite them with sane values.
102- * The number of allocated elements may grow, shrink, or stay the same.
104+ * The number of allocated elements may grow, or stay the same.
103105 * Failure is impossible if newsize <= self.allocated on entry.
104106 * Note that self->ob_item may change, and even if newsize is less
105107 * than ob_size on entry.
@@ -110,6 +112,27 @@ static int py_list_resize(PyListObject *self, Py_ssize_t newsize);
110112static inline Py_ALWAYS_INLINE int
111113list_resize (PyListObject * self , Py_ssize_t newsize )
112114{
115+ // list_resize() must only be called to grow the list
116+ assert (Py_SIZE (self ) <= newsize );
117+ Py_ssize_t allocated = self -> allocated ;
118+
119+ /* Bypass realloc() when a previous overallocation is large enough
120+ to accommodate the newsize. */
121+ if (newsize <= allocated && (self -> ob_item != NULL || newsize == 0 )) {
122+ Py_SET_SIZE (self , newsize );
123+ return 0 ;
124+ }
125+ return py_list_resize (self , newsize );
126+ }
127+
128+ /* Shrink a list. Similar to list_resize(), but it can shrink the list if
129+ * needed. The function cannot fail: on memory allocation failure, leave the
130+ * list items unchanged (only change the list size). */
131+ static inline Py_ALWAYS_INLINE void
132+ list_resize_shrink (PyListObject * self , Py_ssize_t newsize )
133+ {
134+ // list_resize_shrink() must only be called to shrink the list
135+ assert (0 <= newsize && newsize <= Py_SIZE (self ));
113136 Py_ssize_t allocated = self -> allocated ;
114137
115138 /* Bypass realloc() when a previous overallocation is large enough
@@ -123,12 +146,19 @@ list_resize(PyListObject *self, Py_ssize_t newsize)
123146 {
124147 assert (self -> ob_item != NULL || newsize == 0 );
125148 Py_SET_SIZE (self , newsize );
126- return 0 ;
149+ return ;
127150 }
128- return py_list_resize (self , newsize );
151+
152+ int res = py_list_resize (self , newsize );
153+ #ifndef NDEBUG
154+ assert (res == 0 );
155+ #else
156+ (void )res ;
157+ #endif
129158}
130159
131- /* Slow path of list_resize(): allocate or reallocate ob_item. */
160+ /* Slow path of list_resize() and list_resize_shrink():
161+ * allocate or reallocate ob_item. */
132162static int
133163py_list_resize (PyListObject * self , Py_ssize_t newsize )
134164{
@@ -1065,7 +1095,7 @@ list_ass_slice_lock_held(PyListObject *a, Py_ssize_t ilow, Py_ssize_t ihigh, PyO
10651095 if (d < 0 ) { /* Delete -d items */
10661096 Py_ssize_t tail = Py_SIZE (a ) - ihigh ;
10671097 ptr_wise_atomic_memmove (a , & item [ihigh + d ], & item [ihigh ], tail );
1068- ( void ) list_resize (a , Py_SIZE (a ) + d ); // NB: shrinking a list can't fail
1098+ list_resize_shrink (a , Py_SIZE (a ) + d ); // NB: shrinking a list can't fail
10691099 item = a -> ob_item ;
10701100 }
10711101 else if (d > 0 ) { /* Insert d items */
@@ -1214,7 +1244,7 @@ list_ass_item_impl(PyListObject *a, Py_ssize_t i, PyObject *v)
12141244 if (i < size - 1 ) {
12151245 list_shift_items_left_lock_held (a , i , size - 1 );
12161246 }
1217- list_resize (a , size - 1 ); // NB: shrinking a list can't fail
1247+ list_resize_shrink (a , size - 1 ); // NB: shrinking a list can't fail
12181248 }
12191249 else {
12201250 FT_ATOMIC_STORE_PTR_RELEASE (a -> ob_item [i ], Py_NewRef (v ));
@@ -1394,8 +1424,7 @@ list_extend_iter_lock_held(PyListObject *self, PyObject *iterable)
13941424
13951425 /* Cut back result list if initial guess was too large. */
13961426 if (Py_SIZE (self ) < self -> allocated ) {
1397- if (list_resize (self , Py_SIZE (self )) < 0 )
1398- goto error ;
1427+ list_resize_shrink (self , Py_SIZE (self ));
13991428 }
14001429
14011430 Py_DECREF (it );
@@ -1667,7 +1696,7 @@ list_pop_impl(PyListObject *self, Py_ssize_t index)
16671696 ptr_wise_atomic_memmove (self , & items [index ], & items [index + 1 ],
16681697 size_after_pop - index );
16691698 }
1670- list_resize (self , size_after_pop ); // NB: shrinking a list can't fail
1699+ list_resize_shrink (self , size_after_pop ); // NB: shrinking a list can't fail
16711700 return v ;
16721701}
16731702
@@ -3835,7 +3864,6 @@ list_ass_subscript_lock_held(PyObject *_self, PyObject *item, PyObject *value)
38353864 PyObject * * garbage ;
38363865 size_t cur ;
38373866 Py_ssize_t i ;
3838- int res ;
38393867
38403868 Py_ssize_t slicelength = adjust_slice_indexes (self , & start , & stop ,
38413869 step );
@@ -3885,15 +3913,14 @@ list_ass_subscript_lock_held(PyObject *_self, PyObject *item, PyObject *value)
38853913 self -> ob_item + cur , Py_SIZE (self ) - cur );
38863914 }
38873915
3888- Py_SET_SIZE (self , Py_SIZE (self ) - slicelength );
3889- res = list_resize (self , Py_SIZE (self ));
3916+ list_resize_shrink (self , Py_SIZE (self ) - slicelength );
38903917
38913918 for (i = 0 ; i < slicelength ; i ++ ) {
38923919 Py_DECREF (garbage [i ]);
38933920 }
38943921 PyMem_Free (garbage );
38953922
3896- return res ;
3923+ return 0 ;
38973924 }
38983925 else {
38993926 /* assign slice */
0 commit comments