Coverage Report

Created: 2025-11-24 06:34

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/cpython3/Objects/listobject.c
Line
Count
Source
1
/* List object implementation */
2
3
#include "Python.h"
4
#include "pycore_abstract.h"      // _PyIndex_Check()
5
#include "pycore_ceval.h"         // _PyEval_GetBuiltin()
6
#include "pycore_critical_section.h"  // _Py_CRITICAL_SECTION_ASSERT_OBJECT_LOCKED()
7
#include "pycore_dict.h"          // _PyDictViewObject
8
#include "pycore_freelist.h"      // _Py_FREELIST_FREE(), _Py_FREELIST_POP()
9
#include "pycore_interp.h"        // PyInterpreterState.list
10
#include "pycore_list.h"          // struct _Py_list_freelist, _PyListIterObject
11
#include "pycore_long.h"          // _PyLong_DigitCount
12
#include "pycore_modsupport.h"    // _PyArg_NoKwnames()
13
#include "pycore_object.h"        // _PyObject_GC_TRACK(), _PyDebugAllocatorStats()
14
#include "pycore_pyatomic_ft_wrappers.h"
15
#include "pycore_setobject.h"     // _PySet_NextEntry()
16
#include "pycore_stackref.h"      // _Py_TryIncrefCompareStackRef()
17
#include "pycore_tuple.h"         // _PyTuple_FromArraySteal()
18
#include "pycore_typeobject.h"    // _Py_TYPE_VERSION_LIST
19
#include <stddef.h>
20
21
/*[clinic input]
22
class list "PyListObject *" "&PyList_Type"
23
[clinic start generated code]*/
24
/*[clinic end generated code: output=da39a3ee5e6b4b0d input=f9b222678f9f71e0]*/
25
26
#include "clinic/listobject.c.h"
27
28
_Py_DECLARE_STR(list_err, "list index out of range");
29
30
#ifdef Py_GIL_DISABLED
31
typedef struct {
32
    Py_ssize_t allocated;
33
    PyObject *ob_item[];
34
} _PyListArray;
35
36
static _PyListArray *
37
list_allocate_array(size_t capacity)
38
{
39
    if (capacity > PY_SSIZE_T_MAX/sizeof(PyObject*) - 1) {
40
        return NULL;
41
    }
42
    _PyListArray *array = PyMem_Malloc(sizeof(_PyListArray) + capacity * sizeof(PyObject *));
43
    if (array == NULL) {
44
        return NULL;
45
    }
46
    array->allocated = capacity;
47
    return array;
48
}
49
50
static Py_ssize_t
51
list_capacity(PyObject **items)
52
{
53
    _PyListArray *array = _Py_CONTAINER_OF(items, _PyListArray, ob_item);
54
    return array->allocated;
55
}
56
#endif
57
58
static void
59
free_list_items(PyObject** items, bool use_qsbr)
60
15.2M
{
61
#ifdef Py_GIL_DISABLED
62
    _PyListArray *array = _Py_CONTAINER_OF(items, _PyListArray, ob_item);
63
    if (use_qsbr) {
64
        size_t size = sizeof(_PyListArray) + array->allocated * sizeof(PyObject *);
65
        _PyMem_FreeDelayed(array, size);
66
    }
67
    else {
68
        PyMem_Free(array);
69
    }
70
#else
71
15.2M
    PyMem_Free(items);
72
15.2M
#endif
73
15.2M
}
74
75
static void
76
ensure_shared_on_resize(PyListObject *self)
77
15.1M
{
78
#ifdef Py_GIL_DISABLED
79
    // We can't use _Py_CRITICAL_SECTION_ASSERT_OBJECT_LOCKED here because
80
    // the `CALL_LIST_APPEND` bytecode handler may lock the list without
81
    // a critical section.
82
    assert(Py_REFCNT(self) == 1 || PyMutex_IsLocked(&_PyObject_CAST(self)->ob_mutex));
83
84
    // Ensure that the list array is freed using QSBR if we are not the
85
    // owning thread.
86
    if (!_Py_IsOwnedByCurrentThread((PyObject *)self) &&
87
        !_PyObject_GC_IS_SHARED(self))
88
    {
89
        _PyObject_GC_SET_SHARED(self);
90
    }
91
#endif
92
15.1M
}
93
94
/* Ensure ob_item has room for at least newsize elements, and set
95
 * ob_size to newsize.  If newsize > ob_size on entry, the content
96
 * of the new slots at exit is undefined heap trash; it's the caller's
97
 * responsibility to overwrite them with sane values.
98
 * The number of allocated elements may grow, shrink, or stay the same.
99
 * Failure is impossible if newsize <= self.allocated on entry, although
100
 * that partly relies on an assumption that the system realloc() never
101
 * fails when passed a number of bytes <= the number of bytes last
102
 * allocated (the C standard doesn't guarantee this, but it's hard to
103
 * imagine a realloc implementation where it wouldn't be true).
104
 * Note that self->ob_item may change, and even if newsize is less
105
 * than ob_size on entry.
106
 */
107
static int
108
list_resize(PyListObject *self, Py_ssize_t newsize)
109
15.4M
{
110
15.4M
    size_t new_allocated, target_bytes;
111
15.4M
    Py_ssize_t allocated = self->allocated;
112
113
    /* Bypass realloc() when a previous overallocation is large enough
114
       to accommodate the newsize.  If the newsize falls lower than half
115
       the allocated size, then proceed with the realloc() to shrink the list.
116
    */
117
15.4M
    if (allocated >= newsize && newsize >= (allocated >> 1)) {
118
350k
        assert(self->ob_item != NULL || newsize == 0);
119
350k
        Py_SET_SIZE(self, newsize);
120
350k
        return 0;
121
350k
    }
122
123
    /* This over-allocates proportional to the list size, making room
124
     * for additional growth.  The over-allocation is mild, but is
125
     * enough to give linear-time amortized behavior over a long
126
     * sequence of appends() in the presence of a poorly-performing
127
     * system realloc().
128
     * Add padding to make the allocated size multiple of 4.
129
     * The growth pattern is:  0, 4, 8, 16, 24, 32, 40, 52, 64, 76, ...
130
     * Note: new_allocated won't overflow because the largest possible value
131
     *       is PY_SSIZE_T_MAX * (9 / 8) + 6 which always fits in a size_t.
132
     */
133
15.1M
    new_allocated = ((size_t)newsize + (newsize >> 3) + 6) & ~(size_t)3;
134
    /* Do not overallocate if the new size is closer to overallocated size
135
     * than to the old size.
136
     */
137
15.1M
    if (newsize - Py_SIZE(self) > (Py_ssize_t)(new_allocated - newsize))
138
18.8k
        new_allocated = ((size_t)newsize + 3) & ~(size_t)3;
139
140
15.1M
    if (newsize == 0)
141
0
        new_allocated = 0;
142
143
15.1M
    ensure_shared_on_resize(self);
144
145
#ifdef Py_GIL_DISABLED
146
    _PyListArray *array = list_allocate_array(new_allocated);
147
    if (array == NULL) {
148
        PyErr_NoMemory();
149
        return -1;
150
    }
151
    PyObject **old_items = self->ob_item;
152
    if (self->ob_item) {
153
        if (new_allocated < (size_t)allocated) {
154
            target_bytes = new_allocated * sizeof(PyObject*);
155
        }
156
        else {
157
            target_bytes = allocated * sizeof(PyObject*);
158
        }
159
        memcpy(array->ob_item, self->ob_item, target_bytes);
160
    }
161
    if (new_allocated > (size_t)allocated) {
162
        memset(array->ob_item + allocated, 0, sizeof(PyObject *) * (new_allocated - allocated));
163
    }
164
     _Py_atomic_store_ptr_release(&self->ob_item, &array->ob_item);
165
    self->allocated = new_allocated;
166
    Py_SET_SIZE(self, newsize);
167
    if (old_items != NULL) {
168
        free_list_items(old_items, _PyObject_GC_IS_SHARED(self));
169
    }
170
#else
171
15.1M
    PyObject **items;
172
15.1M
    if (new_allocated <= (size_t)PY_SSIZE_T_MAX / sizeof(PyObject *)) {
173
15.1M
        target_bytes = new_allocated * sizeof(PyObject *);
174
15.1M
        items = (PyObject **)PyMem_Realloc(self->ob_item, target_bytes);
175
15.1M
    }
176
0
    else {
177
        // integer overflow
178
0
        items = NULL;
179
0
    }
180
15.1M
    if (items == NULL) {
181
0
        PyErr_NoMemory();
182
0
        return -1;
183
0
    }
184
15.1M
    self->ob_item = items;
185
15.1M
    Py_SET_SIZE(self, newsize);
186
15.1M
    self->allocated = new_allocated;
187
15.1M
#endif
188
15.1M
    return 0;
189
15.1M
}
190
191
static int
192
list_preallocate_exact(PyListObject *self, Py_ssize_t size)
193
495k
{
194
495k
    PyObject **items;
195
495k
    assert(self->ob_item == NULL);
196
495k
    assert(size > 0);
197
198
    /* Since the Python memory allocator has granularity of 16 bytes on 64-bit
199
     * platforms (8 on 32-bit), there is no benefit of allocating space for
200
     * the odd number of items, and there is no drawback of rounding the
201
     * allocated size up to the nearest even number.
202
     */
203
495k
    size = (size + 1) & ~(size_t)1;
204
#ifdef Py_GIL_DISABLED
205
    _PyListArray *array = list_allocate_array(size);
206
    if (array == NULL) {
207
        PyErr_NoMemory();
208
        return -1;
209
    }
210
    items = array->ob_item;
211
    memset(items, 0, size * sizeof(PyObject *));
212
#else
213
495k
    items = PyMem_New(PyObject*, size);
214
495k
    if (items == NULL) {
215
0
        PyErr_NoMemory();
216
0
        return -1;
217
0
    }
218
495k
#endif
219
495k
    FT_ATOMIC_STORE_PTR_RELEASE(self->ob_item, items);
220
495k
    self->allocated = size;
221
495k
    return 0;
222
495k
}
223
224
/* Print summary info about the state of the optimized allocator */
225
void
226
_PyList_DebugMallocStats(FILE *out)
227
0
{
228
0
    _PyDebugAllocatorStats(out,
229
0
                           "free PyListObject",
230
0
                            _Py_FREELIST_SIZE(lists),
231
0
                           sizeof(PyListObject));
232
0
}
233
234
PyObject *
235
PyList_New(Py_ssize_t size)
236
58.2M
{
237
58.2M
    if (size < 0) {
238
0
        PyErr_BadInternalCall();
239
0
        return NULL;
240
0
    }
241
242
58.2M
    PyListObject *op = _Py_FREELIST_POP(PyListObject, lists);
243
58.2M
    if (op == NULL) {
244
19.6M
        op = PyObject_GC_New(PyListObject, &PyList_Type);
245
19.6M
        if (op == NULL) {
246
0
            return NULL;
247
0
        }
248
19.6M
    }
249
58.2M
    if (size <= 0) {
250
57.0M
        op->ob_item = NULL;
251
57.0M
    }
252
1.29M
    else {
253
#ifdef Py_GIL_DISABLED
254
        _PyListArray *array = list_allocate_array(size);
255
        if (array == NULL) {
256
            Py_DECREF(op);
257
            return PyErr_NoMemory();
258
        }
259
        memset(&array->ob_item, 0, size * sizeof(PyObject *));
260
        op->ob_item = array->ob_item;
261
#else
262
1.29M
        op->ob_item = (PyObject **) PyMem_Calloc(size, sizeof(PyObject *));
263
1.29M
#endif
264
1.29M
        if (op->ob_item == NULL) {
265
0
            Py_DECREF(op);
266
0
            return PyErr_NoMemory();
267
0
        }
268
1.29M
    }
269
58.2M
    Py_SET_SIZE(op, size);
270
58.2M
    op->allocated = size;
271
58.2M
    _PyObject_GC_TRACK(op);
272
58.2M
    return (PyObject *) op;
273
58.2M
}
274
275
static PyObject *
276
list_new_prealloc(Py_ssize_t size)
277
1.66M
{
278
1.66M
    assert(size > 0);
279
1.66M
    PyListObject *op = (PyListObject *) PyList_New(0);
280
1.66M
    if (op == NULL) {
281
0
        return NULL;
282
0
    }
283
1.66M
    assert(op->ob_item == NULL);
284
#ifdef Py_GIL_DISABLED
285
    _PyListArray *array = list_allocate_array(size);
286
    if (array == NULL) {
287
        Py_DECREF(op);
288
        return PyErr_NoMemory();
289
    }
290
    op->ob_item = array->ob_item;
291
#else
292
1.66M
    op->ob_item = PyMem_New(PyObject *, size);
293
1.66M
    if (op->ob_item == NULL) {
294
0
        Py_DECREF(op);
295
0
        return PyErr_NoMemory();
296
0
    }
297
1.66M
#endif
298
1.66M
    op->allocated = size;
299
1.66M
    return (PyObject *) op;
300
1.66M
}
301
302
Py_ssize_t
303
PyList_Size(PyObject *op)
304
44.9k
{
305
44.9k
    if (!PyList_Check(op)) {
306
0
        PyErr_BadInternalCall();
307
0
        return -1;
308
0
    }
309
44.9k
    else {
310
44.9k
        return PyList_GET_SIZE(op);
311
44.9k
    }
312
44.9k
}
313
314
static inline int
315
valid_index(Py_ssize_t i, Py_ssize_t limit)
316
48.3M
{
317
    /* The cast to size_t lets us use just a single comparison
318
       to check whether i is in the range: 0 <= i < limit.
319
320
       See:  Section 14.2 "Bounds Checking" in the Agner Fog
321
       optimization manual found at:
322
       https://www.agner.org/optimize/optimizing_cpp.pdf
323
    */
324
48.3M
    return (size_t) i < (size_t) limit;
325
48.3M
}
326
327
#ifdef Py_GIL_DISABLED
328
329
static PyObject *
330
list_item_impl(PyListObject *self, Py_ssize_t idx)
331
{
332
    PyObject *item = NULL;
333
    Py_BEGIN_CRITICAL_SECTION(self);
334
    if (!_PyObject_GC_IS_SHARED(self)) {
335
        _PyObject_GC_SET_SHARED(self);
336
    }
337
    Py_ssize_t size = Py_SIZE(self);
338
    if (!valid_index(idx, size)) {
339
        goto exit;
340
    }
341
    item = _Py_NewRefWithLock(self->ob_item[idx]);
342
exit:
343
    Py_END_CRITICAL_SECTION();
344
    return item;
345
}
346
347
static inline PyObject*
348
list_get_item_ref(PyListObject *op, Py_ssize_t i)
349
{
350
    if (!_Py_IsOwnedByCurrentThread((PyObject *)op) && !_PyObject_GC_IS_SHARED(op)) {
351
        return list_item_impl(op, i);
352
    }
353
    // Need atomic operation for the getting size.
354
    Py_ssize_t size = PyList_GET_SIZE(op);
355
    if (!valid_index(i, size)) {
356
        return NULL;
357
    }
358
    PyObject **ob_item = _Py_atomic_load_ptr(&op->ob_item);
359
    if (ob_item == NULL) {
360
        return NULL;
361
    }
362
    Py_ssize_t cap = list_capacity(ob_item);
363
    assert(cap != -1);
364
    if (!valid_index(i, cap)) {
365
        return NULL;
366
    }
367
    PyObject *item = _Py_TryXGetRef(&ob_item[i]);
368
    if (item == NULL) {
369
        return list_item_impl(op, i);
370
    }
371
    return item;
372
}
373
#else
374
static inline PyObject*
375
list_get_item_ref(PyListObject *op, Py_ssize_t i)
376
38.3M
{
377
38.3M
    if (!valid_index(i, Py_SIZE(op))) {
378
355k
        return NULL;
379
355k
    }
380
38.0M
    return Py_NewRef(PyList_GET_ITEM(op, i));
381
38.0M
}
382
#endif
383
384
PyObject *
385
PyList_GetItem(PyObject *op, Py_ssize_t i)
386
22.2k
{
387
22.2k
    if (!PyList_Check(op)) {
388
0
        PyErr_BadInternalCall();
389
0
        return NULL;
390
0
    }
391
22.2k
    if (!valid_index(i, Py_SIZE(op))) {
392
0
        _Py_DECLARE_STR(list_err, "list index out of range");
393
0
        PyErr_SetObject(PyExc_IndexError, &_Py_STR(list_err));
394
0
        return NULL;
395
0
    }
396
22.2k
    return ((PyListObject *)op) -> ob_item[i];
397
22.2k
}
398
399
PyObject *
400
PyList_GetItemRef(PyObject *op, Py_ssize_t i)
401
718
{
402
718
    if (!PyList_Check(op)) {
403
0
        PyErr_SetString(PyExc_TypeError, "expected a list");
404
0
        return NULL;
405
0
    }
406
718
    PyObject *item = list_get_item_ref((PyListObject *)op, i);
407
718
    if (item == NULL) {
408
0
        _Py_DECLARE_STR(list_err, "list index out of range");
409
0
        PyErr_SetObject(PyExc_IndexError, &_Py_STR(list_err));
410
0
        return NULL;
411
0
    }
412
718
    return item;
413
718
}
414
415
PyObject *
416
_PyList_GetItemRef(PyListObject *list, Py_ssize_t i)
417
715k
{
418
715k
    return list_get_item_ref(list, i);
419
715k
}
420
421
#ifdef Py_GIL_DISABLED
422
int
423
_PyList_GetItemRefNoLock(PyListObject *list, Py_ssize_t i, _PyStackRef *result)
424
{
425
    assert(_Py_IsOwnedByCurrentThread((PyObject *)list) ||
426
           _PyObject_GC_IS_SHARED(list));
427
    if (!valid_index(i, PyList_GET_SIZE(list))) {
428
        return 0;
429
    }
430
    PyObject **ob_item = _Py_atomic_load_ptr(&list->ob_item);
431
    if (ob_item == NULL) {
432
        return 0;
433
    }
434
    Py_ssize_t cap = list_capacity(ob_item);
435
    assert(cap != -1);
436
    if (!valid_index(i, cap)) {
437
        return 0;
438
    }
439
    PyObject *obj = _Py_atomic_load_ptr(&ob_item[i]);
440
    if (obj == NULL || !_Py_TryIncrefCompareStackRef(&ob_item[i], obj, result)) {
441
        return -1;
442
    }
443
    return 1;
444
}
445
#endif
446
447
int
448
PyList_SetItem(PyObject *op, Py_ssize_t i,
449
               PyObject *newitem)
450
5.97M
{
451
5.97M
    if (!PyList_Check(op)) {
452
0
        Py_XDECREF(newitem);
453
0
        PyErr_BadInternalCall();
454
0
        return -1;
455
0
    }
456
5.97M
    int ret;
457
5.97M
    PyListObject *self = ((PyListObject *)op);
458
5.97M
    Py_BEGIN_CRITICAL_SECTION(self);
459
5.97M
    if (!valid_index(i, Py_SIZE(self))) {
460
0
        Py_XDECREF(newitem);
461
0
        PyErr_SetString(PyExc_IndexError,
462
0
                        "list assignment index out of range");
463
0
        ret = -1;
464
0
        goto end;
465
0
    }
466
5.97M
    PyObject *tmp = self->ob_item[i];
467
5.97M
    FT_ATOMIC_STORE_PTR_RELEASE(self->ob_item[i], newitem);
468
5.97M
    Py_XDECREF(tmp);
469
5.97M
    ret = 0;
470
5.97M
end:;
471
5.97M
    Py_END_CRITICAL_SECTION();
472
5.97M
    return ret;
473
5.97M
}
474
475
static int
476
ins1(PyListObject *self, Py_ssize_t where, PyObject *v)
477
6.43k
{
478
6.43k
    Py_ssize_t i, n = Py_SIZE(self);
479
6.43k
    PyObject **items;
480
6.43k
    if (v == NULL) {
481
0
        PyErr_BadInternalCall();
482
0
        return -1;
483
0
    }
484
485
6.43k
    assert((size_t)n + 1 < PY_SSIZE_T_MAX);
486
6.43k
    if (list_resize(self, n+1) < 0)
487
0
        return -1;
488
489
6.43k
    if (where < 0) {
490
0
        where += n;
491
0
        if (where < 0)
492
0
            where = 0;
493
0
    }
494
6.43k
    if (where > n)
495
0
        where = n;
496
6.43k
    items = self->ob_item;
497
35.0k
    for (i = n; --i >= where; )
498
28.6k
        FT_ATOMIC_STORE_PTR_RELAXED(items[i+1], items[i]);
499
6.43k
    FT_ATOMIC_STORE_PTR_RELEASE(items[where], Py_NewRef(v));
500
6.43k
    return 0;
501
6.43k
}
502
503
int
504
PyList_Insert(PyObject *op, Py_ssize_t where, PyObject *newitem)
505
22
{
506
22
    if (!PyList_Check(op)) {
507
0
        PyErr_BadInternalCall();
508
0
        return -1;
509
0
    }
510
22
    PyListObject *self = (PyListObject *)op;
511
22
    int err;
512
22
    Py_BEGIN_CRITICAL_SECTION(self);
513
22
    err = ins1(self, where, newitem);
514
22
    Py_END_CRITICAL_SECTION();
515
22
    return err;
516
22
}
517
518
/* internal, used by _PyList_AppendTakeRef */
519
int
520
_PyList_AppendTakeRefListResize(PyListObject *self, PyObject *newitem)
521
14.5M
{
522
14.5M
    Py_ssize_t len = Py_SIZE(self);
523
14.5M
    assert(self->allocated == -1 || self->allocated == len);
524
14.5M
    if (list_resize(self, len + 1) < 0) {
525
0
        Py_DECREF(newitem);
526
0
        return -1;
527
0
    }
528
14.5M
    FT_ATOMIC_STORE_PTR_RELEASE(self->ob_item[len], newitem);
529
14.5M
    return 0;
530
14.5M
}
531
532
int
533
PyList_Append(PyObject *op, PyObject *newitem)
534
97.3M
{
535
97.3M
    if (PyList_Check(op) && (newitem != NULL)) {
536
97.3M
        int ret;
537
97.3M
        Py_BEGIN_CRITICAL_SECTION(op);
538
97.3M
        ret = _PyList_AppendTakeRef((PyListObject *)op, Py_NewRef(newitem));
539
97.3M
        Py_END_CRITICAL_SECTION();
540
97.3M
        return ret;
541
97.3M
    }
542
0
    PyErr_BadInternalCall();
543
0
    return -1;
544
97.3M
}
545
546
/* Methods */
547
548
static void
549
list_dealloc(PyObject *self)
550
58.6M
{
551
58.6M
    PyListObject *op = (PyListObject *)self;
552
58.6M
    Py_ssize_t i;
553
58.6M
    PyObject_GC_UnTrack(op);
554
58.6M
    if (op->ob_item != NULL) {
555
        /* Do it backwards, for Christian Tismer.
556
           There's a simple test case where somehow this reduces
557
           thrashing when a *very* large list is created and
558
           immediately deleted. */
559
15.2M
        i = Py_SIZE(op);
560
312M
        while (--i >= 0) {
561
297M
            Py_XDECREF(op->ob_item[i]);
562
297M
        }
563
15.2M
        free_list_items(op->ob_item, false);
564
15.2M
        op->ob_item = NULL;
565
15.2M
    }
566
58.6M
    if (PyList_CheckExact(op)) {
567
58.6M
        _Py_FREELIST_FREE(lists, op, PyObject_GC_Del);
568
58.6M
    }
569
1.37k
    else {
570
1.37k
        PyObject_GC_Del(op);
571
1.37k
    }
572
58.6M
}
573
574
static PyObject *
575
list_repr_impl(PyListObject *v)
576
0
{
577
0
    int res = Py_ReprEnter((PyObject*)v);
578
0
    if (res != 0) {
579
0
        return (res > 0 ? PyUnicode_FromString("[...]") : NULL);
580
0
    }
581
582
    /* "[" + "1" + ", 2" * (len - 1) + "]" */
583
0
    Py_ssize_t prealloc = 1 + 1 + (2 + 1) * (Py_SIZE(v) - 1) + 1;
584
0
    PyUnicodeWriter *writer = PyUnicodeWriter_Create(prealloc);
585
0
    PyObject *item = NULL;
586
0
    if (writer == NULL) {
587
0
        goto error;
588
0
    }
589
590
0
    if (PyUnicodeWriter_WriteChar(writer, '[') < 0) {
591
0
        goto error;
592
0
    }
593
594
    /* Do repr() on each element.  Note that this may mutate the list,
595
       so must refetch the list size on each iteration. */
596
0
    for (Py_ssize_t i = 0; i < Py_SIZE(v); ++i) {
597
        /* Hold a strong reference since repr(item) can mutate the list */
598
0
        item = Py_NewRef(v->ob_item[i]);
599
600
0
        if (i > 0) {
601
0
            if (PyUnicodeWriter_WriteChar(writer, ',') < 0) {
602
0
                goto error;
603
0
            }
604
0
            if (PyUnicodeWriter_WriteChar(writer, ' ') < 0) {
605
0
                goto error;
606
0
            }
607
0
        }
608
609
0
        if (PyUnicodeWriter_WriteRepr(writer, item) < 0) {
610
0
            goto error;
611
0
        }
612
0
        Py_CLEAR(item);
613
0
    }
614
615
0
    if (PyUnicodeWriter_WriteChar(writer, ']') < 0) {
616
0
        goto error;
617
0
    }
618
619
0
    Py_ReprLeave((PyObject *)v);
620
0
    return PyUnicodeWriter_Finish(writer);
621
622
0
error:
623
0
    Py_XDECREF(item);
624
0
    PyUnicodeWriter_Discard(writer);
625
0
    Py_ReprLeave((PyObject *)v);
626
0
    return NULL;
627
0
}
628
629
static PyObject *
630
list_repr(PyObject *self)
631
0
{
632
0
    if (PyList_GET_SIZE(self) == 0) {
633
0
        return PyUnicode_FromString("[]");
634
0
    }
635
0
    PyListObject *v = (PyListObject *)self;
636
0
    PyObject *ret = NULL;
637
0
    Py_BEGIN_CRITICAL_SECTION(v);
638
0
    ret = list_repr_impl(v);
639
0
    Py_END_CRITICAL_SECTION();
640
0
    return ret;
641
0
}
642
643
static Py_ssize_t
644
list_length(PyObject *a)
645
20.4M
{
646
20.4M
    return PyList_GET_SIZE(a);
647
20.4M
}
648
649
static int
650
list_contains(PyObject *aa, PyObject *el)
651
5.74k
{
652
653
26.6k
    for (Py_ssize_t i = 0; ; i++) {
654
26.6k
        PyObject *item = list_get_item_ref((PyListObject *)aa, i);
655
26.6k
        if (item == NULL) {
656
            // out-of-bounds
657
4.07k
            return 0;
658
4.07k
        }
659
22.5k
        int cmp = PyObject_RichCompareBool(item, el, Py_EQ);
660
22.5k
        Py_DECREF(item);
661
22.5k
        if (cmp != 0) {
662
1.66k
            return cmp;
663
1.66k
        }
664
22.5k
    }
665
0
    return 0;
666
5.74k
}
667
668
static PyObject *
669
list_item(PyObject *aa, Py_ssize_t i)
670
2.30M
{
671
2.30M
    PyListObject *a = (PyListObject *)aa;
672
2.30M
    if (!valid_index(i, PyList_GET_SIZE(a))) {
673
2.28M
        PyErr_SetObject(PyExc_IndexError, &_Py_STR(list_err));
674
2.28M
        return NULL;
675
2.28M
    }
676
16.4k
    PyObject *item;
677
#ifdef Py_GIL_DISABLED
678
    item = list_get_item_ref(a, i);
679
    if (item == NULL) {
680
        PyErr_SetObject(PyExc_IndexError, &_Py_STR(list_err));
681
        return NULL;
682
    }
683
#else
684
16.4k
    item = Py_NewRef(a->ob_item[i]);
685
16.4k
#endif
686
16.4k
    return item;
687
2.30M
}
688
689
static PyObject *
690
list_slice_lock_held(PyListObject *a, Py_ssize_t ilow, Py_ssize_t ihigh)
691
1.66M
{
692
1.66M
    PyListObject *np;
693
1.66M
    PyObject **src, **dest;
694
1.66M
    Py_ssize_t i, len;
695
1.66M
    len = ihigh - ilow;
696
1.66M
    if (len <= 0) {
697
0
        return PyList_New(0);
698
0
    }
699
1.66M
    np = (PyListObject *) list_new_prealloc(len);
700
1.66M
    if (np == NULL)
701
0
        return NULL;
702
703
1.66M
    src = a->ob_item + ilow;
704
1.66M
    dest = np->ob_item;
705
3.37M
    for (i = 0; i < len; i++) {
706
1.70M
        PyObject *v = src[i];
707
1.70M
        dest[i] = Py_NewRef(v);
708
1.70M
    }
709
1.66M
    Py_SET_SIZE(np, len);
710
1.66M
    return (PyObject *)np;
711
1.66M
}
712
713
PyObject *
714
PyList_GetSlice(PyObject *a, Py_ssize_t ilow, Py_ssize_t ihigh)
715
0
{
716
0
    if (!PyList_Check(a)) {
717
0
        PyErr_BadInternalCall();
718
0
        return NULL;
719
0
    }
720
0
    PyObject *ret;
721
0
    Py_BEGIN_CRITICAL_SECTION(a);
722
0
    if (ilow < 0) {
723
0
        ilow = 0;
724
0
    }
725
0
    else if (ilow > Py_SIZE(a)) {
726
0
        ilow = Py_SIZE(a);
727
0
    }
728
0
    if (ihigh < ilow) {
729
0
        ihigh = ilow;
730
0
    }
731
0
    else if (ihigh > Py_SIZE(a)) {
732
0
        ihigh = Py_SIZE(a);
733
0
    }
734
0
    ret = list_slice_lock_held((PyListObject *)a, ilow, ihigh);
735
0
    Py_END_CRITICAL_SECTION();
736
0
    return ret;
737
0
}
738
739
static PyObject *
740
list_concat_lock_held(PyListObject *a, PyListObject *b)
741
87
{
742
87
    Py_ssize_t size;
743
87
    Py_ssize_t i;
744
87
    PyObject **src, **dest;
745
87
    PyListObject *np;
746
87
    assert((size_t)Py_SIZE(a) + (size_t)Py_SIZE(b) < PY_SSIZE_T_MAX);
747
87
    size = Py_SIZE(a) + Py_SIZE(b);
748
87
    if (size == 0) {
749
0
        return PyList_New(0);
750
0
    }
751
87
    np = (PyListObject *) list_new_prealloc(size);
752
87
    if (np == NULL) {
753
0
        return NULL;
754
0
    }
755
87
    src = a->ob_item;
756
87
    dest = np->ob_item;
757
780
    for (i = 0; i < Py_SIZE(a); i++) {
758
693
        PyObject *v = src[i];
759
693
        dest[i] = Py_NewRef(v);
760
693
    }
761
87
    src = b->ob_item;
762
87
    dest = np->ob_item + Py_SIZE(a);
763
638
    for (i = 0; i < Py_SIZE(b); i++) {
764
551
        PyObject *v = src[i];
765
551
        dest[i] = Py_NewRef(v);
766
551
    }
767
87
    Py_SET_SIZE(np, size);
768
87
    return (PyObject *)np;
769
87
}
770
771
static PyObject *
772
list_concat(PyObject *aa, PyObject *bb)
773
87
{
774
87
    if (!PyList_Check(bb)) {
775
0
        PyErr_Format(PyExc_TypeError,
776
0
                  "can only concatenate list (not \"%.200s\") to list",
777
0
                  Py_TYPE(bb)->tp_name);
778
0
        return NULL;
779
0
    }
780
87
    PyListObject *a = (PyListObject *)aa;
781
87
    PyListObject *b = (PyListObject *)bb;
782
87
    PyObject *ret;
783
87
    Py_BEGIN_CRITICAL_SECTION2(a, b);
784
87
    ret = list_concat_lock_held(a, b);
785
87
    Py_END_CRITICAL_SECTION2();
786
87
    return ret;
787
87
}
788
789
static PyObject *
790
list_repeat_lock_held(PyListObject *a, Py_ssize_t n)
791
3.00k
{
792
3.00k
    const Py_ssize_t input_size = Py_SIZE(a);
793
3.00k
    if (input_size == 0 || n <= 0)
794
0
        return PyList_New(0);
795
3.00k
    assert(n > 0);
796
797
3.00k
    if (input_size > PY_SSIZE_T_MAX / n)
798
0
        return PyErr_NoMemory();
799
3.00k
    Py_ssize_t output_size = input_size * n;
800
801
3.00k
    PyListObject *np = (PyListObject *) list_new_prealloc(output_size);
802
3.00k
    if (np == NULL)
803
0
        return NULL;
804
805
3.00k
    PyObject **dest = np->ob_item;
806
3.00k
    if (input_size == 1) {
807
3.00k
        PyObject *elem = a->ob_item[0];
808
3.00k
        _Py_RefcntAdd(elem, n);
809
3.00k
        PyObject **dest_end = dest + output_size;
810
1.53M
        while (dest < dest_end) {
811
1.53M
            *dest++ = elem;
812
1.53M
        }
813
3.00k
    }
814
0
    else {
815
0
        PyObject **src = a->ob_item;
816
0
        PyObject **src_end = src + input_size;
817
0
        while (src < src_end) {
818
0
            _Py_RefcntAdd(*src, n);
819
0
            *dest++ = *src++;
820
0
        }
821
        // TODO: _Py_memory_repeat calls are not safe for shared lists in
822
        // GIL_DISABLED builds. (See issue #129069)
823
0
        _Py_memory_repeat((char *)np->ob_item, sizeof(PyObject *)*output_size,
824
0
                                        sizeof(PyObject *)*input_size);
825
0
    }
826
827
3.00k
    Py_SET_SIZE(np, output_size);
828
3.00k
    return (PyObject *) np;
829
3.00k
}
830
831
static PyObject *
832
list_repeat(PyObject *aa, Py_ssize_t n)
833
3.00k
{
834
3.00k
    PyObject *ret;
835
3.00k
    PyListObject *a = (PyListObject *)aa;
836
3.00k
    Py_BEGIN_CRITICAL_SECTION(a);
837
3.00k
    ret = list_repeat_lock_held(a, n);
838
3.00k
    Py_END_CRITICAL_SECTION();
839
3.00k
    return ret;
840
3.00k
}
841
842
static void
843
list_clear_impl(PyListObject *a, bool is_resize)
844
16.9k
{
845
16.9k
    PyObject **items = a->ob_item;
846
16.9k
    if (items == NULL) {
847
0
        return;
848
0
    }
849
850
    /* Because XDECREF can recursively invoke operations on
851
       this list, we make it empty first. */
852
16.9k
    Py_ssize_t i = Py_SIZE(a);
853
16.9k
    Py_SET_SIZE(a, 0);
854
16.9k
    FT_ATOMIC_STORE_PTR_RELEASE(a->ob_item, NULL);
855
16.9k
    a->allocated = 0;
856
34.0k
    while (--i >= 0) {
857
17.0k
        Py_XDECREF(items[i]);
858
17.0k
    }
859
#ifdef Py_GIL_DISABLED
860
    if (is_resize) {
861
        ensure_shared_on_resize(a);
862
    }
863
    bool use_qsbr = is_resize && _PyObject_GC_IS_SHARED(a);
864
#else
865
16.9k
    bool use_qsbr = false;
866
16.9k
#endif
867
16.9k
    free_list_items(items, use_qsbr);
868
    // Note that there is no guarantee that the list is actually empty
869
    // at this point, because XDECREF may have populated it indirectly again!
870
16.9k
}
871
872
static void
873
list_clear(PyListObject *a)
874
16.8k
{
875
16.8k
    list_clear_impl(a, true);
876
16.8k
}
877
878
static int
879
list_clear_slot(PyObject *self)
880
58
{
881
58
    list_clear_impl((PyListObject *)self, false);
882
58
    return 0;
883
58
}
884
885
/* a[ilow:ihigh] = v if v != NULL.
886
 * del a[ilow:ihigh] if v == NULL.
887
 *
888
 * Special speed gimmick:  when v is NULL and ihigh - ilow <= 8, it's
889
 * guaranteed the call cannot fail.
890
 */
891
static int
892
list_ass_slice_lock_held(PyListObject *a, Py_ssize_t ilow, Py_ssize_t ihigh, PyObject *v)
893
458k
{
894
    /* Because [X]DECREF can recursively invoke list operations on
895
       this list, we must postpone all [X]DECREF activity until
896
       after the list is back in its canonical shape.  Therefore
897
       we must allocate an additional array, 'recycle', into which
898
       we temporarily copy the items that are deleted from the
899
       list. :-( */
900
458k
    PyObject *recycle_on_stack[8];
901
458k
    PyObject **recycle = recycle_on_stack; /* will allocate more if needed */
902
458k
    PyObject **item;
903
458k
    PyObject **vitem = NULL;
904
458k
    PyObject *v_as_SF = NULL; /* PySequence_Fast(v) */
905
458k
    Py_ssize_t n; /* # of elements in replacement list */
906
458k
    Py_ssize_t norig; /* # of elements in list getting replaced */
907
458k
    Py_ssize_t d; /* Change in size */
908
458k
    Py_ssize_t k;
909
458k
    size_t s;
910
458k
    int result = -1;            /* guilty until proved innocent */
911
458k
#define b ((PyListObject *)v)
912
458k
    if (v == NULL)
913
86.9k
        n = 0;
914
371k
    else {
915
371k
        v_as_SF = PySequence_Fast(v, "can only assign an iterable");
916
371k
        if(v_as_SF == NULL)
917
0
            goto Error;
918
371k
        n = PySequence_Fast_GET_SIZE(v_as_SF);
919
371k
        vitem = PySequence_Fast_ITEMS(v_as_SF);
920
371k
    }
921
458k
    if (ilow < 0)
922
0
        ilow = 0;
923
458k
    else if (ilow > Py_SIZE(a))
924
353k
        ilow = Py_SIZE(a);
925
926
458k
    if (ihigh < ilow)
927
0
        ihigh = ilow;
928
458k
    else if (ihigh > Py_SIZE(a))
929
353k
        ihigh = Py_SIZE(a);
930
931
458k
    norig = ihigh - ilow;
932
458k
    assert(norig >= 0);
933
458k
    d = n - norig;
934
458k
    if (Py_SIZE(a) + d == 0) {
935
15.5k
        Py_XDECREF(v_as_SF);
936
15.5k
        list_clear(a);
937
15.5k
        return 0;
938
15.5k
    }
939
443k
    item = a->ob_item;
940
    /* recycle the items that we are about to remove */
941
443k
    s = norig * sizeof(PyObject *);
942
    /* If norig == 0, item might be NULL, in which case we may not memcpy from it. */
943
443k
    if (s) {
944
89.8k
        if (s > sizeof(recycle_on_stack)) {
945
2.33k
            recycle = (PyObject **)PyMem_Malloc(s);
946
2.33k
            if (recycle == NULL) {
947
0
                PyErr_NoMemory();
948
0
                goto Error;
949
0
            }
950
2.33k
        }
951
89.8k
        memcpy(recycle, &item[ilow], s);
952
89.8k
    }
953
954
443k
    if (d < 0) { /* Delete -d items */
955
82.6k
        Py_ssize_t tail;
956
82.6k
        tail = (Py_SIZE(a) - ihigh) * sizeof(PyObject *);
957
        // TODO: these memmove/memcpy calls are not safe for shared lists in
958
        // GIL_DISABLED builds. (See issue #129069)
959
82.6k
        memmove(&item[ihigh+d], &item[ihigh], tail);
960
82.6k
        if (list_resize(a, Py_SIZE(a) + d) < 0) {
961
0
            memmove(&item[ihigh], &item[ihigh+d], tail);
962
0
            memcpy(&item[ilow], recycle, s);
963
0
            goto Error;
964
0
        }
965
82.6k
        item = a->ob_item;
966
82.6k
    }
967
360k
    else if (d > 0) { /* Insert d items */
968
359k
        k = Py_SIZE(a);
969
359k
        if (list_resize(a, k+d) < 0)
970
0
            goto Error;
971
359k
        item = a->ob_item;
972
        // TODO: these memmove/memcpy calls are not safe for shared lists in
973
        // GIL_DISABLED builds. (See issue #129069)
974
359k
        memmove(&item[ihigh+d], &item[ihigh],
975
359k
            (k - ihigh)*sizeof(PyObject *));
976
359k
    }
977
80.0M
    for (k = 0; k < n; k++, ilow++) {
978
79.6M
        PyObject *w = vitem[k];
979
79.6M
        FT_ATOMIC_STORE_PTR_RELEASE(item[ilow], Py_XNewRef(w));
980
79.6M
    }
981
683k
    for (k = norig - 1; k >= 0; --k)
982
240k
        Py_XDECREF(recycle[k]);
983
443k
    result = 0;
984
443k
 Error:
985
443k
    if (recycle != recycle_on_stack)
986
2.33k
        PyMem_Free(recycle);
987
443k
    Py_XDECREF(v_as_SF);
988
443k
    return result;
989
443k
#undef b
990
443k
}
991
992
static int
993
list_ass_slice(PyListObject *a, Py_ssize_t ilow, Py_ssize_t ihigh, PyObject *v)
994
442k
{
995
442k
    int ret;
996
442k
    if (a == (PyListObject *)v) {
997
0
        Py_BEGIN_CRITICAL_SECTION(a);
998
0
        Py_ssize_t n = PyList_GET_SIZE(a);
999
0
        PyObject *copy = list_slice_lock_held(a, 0, n);
1000
0
        if (copy == NULL) {
1001
0
            ret = -1;
1002
0
        }
1003
0
        else {
1004
0
            ret = list_ass_slice_lock_held(a, ilow, ihigh, copy);
1005
0
            Py_DECREF(copy);
1006
0
        }
1007
0
        Py_END_CRITICAL_SECTION();
1008
0
    }
1009
442k
    else if (v != NULL && PyList_CheckExact(v)) {
1010
202k
        Py_BEGIN_CRITICAL_SECTION2(a, v);
1011
202k
        ret = list_ass_slice_lock_held(a, ilow, ihigh, v);
1012
202k
        Py_END_CRITICAL_SECTION2();
1013
202k
    }
1014
239k
    else {
1015
239k
        Py_BEGIN_CRITICAL_SECTION(a);
1016
239k
        ret = list_ass_slice_lock_held(a, ilow, ihigh, v);
1017
239k
        Py_END_CRITICAL_SECTION();
1018
239k
    }
1019
442k
    return ret;
1020
442k
}
1021
1022
int
1023
PyList_SetSlice(PyObject *a, Py_ssize_t ilow, Py_ssize_t ihigh, PyObject *v)
1024
442k
{
1025
442k
    if (!PyList_Check(a)) {
1026
0
        PyErr_BadInternalCall();
1027
0
        return -1;
1028
0
    }
1029
442k
    return list_ass_slice((PyListObject *)a, ilow, ihigh, v);
1030
442k
}
1031
1032
static int
1033
list_inplace_repeat_lock_held(PyListObject *self, Py_ssize_t n)
1034
0
{
1035
0
    Py_ssize_t input_size = PyList_GET_SIZE(self);
1036
0
    if (input_size == 0 || n == 1) {
1037
0
        return 0;
1038
0
    }
1039
1040
0
    if (n < 1) {
1041
0
        list_clear(self);
1042
0
        return 0;
1043
0
    }
1044
1045
0
    if (input_size > PY_SSIZE_T_MAX / n) {
1046
0
        PyErr_NoMemory();
1047
0
        return -1;
1048
0
    }
1049
0
    Py_ssize_t output_size = input_size * n;
1050
1051
0
    if (list_resize(self, output_size) < 0) {
1052
0
        return -1;
1053
0
    }
1054
1055
0
    PyObject **items = self->ob_item;
1056
0
    for (Py_ssize_t j = 0; j < input_size; j++) {
1057
0
        _Py_RefcntAdd(items[j], n-1);
1058
0
    }
1059
    // TODO: _Py_memory_repeat calls are not safe for shared lists in
1060
    // GIL_DISABLED builds. (See issue #129069)
1061
0
    _Py_memory_repeat((char *)items, sizeof(PyObject *)*output_size,
1062
0
                      sizeof(PyObject *)*input_size);
1063
0
    return 0;
1064
0
}
1065
1066
static PyObject *
1067
list_inplace_repeat(PyObject *_self, Py_ssize_t n)
1068
0
{
1069
0
    PyObject *ret;
1070
0
    PyListObject *self = (PyListObject *) _self;
1071
0
    Py_BEGIN_CRITICAL_SECTION(self);
1072
0
    if (list_inplace_repeat_lock_held(self, n) < 0) {
1073
0
        ret = NULL;
1074
0
    }
1075
0
    else {
1076
0
        ret = Py_NewRef(self);
1077
0
    }
1078
0
    Py_END_CRITICAL_SECTION();
1079
0
    return ret;
1080
0
}
1081
1082
static int
1083
list_ass_item_lock_held(PyListObject *a, Py_ssize_t i, PyObject *v)
1084
1.70M
{
1085
1.70M
    if (!valid_index(i, Py_SIZE(a))) {
1086
0
        PyErr_SetString(PyExc_IndexError,
1087
0
                        "list assignment index out of range");
1088
0
        return -1;
1089
0
    }
1090
1.70M
    PyObject *tmp = a->ob_item[i];
1091
1.70M
    if (v == NULL) {
1092
38.1k
        Py_ssize_t size = Py_SIZE(a);
1093
2.01M
        for (Py_ssize_t idx = i; idx < size - 1; idx++) {
1094
1.97M
            FT_ATOMIC_STORE_PTR_RELAXED(a->ob_item[idx], a->ob_item[idx + 1]);
1095
1.97M
        }
1096
38.1k
        Py_SET_SIZE(a, size - 1);
1097
38.1k
    }
1098
1.66M
    else {
1099
1.66M
        FT_ATOMIC_STORE_PTR_RELEASE(a->ob_item[i], Py_NewRef(v));
1100
1.66M
    }
1101
1.70M
    Py_DECREF(tmp);
1102
1.70M
    return 0;
1103
1.70M
}
1104
1105
static int
1106
list_ass_item(PyObject *aa, Py_ssize_t i, PyObject *v)
1107
30.0k
{
1108
30.0k
    int ret;
1109
30.0k
    PyListObject *a = (PyListObject *)aa;
1110
30.0k
    Py_BEGIN_CRITICAL_SECTION(a);
1111
30.0k
    ret = list_ass_item_lock_held(a, i, v);
1112
30.0k
    Py_END_CRITICAL_SECTION();
1113
30.0k
    return ret;
1114
30.0k
}
1115
1116
/*[clinic input]
1117
@critical_section
1118
list.insert
1119
1120
    index: Py_ssize_t
1121
    object: object
1122
    /
1123
1124
Insert object before index.
1125
[clinic start generated code]*/
1126
1127
static PyObject *
1128
list_insert_impl(PyListObject *self, Py_ssize_t index, PyObject *object)
1129
/*[clinic end generated code: output=7f35e32f60c8cb78 input=b1987ca998a4ae2d]*/
1130
6.41k
{
1131
6.41k
    if (ins1(self, index, object) == 0) {
1132
6.41k
        Py_RETURN_NONE;
1133
6.41k
    }
1134
0
    return NULL;
1135
6.41k
}
1136
1137
/*[clinic input]
1138
@critical_section
1139
list.clear as py_list_clear
1140
1141
Remove all items from list.
1142
[clinic start generated code]*/
1143
1144
static PyObject *
1145
py_list_clear_impl(PyListObject *self)
1146
/*[clinic end generated code: output=83726743807e3518 input=e285b7f09051a9ba]*/
1147
0
{
1148
0
    list_clear(self);
1149
0
    Py_RETURN_NONE;
1150
0
}
1151
1152
/*[clinic input]
1153
@critical_section
1154
list.copy
1155
1156
Return a shallow copy of the list.
1157
[clinic start generated code]*/
1158
1159
static PyObject *
1160
list_copy_impl(PyListObject *self)
1161
/*[clinic end generated code: output=ec6b72d6209d418e input=81c54b0c7bb4f73d]*/
1162
0
{
1163
0
    return list_slice_lock_held(self, 0, Py_SIZE(self));
1164
0
}
1165
1166
/*[clinic input]
1167
@critical_section
1168
list.append
1169
1170
     object: object
1171
     /
1172
1173
Append object to the end of the list.
1174
[clinic start generated code]*/
1175
1176
static PyObject *
1177
list_append_impl(PyListObject *self, PyObject *object)
1178
/*[clinic end generated code: output=78423561d92ed405 input=122b0853de54004f]*/
1179
24.0M
{
1180
24.0M
    if (_PyList_AppendTakeRef(self, Py_NewRef(object)) < 0) {
1181
0
        return NULL;
1182
0
    }
1183
24.0M
    Py_RETURN_NONE;
1184
24.0M
}
1185
1186
static int
1187
list_extend_fast(PyListObject *self, PyObject *iterable)
1188
313k
{
1189
313k
    Py_ssize_t n = PySequence_Fast_GET_SIZE(iterable);
1190
313k
    if (n == 0) {
1191
        /* short circuit when iterable is empty */
1192
177k
        return 0;
1193
177k
    }
1194
1195
136k
    Py_ssize_t m = Py_SIZE(self);
1196
    // It should not be possible to allocate a list large enough to cause
1197
    // an overflow on any relevant platform.
1198
136k
    assert(m < PY_SSIZE_T_MAX - n);
1199
136k
    if (self->ob_item == NULL) {
1200
68.3k
        if (list_preallocate_exact(self, n) < 0) {
1201
0
            return -1;
1202
0
        }
1203
68.3k
        Py_SET_SIZE(self, n);
1204
68.3k
    }
1205
67.7k
    else if (list_resize(self, m + n) < 0) {
1206
0
        return -1;
1207
0
    }
1208
1209
    // note that we may still have self == iterable here for the
1210
    // situation a.extend(a), but the following code works
1211
    // in that case too.  Just make sure to resize self
1212
    // before calling PySequence_Fast_ITEMS.
1213
    //
1214
    // populate the end of self with iterable's items.
1215
136k
    PyObject **src = PySequence_Fast_ITEMS(iterable);
1216
136k
    PyObject **dest = self->ob_item + m;
1217
3.01M
    for (Py_ssize_t i = 0; i < n; i++) {
1218
2.87M
        PyObject *o = src[i];
1219
2.87M
        FT_ATOMIC_STORE_PTR_RELEASE(dest[i], Py_NewRef(o));
1220
2.87M
    }
1221
136k
    return 0;
1222
136k
}
1223
1224
static int
1225
list_extend_iter_lock_held(PyListObject *self, PyObject *iterable)
1226
434k
{
1227
434k
    PyObject *it = PyObject_GetIter(iterable);
1228
434k
    if (it == NULL) {
1229
0
        return -1;
1230
0
    }
1231
434k
    PyObject *(*iternext)(PyObject *) = *Py_TYPE(it)->tp_iternext;
1232
1233
    /* Guess a result list size. */
1234
434k
    Py_ssize_t n = PyObject_LengthHint(iterable, 8);
1235
434k
    if (n < 0) {
1236
0
        Py_DECREF(it);
1237
0
        return -1;
1238
0
    }
1239
1240
434k
    Py_ssize_t m = Py_SIZE(self);
1241
434k
    if (m > PY_SSIZE_T_MAX - n) {
1242
        /* m + n overflowed; on the chance that n lied, and there really
1243
         * is enough room, ignore it.  If n was telling the truth, we'll
1244
         * eventually run out of memory during the loop.
1245
         */
1246
0
    }
1247
434k
    else if (self->ob_item == NULL) {
1248
434k
        if (n && list_preallocate_exact(self, n) < 0)
1249
0
            goto error;
1250
434k
    }
1251
196
    else {
1252
        /* Make room. */
1253
196
        if (list_resize(self, m + n) < 0) {
1254
0
            goto error;
1255
0
        }
1256
1257
        /* Make the list sane again. */
1258
196
        Py_SET_SIZE(self, m);
1259
196
    }
1260
1261
    /* Run iterator to exhaustion. */
1262
75.7M
    for (;;) {
1263
75.7M
        PyObject *item = iternext(it);
1264
75.7M
        if (item == NULL) {
1265
434k
            if (PyErr_Occurred()) {
1266
0
                if (PyErr_ExceptionMatches(PyExc_StopIteration))
1267
0
                    PyErr_Clear();
1268
0
                else
1269
0
                    goto error;
1270
0
            }
1271
434k
            break;
1272
434k
        }
1273
1274
75.3M
        if (Py_SIZE(self) < self->allocated) {
1275
75.3M
            Py_ssize_t len = Py_SIZE(self);
1276
75.3M
            FT_ATOMIC_STORE_PTR_RELEASE(self->ob_item[len], item);  // steals item ref
1277
75.3M
            Py_SET_SIZE(self, len + 1);
1278
75.3M
        }
1279
0
        else {
1280
0
            if (_PyList_AppendTakeRef(self, item) < 0)
1281
0
                goto error;
1282
0
        }
1283
75.3M
    }
1284
1285
    /* Cut back result list if initial guess was too large. */
1286
434k
    if (Py_SIZE(self) < self->allocated) {
1287
67.4k
        if (list_resize(self, Py_SIZE(self)) < 0)
1288
0
            goto error;
1289
67.4k
    }
1290
1291
434k
    Py_DECREF(it);
1292
434k
    return 0;
1293
1294
0
  error:
1295
0
    Py_DECREF(it);
1296
0
    return -1;
1297
434k
}
1298
1299
static int
1300
list_extend_lock_held(PyListObject *self, PyObject *iterable)
1301
313k
{
1302
313k
    PyObject *seq = PySequence_Fast(iterable, "argument must be iterable");
1303
313k
    if (!seq) {
1304
0
        return -1;
1305
0
    }
1306
1307
313k
    int res = list_extend_fast(self, seq);
1308
313k
    Py_DECREF(seq);
1309
313k
    return res;
1310
313k
}
1311
1312
static int
1313
list_extend_set(PyListObject *self, PySetObject *other)
1314
38.5k
{
1315
38.5k
    Py_ssize_t m = Py_SIZE(self);
1316
38.5k
    Py_ssize_t n = PySet_GET_SIZE(other);
1317
38.5k
    Py_ssize_t r = m + n;
1318
38.5k
    if (r == 0) {
1319
7.05k
        return 0;
1320
7.05k
    }
1321
31.4k
    if (list_resize(self, r) < 0) {
1322
0
        return -1;
1323
0
    }
1324
1325
31.4k
    assert(self->ob_item != NULL);
1326
    /* populate the end of self with iterable's items */
1327
31.4k
    Py_ssize_t setpos = 0;
1328
31.4k
    Py_hash_t hash;
1329
31.4k
    PyObject *key;
1330
31.4k
    PyObject **dest = self->ob_item + m;
1331
180k
    while (_PySet_NextEntryRef((PyObject *)other, &setpos, &key, &hash)) {
1332
149k
        FT_ATOMIC_STORE_PTR_RELEASE(*dest, key);
1333
149k
        dest++;
1334
149k
    }
1335
31.4k
    Py_SET_SIZE(self, r);
1336
31.4k
    return 0;
1337
31.4k
}
1338
1339
static int
1340
list_extend_dict(PyListObject *self, PyDictObject *dict, int which_item)
1341
325k
{
1342
    // which_item: 0 for keys and 1 for values
1343
325k
    Py_ssize_t m = Py_SIZE(self);
1344
325k
    Py_ssize_t n = PyDict_GET_SIZE(dict);
1345
325k
    Py_ssize_t r = m + n;
1346
325k
    if (r == 0) {
1347
0
        return 0;
1348
0
    }
1349
325k
    if (list_resize(self, r) < 0) {
1350
0
        return -1;
1351
0
    }
1352
1353
325k
    assert(self->ob_item != NULL);
1354
325k
    PyObject **dest = self->ob_item + m;
1355
325k
    Py_ssize_t pos = 0;
1356
325k
    PyObject *keyvalue[2];
1357
1.18M
    while (_PyDict_Next((PyObject *)dict, &pos, &keyvalue[0], &keyvalue[1], NULL)) {
1358
860k
        PyObject *obj = keyvalue[which_item];
1359
860k
        Py_INCREF(obj);
1360
860k
        FT_ATOMIC_STORE_PTR_RELEASE(*dest, obj);
1361
860k
        dest++;
1362
860k
    }
1363
1364
325k
    Py_SET_SIZE(self, r);
1365
325k
    return 0;
1366
325k
}
1367
1368
static int
1369
list_extend_dictitems(PyListObject *self, PyDictObject *dict)
1370
4
{
1371
4
    Py_ssize_t m = Py_SIZE(self);
1372
4
    Py_ssize_t n = PyDict_GET_SIZE(dict);
1373
4
    Py_ssize_t r = m + n;
1374
4
    if (r == 0) {
1375
0
        return 0;
1376
0
    }
1377
4
    if (list_resize(self, r) < 0) {
1378
0
        return -1;
1379
0
    }
1380
1381
4
    assert(self->ob_item != NULL);
1382
4
    PyObject **dest = self->ob_item + m;
1383
4
    Py_ssize_t pos = 0;
1384
4
    Py_ssize_t i = 0;
1385
4
    PyObject *key_value[2];
1386
180
    while (_PyDict_Next((PyObject *)dict, &pos, &key_value[0], &key_value[1], NULL)) {
1387
176
        PyObject *item = PyTuple_FromArray(key_value, 2);
1388
176
        if (item == NULL) {
1389
0
            Py_SET_SIZE(self, m + i);
1390
0
            return -1;
1391
0
        }
1392
176
        FT_ATOMIC_STORE_PTR_RELEASE(*dest, item);
1393
176
        dest++;
1394
176
        i++;
1395
176
    }
1396
1397
4
    Py_SET_SIZE(self, r);
1398
4
    return 0;
1399
4
}
1400
1401
static int
1402
_list_extend(PyListObject *self, PyObject *iterable)
1403
1.11M
{
1404
    // Special case:
1405
    // lists and tuples which can use PySequence_Fast ops
1406
1.11M
    int res = -1;
1407
1.11M
    if ((PyObject *)self == iterable) {
1408
0
        Py_BEGIN_CRITICAL_SECTION(self);
1409
0
        res = list_inplace_repeat_lock_held(self, 2);
1410
0
        Py_END_CRITICAL_SECTION();
1411
0
    }
1412
1.11M
    else if (PyList_CheckExact(iterable)) {
1413
312k
        Py_BEGIN_CRITICAL_SECTION2(self, iterable);
1414
312k
        res = list_extend_lock_held(self, iterable);
1415
312k
        Py_END_CRITICAL_SECTION2();
1416
312k
    }
1417
800k
    else if (PyTuple_CheckExact(iterable)) {
1418
1.55k
        Py_BEGIN_CRITICAL_SECTION(self);
1419
1.55k
        res = list_extend_lock_held(self, iterable);
1420
1.55k
        Py_END_CRITICAL_SECTION();
1421
1.55k
    }
1422
798k
    else if (PyAnySet_CheckExact(iterable)) {
1423
38.5k
        Py_BEGIN_CRITICAL_SECTION2(self, iterable);
1424
38.5k
        res = list_extend_set(self, (PySetObject *)iterable);
1425
38.5k
        Py_END_CRITICAL_SECTION2();
1426
38.5k
    }
1427
760k
    else if (PyDict_CheckExact(iterable)) {
1428
325k
        Py_BEGIN_CRITICAL_SECTION2(self, iterable);
1429
325k
        res = list_extend_dict(self, (PyDictObject *)iterable, 0 /*keys*/);
1430
325k
        Py_END_CRITICAL_SECTION2();
1431
325k
    }
1432
434k
    else if (Py_IS_TYPE(iterable, &PyDictKeys_Type)) {
1433
0
        PyDictObject *dict = ((_PyDictViewObject *)iterable)->dv_dict;
1434
0
        Py_BEGIN_CRITICAL_SECTION2(self, dict);
1435
0
        res = list_extend_dict(self, dict, 0 /*keys*/);
1436
0
        Py_END_CRITICAL_SECTION2();
1437
0
    }
1438
434k
    else if (Py_IS_TYPE(iterable, &PyDictValues_Type)) {
1439
3
        PyDictObject *dict = ((_PyDictViewObject *)iterable)->dv_dict;
1440
3
        Py_BEGIN_CRITICAL_SECTION2(self, dict);
1441
3
        res = list_extend_dict(self, dict, 1 /*values*/);
1442
3
        Py_END_CRITICAL_SECTION2();
1443
3
    }
1444
434k
    else if (Py_IS_TYPE(iterable, &PyDictItems_Type)) {
1445
4
        PyDictObject *dict = ((_PyDictViewObject *)iterable)->dv_dict;
1446
4
        Py_BEGIN_CRITICAL_SECTION2(self, dict);
1447
4
        res = list_extend_dictitems(self, dict);
1448
4
        Py_END_CRITICAL_SECTION2();
1449
4
    }
1450
434k
    else {
1451
434k
        Py_BEGIN_CRITICAL_SECTION(self);
1452
434k
        res = list_extend_iter_lock_held(self, iterable);
1453
434k
        Py_END_CRITICAL_SECTION();
1454
434k
    }
1455
1.11M
    return res;
1456
1.11M
}
1457
1458
/*[clinic input]
1459
list.extend as list_extend
1460
1461
     iterable: object
1462
     /
1463
1464
Extend list by appending elements from the iterable.
1465
[clinic start generated code]*/
1466
1467
static PyObject *
1468
list_extend_impl(PyListObject *self, PyObject *iterable)
1469
/*[clinic end generated code: output=b0eba9e0b186d5ce input=979da7597a515791]*/
1470
577k
{
1471
577k
    if (_list_extend(self, iterable) < 0) {
1472
0
        return NULL;
1473
0
    }
1474
577k
    Py_RETURN_NONE;
1475
577k
}
1476
1477
PyObject *
1478
_PyList_Extend(PyListObject *self, PyObject *iterable)
1479
474k
{
1480
474k
    return list_extend((PyObject*)self, iterable);
1481
474k
}
1482
1483
int
1484
PyList_Extend(PyObject *self, PyObject *iterable)
1485
0
{
1486
0
    if (!PyList_Check(self)) {
1487
0
        PyErr_BadInternalCall();
1488
0
        return -1;
1489
0
    }
1490
0
    return _list_extend((PyListObject*)self, iterable);
1491
0
}
1492
1493
1494
int
1495
PyList_Clear(PyObject *self)
1496
0
{
1497
0
    if (!PyList_Check(self)) {
1498
0
        PyErr_BadInternalCall();
1499
0
        return -1;
1500
0
    }
1501
0
    Py_BEGIN_CRITICAL_SECTION(self);
1502
0
    list_clear((PyListObject*)self);
1503
0
    Py_END_CRITICAL_SECTION();
1504
0
    return 0;
1505
0
}
1506
1507
1508
static PyObject *
1509
list_inplace_concat(PyObject *_self, PyObject *other)
1510
208k
{
1511
208k
    PyListObject *self = (PyListObject *)_self;
1512
208k
    if (_list_extend(self, other) < 0) {
1513
0
        return NULL;
1514
0
    }
1515
208k
    return Py_NewRef(self);
1516
208k
}
1517
1518
/*[clinic input]
1519
@critical_section
1520
list.pop
1521
1522
    index: Py_ssize_t = -1
1523
    /
1524
1525
Remove and return item at index (default last).
1526
1527
Raises IndexError if list is empty or index is out of range.
1528
[clinic start generated code]*/
1529
1530
static PyObject *
1531
list_pop_impl(PyListObject *self, Py_ssize_t index)
1532
/*[clinic end generated code: output=6bd69dcb3f17eca8 input=c269141068ae4b8f]*/
1533
1.58k
{
1534
1.58k
    PyObject *v;
1535
1.58k
    int status;
1536
1537
1.58k
    if (Py_SIZE(self) == 0) {
1538
        /* Special-case most common failure cause */
1539
0
        PyErr_SetString(PyExc_IndexError, "pop from empty list");
1540
0
        return NULL;
1541
0
    }
1542
1.58k
    if (index < 0)
1543
1.58k
        index += Py_SIZE(self);
1544
1.58k
    if (!valid_index(index, Py_SIZE(self))) {
1545
0
        PyErr_SetString(PyExc_IndexError, "pop index out of range");
1546
0
        return NULL;
1547
0
    }
1548
1549
1.58k
    PyObject **items = self->ob_item;
1550
1.58k
    v = items[index];
1551
1.58k
    const Py_ssize_t size_after_pop = Py_SIZE(self) - 1;
1552
1.58k
    if (size_after_pop == 0) {
1553
1.39k
        Py_INCREF(v);
1554
1.39k
        list_clear(self);
1555
1.39k
        status = 0;
1556
1.39k
    }
1557
193
    else {
1558
193
        if ((size_after_pop - index) > 0) {
1559
0
            memmove(&items[index], &items[index+1], (size_after_pop - index) * sizeof(PyObject *));
1560
0
        }
1561
193
        status = list_resize(self, size_after_pop);
1562
193
    }
1563
1.58k
    if (status >= 0) {
1564
1.58k
        return v; // and v now owns the reference the list had
1565
1.58k
    }
1566
0
    else {
1567
        // list resize failed, need to restore
1568
0
        memmove(&items[index+1], &items[index], (size_after_pop - index)* sizeof(PyObject *));
1569
0
        items[index] = v;
1570
0
        return NULL;
1571
0
    }
1572
1.58k
}
1573
1574
/* Reverse a slice of a list in place, from lo up to (exclusive) hi. */
1575
static void
1576
reverse_slice(PyObject **lo, PyObject **hi)
1577
42.1k
{
1578
42.1k
    assert(lo && hi);
1579
1580
42.1k
    --hi;
1581
85.3k
    while (lo < hi) {
1582
43.2k
        PyObject *t = *lo;
1583
43.2k
        *lo = *hi;
1584
43.2k
        *hi = t;
1585
43.2k
        ++lo;
1586
43.2k
        --hi;
1587
43.2k
    }
1588
42.1k
}
1589
1590
/* Lots of code for an adaptive, stable, natural mergesort.  There are many
1591
 * pieces to this algorithm; read listsort.txt for overviews and details.
1592
 */
1593
1594
/* A sortslice contains a pointer to an array of keys and a pointer to
1595
 * an array of corresponding values.  In other words, keys[i]
1596
 * corresponds with values[i].  If values == NULL, then the keys are
1597
 * also the values.
1598
 *
1599
 * Several convenience routines are provided here, so that keys and
1600
 * values are always moved in sync.
1601
 */
1602
1603
typedef struct {
1604
    PyObject **keys;
1605
    PyObject **values;
1606
} sortslice;
1607
1608
Py_LOCAL_INLINE(void)
1609
sortslice_copy(sortslice *s1, Py_ssize_t i, sortslice *s2, Py_ssize_t j)
1610
579
{
1611
579
    s1->keys[i] = s2->keys[j];
1612
579
    if (s1->values != NULL)
1613
0
        s1->values[i] = s2->values[j];
1614
579
}
1615
1616
Py_LOCAL_INLINE(void)
1617
sortslice_copy_incr(sortslice *dst, sortslice *src)
1618
61.4k
{
1619
61.4k
    *dst->keys++ = *src->keys++;
1620
61.4k
    if (dst->values != NULL)
1621
0
        *dst->values++ = *src->values++;
1622
61.4k
}
1623
1624
Py_LOCAL_INLINE(void)
1625
sortslice_copy_decr(sortslice *dst, sortslice *src)
1626
37.6k
{
1627
37.6k
    *dst->keys-- = *src->keys--;
1628
37.6k
    if (dst->values != NULL)
1629
0
        *dst->values-- = *src->values--;
1630
37.6k
}
1631
1632
1633
Py_LOCAL_INLINE(void)
1634
sortslice_memcpy(sortslice *s1, Py_ssize_t i, sortslice *s2, Py_ssize_t j,
1635
                 Py_ssize_t n)
1636
5.90k
{
1637
5.90k
    memcpy(&s1->keys[i], &s2->keys[j], sizeof(PyObject *) * n);
1638
5.90k
    if (s1->values != NULL)
1639
0
        memcpy(&s1->values[i], &s2->values[j], sizeof(PyObject *) * n);
1640
5.90k
}
1641
1642
Py_LOCAL_INLINE(void)
1643
sortslice_memmove(sortslice *s1, Py_ssize_t i, sortslice *s2, Py_ssize_t j,
1644
                  Py_ssize_t n)
1645
3.71k
{
1646
3.71k
    memmove(&s1->keys[i], &s2->keys[j], sizeof(PyObject *) * n);
1647
3.71k
    if (s1->values != NULL)
1648
0
        memmove(&s1->values[i], &s2->values[j], sizeof(PyObject *) * n);
1649
3.71k
}
1650
1651
Py_LOCAL_INLINE(void)
1652
sortslice_advance(sortslice *slice, Py_ssize_t n)
1653
102k
{
1654
102k
    slice->keys += n;
1655
102k
    if (slice->values != NULL)
1656
10
        slice->values += n;
1657
102k
}
1658
1659
/* Comparison function: ms->key_compare, which is set at run-time in
1660
 * listsort_impl to optimize for various special cases.
1661
 * Returns -1 on error, 1 if x < y, 0 if x >= y.
1662
 */
1663
1664
1.34M
#define ISLT(X, Y) (*(ms->key_compare))(X, Y, ms)
1665
1666
/* Compare X to Y via "<".  Goto "fail" if the comparison raises an
1667
   error.  Else "k" is set to true iff X<Y, and an "if (k)" block is
1668
   started.  It makes more sense in context <wink>.  X and Y are PyObject*s.
1669
*/
1670
1.26M
#define IFLT(X, Y) if ((k = ISLT(X, Y)) < 0) goto fail;  \
1671
1.26M
           if (k)
1672
1673
/* The maximum number of entries in a MergeState's pending-runs stack.
1674
 * For a list with n elements, this needs at most floor(log2(n)) + 1 entries
1675
 * even if we didn't force runs to a minimal length.  So the number of bits
1676
 * in a Py_ssize_t is plenty large enough for all cases.
1677
 */
1678
#define MAX_MERGE_PENDING (SIZEOF_SIZE_T * 8)
1679
1680
/* When we get into galloping mode, we stay there until both runs win less
1681
 * often than MIN_GALLOP consecutive times.  See listsort.txt for more info.
1682
 */
1683
136k
#define MIN_GALLOP 7
1684
1685
/* Avoid malloc for small temp arrays. */
1686
124k
#define MERGESTATE_TEMP_SIZE 256
1687
1688
/* The largest value of minrun. This must be a power of 2, and >= 1 */
1689
126k
#define MAX_MINRUN 64
1690
#if ((MAX_MINRUN) < 1) || ((MAX_MINRUN) & ((MAX_MINRUN) - 1))
1691
#error "MAX_MINRUN must be a power of 2, and >= 1"
1692
#endif
1693
1694
/* One MergeState exists on the stack per invocation of mergesort.  It's just
1695
 * a convenient way to pass state around among the helper functions.
1696
 */
1697
struct s_slice {
1698
    sortslice base;
1699
    Py_ssize_t len;   /* length of run */
1700
    int power; /* node "level" for powersort merge strategy */
1701
};
1702
1703
typedef struct s_MergeState MergeState;
1704
struct s_MergeState {
1705
    /* This controls when we get *into* galloping mode.  It's initialized
1706
     * to MIN_GALLOP.  merge_lo and merge_hi tend to nudge it higher for
1707
     * random data, and lower for highly structured data.
1708
     */
1709
    Py_ssize_t min_gallop;
1710
1711
    Py_ssize_t listlen;     /* len(input_list) - read only */
1712
    PyObject **basekeys;    /* base address of keys array - read only */
1713
1714
    /* 'a' is temp storage to help with merges.  It contains room for
1715
     * alloced entries.
1716
     */
1717
    sortslice a;        /* may point to temparray below */
1718
    Py_ssize_t alloced;
1719
1720
    /* A stack of n pending runs yet to be merged.  Run #i starts at
1721
     * address base[i] and extends for len[i] elements.  It's always
1722
     * true (so long as the indices are in bounds) that
1723
     *
1724
     *     pending[i].base + pending[i].len == pending[i+1].base
1725
     *
1726
     * so we could cut the storage for this, but it's a minor amount,
1727
     * and keeping all the info explicit simplifies the code.
1728
     */
1729
    int n;
1730
    struct s_slice pending[MAX_MERGE_PENDING];
1731
1732
    /* 'a' points to this when possible, rather than muck with malloc. */
1733
    PyObject *temparray[MERGESTATE_TEMP_SIZE];
1734
1735
    /* This is the function we will use to compare two keys,
1736
     * even when none of our special cases apply and we have to use
1737
     * safe_object_compare. */
1738
    int (*key_compare)(PyObject *, PyObject *, MergeState *);
1739
1740
    /* This function is used by unsafe_object_compare to optimize comparisons
1741
     * when we know our list is type-homogeneous but we can't assume anything else.
1742
     * In the pre-sort check it is set equal to Py_TYPE(key)->tp_richcompare */
1743
    PyObject *(*key_richcompare)(PyObject *, PyObject *, int);
1744
1745
    /* This function is used by unsafe_tuple_compare to compare the first elements
1746
     * of tuples. It may be set to safe_object_compare, but the idea is that hopefully
1747
     * we can assume more, and use one of the special-case compares. */
1748
    int (*tuple_elem_compare)(PyObject *, PyObject *, MergeState *);
1749
1750
    /* Varisbles used for minrun computation. The "ideal" minrun length is
1751
     * the infinite precision listlen / 2**e. See listsort.txt.
1752
     */
1753
     Py_ssize_t mr_current, mr_e, mr_mask;
1754
};
1755
1756
/* binarysort is the best method for sorting small arrays: it does few
1757
   compares, but can do data movement quadratic in the number of elements.
1758
   ss->keys is viewed as an array of n kays, a[:n]. a[:ok] is already sorted.
1759
   Pass ok = 0 (or 1) if you don't know.
1760
   It's sorted in-place, by a stable binary insertion sort. If ss->values
1761
   isn't NULL, it's permuted in lockstap with ss->keys.
1762
   On entry, must have n >= 1, and 0 <= ok <= n <= MAX_MINRUN.
1763
   Return -1 if comparison raises an exception, else 0.
1764
   Even in case of error, the output slice will be some permutation of
1765
   the input (nothing is lost or duplicated).
1766
*/
1767
static int
1768
binarysort(MergeState *ms, const sortslice *ss, Py_ssize_t n, Py_ssize_t ok)
1769
36.6k
{
1770
36.6k
    Py_ssize_t k; /* for IFLT macro expansion */
1771
36.6k
    PyObject ** const a = ss->keys;
1772
36.6k
    PyObject ** const v = ss->values;
1773
36.6k
    const bool has_values = v != NULL;
1774
36.6k
    PyObject *pivot;
1775
36.6k
    Py_ssize_t M;
1776
1777
36.6k
    assert(0 <= ok && ok <= n && 1 <= n && n <= MAX_MINRUN);
1778
    /* assert a[:ok] is sorted */
1779
36.6k
    if (! ok)
1780
0
        ++ok;
1781
    /* Regular insertion sort has average- and worst-case O(n**2) cost
1782
       for both # of comparisons and number of bytes moved. But its branches
1783
       are highly predictable, and it loves sorted input (n-1 compares and no
1784
       data movement). This is significant in cases like sortperf.py's %sort,
1785
       where an out-of-order element near the start of a run is moved into
1786
       place slowly but then the remaining elements up to length minrun are
1787
       generally at worst one slot away from their correct position (so only
1788
       need 1 or 2 commpares to resolve). If comparisons are very fast (such
1789
       as for a list of Python floats), the simple inner loop leaves it
1790
       very competitive with binary insertion, despite that it does
1791
       significantly more compares overall on random data.
1792
1793
       Binary insertion sort has worst, average, and best case O(n log n)
1794
       cost for # of comparisons, but worst and average case O(n**2) cost
1795
       for data movement. The more expensive comparisons, the more important
1796
       the comparison advantage. But its branches are less predictable the
1797
       more "randomish" the data, and that's so significant its worst case
1798
       in real life is random input rather than reverse-ordered (which does
1799
       about twice the data movement than random input does).
1800
1801
       Note that the number of bytes moved doesn't seem to matter. MAX_MINRUN
1802
       of 64 is so small that the key and value pointers all fit in a corner
1803
       of L1 cache, and moving things around in that is very fast. */
1804
#if 0 // ordinary insertion sort.
1805
    PyObject * vpivot = NULL;
1806
    for (; ok < n; ++ok) {
1807
        pivot = a[ok];
1808
        if (has_values)
1809
            vpivot = v[ok];
1810
        for (M = ok - 1; M >= 0; --M) {
1811
            k = ISLT(pivot, a[M]);
1812
            if (k < 0) {
1813
                a[M + 1] = pivot;
1814
                if (has_values)
1815
                    v[M + 1] = vpivot;
1816
                goto fail;
1817
            }
1818
            else if (k) {
1819
                a[M + 1] = a[M];
1820
                if (has_values)
1821
                    v[M + 1] = v[M];
1822
            }
1823
            else
1824
                break;
1825
        }
1826
        a[M + 1] = pivot;
1827
        if (has_values)
1828
            v[M + 1] = vpivot;
1829
    }
1830
#else // binary insertion sort
1831
36.6k
    Py_ssize_t L, R;
1832
311k
    for (; ok < n; ++ok) {
1833
        /* set L to where a[ok] belongs */
1834
274k
        L = 0;
1835
274k
        R = ok;
1836
274k
        pivot = a[ok];
1837
        /* Slice invariants. vacuously true at the start:
1838
         * all a[0:L]  <= pivot
1839
         * all a[L:R]     unknown
1840
         * all a[R:ok]  > pivot
1841
         */
1842
274k
        assert(L < R);
1843
969k
        do {
1844
            /* don't do silly ;-) things to prevent overflow when finding
1845
               the midpoint; L and R are very far from filling a Py_ssize_t */
1846
969k
            M = (L + R) >> 1;
1847
969k
#if 1 // straightforward, but highly unpredictable branch on random data
1848
969k
            IFLT(pivot, a[M])
1849
521k
                R = M;
1850
448k
            else
1851
448k
                L = M + 1;
1852
#else
1853
            /* Try to get compiler to generate conditional move instructions
1854
               instead. Works fine, but leaving it disabled for now because
1855
               it's not yielding consistently faster sorts. Needs more
1856
               investigation. More computation in the inner loop adds its own
1857
               costs, which can be significant when compares are fast. */
1858
            k = ISLT(pivot, a[M]);
1859
            if (k < 0)
1860
                goto fail;
1861
            Py_ssize_t Mp1 = M + 1;
1862
            R = k ? M : R;
1863
            L = k ? L : Mp1;
1864
#endif
1865
969k
        } while (L < R);
1866
274k
        assert(L == R);
1867
        /* a[:L] holds all elements from a[:ok] <= pivot now, so pivot belongs
1868
           at index L. Slide a[L:ok] to the right a slot to make room for it.
1869
           Caution: using memmove is much slower under MSVC 5; we're not
1870
           usually moving many slots. Years later: under Visual Studio 2022,
1871
           memmove seems just slightly slower than doing it "by hand". */
1872
2.12M
        for (M = ok; M > L; --M)
1873
1.85M
            a[M] = a[M - 1];
1874
274k
        a[L] = pivot;
1875
274k
        if (has_values) {
1876
0
            pivot = v[ok];
1877
0
            for (M = ok; M > L; --M)
1878
0
                v[M] = v[M - 1];
1879
0
            v[L] = pivot;
1880
0
        }
1881
274k
    }
1882
36.6k
#endif // pick binary or regular insertion sort
1883
36.6k
    return 0;
1884
1885
0
 fail:
1886
0
    return -1;
1887
36.6k
}
1888
1889
static void
1890
sortslice_reverse(sortslice *s, Py_ssize_t n)
1891
42.0k
{
1892
42.0k
    reverse_slice(s->keys, &s->keys[n]);
1893
42.0k
    if (s->values != NULL)
1894
0
        reverse_slice(s->values, &s->values[n]);
1895
42.0k
}
1896
1897
/*
1898
Return the length of the run beginning at slo->keys, spanning no more than
1899
nremaining elements. The run beginning there may be ascending or descending,
1900
but the function permutes it in place, if needed, so that it's always ascending
1901
upon return.
1902
1903
Returns -1 in case of error.
1904
*/
1905
static Py_ssize_t
1906
count_run(MergeState *ms, sortslice *slo, Py_ssize_t nremaining)
1907
86.2k
{
1908
86.2k
    Py_ssize_t k; /* used by IFLT macro expansion */
1909
86.2k
    Py_ssize_t n;
1910
86.2k
    PyObject ** const lo = slo->keys;
1911
1912
    /* In general, as things go on we've established that the slice starts
1913
       with a monotone run of n elements, starting at lo. */
1914
1915
    /* We're n elements into the slice, and the most recent neq+1 elements are
1916
     * all equal. This reverses them in-place, and resets neq for reuse.
1917
     */
1918
86.2k
#define REVERSE_LAST_NEQ                        \
1919
86.2k
    if (neq) {                                  \
1920
0
        sortslice slice = *slo;                 \
1921
0
        ++neq;                                  \
1922
0
        sortslice_advance(&slice, n - neq);     \
1923
0
        sortslice_reverse(&slice, neq);         \
1924
0
        neq = 0;                                \
1925
0
    }
1926
1927
    /* Sticking to only __lt__ compares is confusing and error-prone. But in
1928
     * this routine, almost all uses of IFLT can be captured by tiny macros
1929
     * giving mnemonic names to the intent. Note that inline functions don't
1930
     * work for this (IFLT expands to code including `goto fail`).
1931
     */
1932
86.2k
#define IF_NEXT_LARGER  IFLT(lo[n-1], lo[n])
1933
198k
#define IF_NEXT_SMALLER IFLT(lo[n], lo[n-1])
1934
1935
86.2k
    assert(nremaining);
1936
    /* try ascending run first */
1937
142k
    for (n = 1; n < nremaining; ++n) {
1938
115k
        IF_NEXT_SMALLER
1939
58.5k
            break;
1940
115k
    }
1941
86.2k
    if (n == nremaining)
1942
27.7k
        return n;
1943
    /* lo[n] is strictly less */
1944
    /* If n is 1 now, then the first compare established it's a descending
1945
     * run, so fall through to the descending case. But if n > 1, there are
1946
     * n elements in an ascending run terminated by the strictly less lo[n].
1947
     * If the first key < lo[n-1], *somewhere* along the way the sequence
1948
     * increased, so we're done (there is no descending run).
1949
     * Else first key >= lo[n-1], which implies that the entire ascending run
1950
     * consists of equal elements. In that case, this is a descending run,
1951
     * and we reverse the all-equal prefix in-place.
1952
     */
1953
58.5k
    if (n > 1) {
1954
16.4k
        IFLT(lo[0], lo[n-1])
1955
16.4k
            return n;
1956
0
        sortslice_reverse(slo, n);
1957
0
    }
1958
42.0k
    ++n; /* in all cases it's been established that lo[n] has been resolved */
1959
1960
    /* Finish descending run. All-squal subruns are reversed in-place on the
1961
     * fly. Their original order will be restored at the end by the whole-slice
1962
     * reversal.
1963
     */
1964
42.0k
    Py_ssize_t neq = 0;
1965
50.0k
    for ( ; n < nremaining; ++n) {
1966
39.8k
        IF_NEXT_SMALLER {
1967
            /* This ends the most recent run of equal elements, but still in
1968
             * the "descending" direction.
1969
             */
1970
7.97k
            REVERSE_LAST_NEQ
1971
7.97k
        }
1972
31.8k
        else {
1973
31.8k
            IF_NEXT_LARGER /* descending run is over */
1974
31.8k
                break;
1975
0
            else /* not x < y and not y < x implies x == y */
1976
0
                ++neq;
1977
31.8k
        }
1978
39.8k
    }
1979
42.0k
    REVERSE_LAST_NEQ
1980
42.0k
    sortslice_reverse(slo, n); /* transform to ascending run */
1981
1982
    /* And after reversing, it's possible this can be extended by a
1983
     * naturally increasing suffix; e.g., [3, 2, 3, 4, 1] makes an
1984
     * ascending run from the first 4 elements.
1985
     */
1986
65.4k
    for ( ; n < nremaining; ++n) {
1987
43.5k
        IF_NEXT_SMALLER
1988
20.1k
            break;
1989
43.5k
    }
1990
1991
42.0k
    return n;
1992
0
fail:
1993
0
    return -1;
1994
1995
42.0k
#undef REVERSE_LAST_NEQ
1996
42.0k
#undef IF_NEXT_SMALLER
1997
42.0k
#undef IF_NEXT_LARGER
1998
42.0k
}
1999
2000
/*
2001
Locate the proper position of key in a sorted vector; if the vector contains
2002
an element equal to key, return the position immediately to the left of
2003
the leftmost equal element.  [gallop_right() does the same except returns
2004
the position to the right of the rightmost equal element (if any).]
2005
2006
"a" is a sorted vector with n elements, starting at a[0].  n must be > 0.
2007
2008
"hint" is an index at which to begin the search, 0 <= hint < n.  The closer
2009
hint is to the final result, the faster this runs.
2010
2011
The return value is the int k in 0..n such that
2012
2013
    a[k-1] < key <= a[k]
2014
2015
pretending that *(a-1) is minus infinity and a[n] is plus infinity.  IOW,
2016
key belongs at index k; or, IOW, the first k elements of a should precede
2017
key, and the last n-k should follow key.
2018
2019
Returns -1 on error.  See listsort.txt for info on the method.
2020
*/
2021
static Py_ssize_t
2022
gallop_left(MergeState *ms, PyObject *key, PyObject **a, Py_ssize_t n, Py_ssize_t hint)
2023
6.18k
{
2024
6.18k
    Py_ssize_t ofs;
2025
6.18k
    Py_ssize_t lastofs;
2026
6.18k
    Py_ssize_t k;
2027
2028
6.18k
    assert(key && a && n > 0 && hint >= 0 && hint < n);
2029
2030
6.18k
    a += hint;
2031
6.18k
    lastofs = 0;
2032
6.18k
    ofs = 1;
2033
6.18k
    IFLT(*a, key) {
2034
        /* a[hint] < key -- gallop right, until
2035
         * a[hint + lastofs] < key <= a[hint + ofs]
2036
         */
2037
2.62k
        const Py_ssize_t maxofs = n - hint;             /* &a[n-1] is highest */
2038
4.39k
        while (ofs < maxofs) {
2039
2.86k
            IFLT(a[ofs], key) {
2040
1.77k
                lastofs = ofs;
2041
1.77k
                assert(ofs <= (PY_SSIZE_T_MAX - 1) / 2);
2042
1.77k
                ofs = (ofs << 1) + 1;
2043
1.77k
            }
2044
1.09k
            else                /* key <= a[hint + ofs] */
2045
1.09k
                break;
2046
2.86k
        }
2047
2.62k
        if (ofs > maxofs)
2048
88
            ofs = maxofs;
2049
        /* Translate back to offsets relative to &a[0]. */
2050
2.62k
        lastofs += hint;
2051
2.62k
        ofs += hint;
2052
2.62k
    }
2053
3.56k
    else {
2054
        /* key <= a[hint] -- gallop left, until
2055
         * a[hint - ofs] < key <= a[hint - lastofs]
2056
         */
2057
3.56k
        const Py_ssize_t maxofs = hint + 1;             /* &a[0] is lowest */
2058
8.06k
        while (ofs < maxofs) {
2059
7.11k
            IFLT(*(a-ofs), key)
2060
2.61k
                break;
2061
            /* key <= a[hint - ofs] */
2062
4.49k
            lastofs = ofs;
2063
4.49k
            assert(ofs <= (PY_SSIZE_T_MAX - 1) / 2);
2064
4.49k
            ofs = (ofs << 1) + 1;
2065
4.49k
        }
2066
3.56k
        if (ofs > maxofs)
2067
50
            ofs = maxofs;
2068
        /* Translate back to positive offsets relative to &a[0]. */
2069
3.56k
        k = lastofs;
2070
3.56k
        lastofs = hint - ofs;
2071
3.56k
        ofs = hint - k;
2072
3.56k
    }
2073
6.18k
    a -= hint;
2074
2075
6.18k
    assert(-1 <= lastofs && lastofs < ofs && ofs <= n);
2076
    /* Now a[lastofs] < key <= a[ofs], so key belongs somewhere to the
2077
     * right of lastofs but no farther right than ofs.  Do a binary
2078
     * search, with invariant a[lastofs-1] < key <= a[ofs].
2079
     */
2080
6.18k
    ++lastofs;
2081
12.3k
    while (lastofs < ofs) {
2082
6.13k
        Py_ssize_t m = lastofs + ((ofs - lastofs) >> 1);
2083
2084
6.13k
        IFLT(a[m], key)
2085
3.04k
            lastofs = m+1;              /* a[m] < key */
2086
3.08k
        else
2087
3.08k
            ofs = m;                    /* key <= a[m] */
2088
6.13k
    }
2089
6.18k
    assert(lastofs == ofs);             /* so a[ofs-1] < key <= a[ofs] */
2090
6.18k
    return ofs;
2091
2092
0
fail:
2093
0
    return -1;
2094
6.18k
}
2095
2096
/*
2097
Exactly like gallop_left(), except that if key already exists in a[0:n],
2098
finds the position immediately to the right of the rightmost equal value.
2099
2100
The return value is the int k in 0..n such that
2101
2102
    a[k-1] <= key < a[k]
2103
2104
or -1 if error.
2105
2106
The code duplication is massive, but this is enough different given that
2107
we're sticking to "<" comparisons that it's much harder to follow if
2108
written as one routine with yet another "left or right?" flag.
2109
*/
2110
static Py_ssize_t
2111
gallop_right(MergeState *ms, PyObject *key, PyObject **a, Py_ssize_t n, Py_ssize_t hint)
2112
6.55k
{
2113
6.55k
    Py_ssize_t ofs;
2114
6.55k
    Py_ssize_t lastofs;
2115
6.55k
    Py_ssize_t k;
2116
2117
6.55k
    assert(key && a && n > 0 && hint >= 0 && hint < n);
2118
2119
6.55k
    a += hint;
2120
6.55k
    lastofs = 0;
2121
6.55k
    ofs = 1;
2122
6.55k
    IFLT(key, *a) {
2123
        /* key < a[hint] -- gallop left, until
2124
         * a[hint - ofs] <= key < a[hint - lastofs]
2125
         */
2126
4.04k
        const Py_ssize_t maxofs = hint + 1;             /* &a[0] is lowest */
2127
7.71k
        while (ofs < maxofs) {
2128
5.22k
            IFLT(key, *(a-ofs)) {
2129
3.66k
                lastofs = ofs;
2130
3.66k
                assert(ofs <= (PY_SSIZE_T_MAX - 1) / 2);
2131
3.66k
                ofs = (ofs << 1) + 1;
2132
3.66k
            }
2133
1.55k
            else                /* a[hint - ofs] <= key */
2134
1.55k
                break;
2135
5.22k
        }
2136
4.04k
        if (ofs > maxofs)
2137
262
            ofs = maxofs;
2138
        /* Translate back to positive offsets relative to &a[0]. */
2139
4.04k
        k = lastofs;
2140
4.04k
        lastofs = hint - ofs;
2141
4.04k
        ofs = hint - k;
2142
4.04k
    }
2143
2.51k
    else {
2144
        /* a[hint] <= key -- gallop right, until
2145
         * a[hint + lastofs] <= key < a[hint + ofs]
2146
        */
2147
2.51k
        const Py_ssize_t maxofs = n - hint;             /* &a[n-1] is highest */
2148
5.01k
        while (ofs < maxofs) {
2149
4.08k
            IFLT(key, a[ofs])
2150
1.59k
                break;
2151
            /* a[hint + ofs] <= key */
2152
2.49k
            lastofs = ofs;
2153
2.49k
            assert(ofs <= (PY_SSIZE_T_MAX - 1) / 2);
2154
2.49k
            ofs = (ofs << 1) + 1;
2155
2.49k
        }
2156
2.51k
        if (ofs > maxofs)
2157
110
            ofs = maxofs;
2158
        /* Translate back to offsets relative to &a[0]. */
2159
2.51k
        lastofs += hint;
2160
2.51k
        ofs += hint;
2161
2.51k
    }
2162
6.55k
    a -= hint;
2163
2164
6.55k
    assert(-1 <= lastofs && lastofs < ofs && ofs <= n);
2165
    /* Now a[lastofs] <= key < a[ofs], so key belongs somewhere to the
2166
     * right of lastofs but no farther right than ofs.  Do a binary
2167
     * search, with invariant a[lastofs-1] <= key < a[ofs].
2168
     */
2169
6.55k
    ++lastofs;
2170
12.4k
    while (lastofs < ofs) {
2171
5.93k
        Py_ssize_t m = lastofs + ((ofs - lastofs) >> 1);
2172
2173
5.93k
        IFLT(key, a[m])
2174
2.99k
            ofs = m;                    /* key < a[m] */
2175
2.94k
        else
2176
2.94k
            lastofs = m+1;              /* a[m] <= key */
2177
5.93k
    }
2178
6.55k
    assert(lastofs == ofs);             /* so a[ofs-1] <= key < a[ofs] */
2179
6.55k
    return ofs;
2180
2181
0
fail:
2182
0
    return -1;
2183
6.55k
}
2184
2185
/* Conceptually a MergeState's constructor. */
2186
static void
2187
merge_init(MergeState *ms, Py_ssize_t list_size, int has_keyfunc,
2188
           sortslice *lo)
2189
124k
{
2190
124k
    assert(ms != NULL);
2191
124k
    if (has_keyfunc) {
2192
        /* The temporary space for merging will need at most half the list
2193
         * size rounded up.  Use the minimum possible space so we can use the
2194
         * rest of temparray for other things.  In particular, if there is
2195
         * enough extra space, listsort() will use it to store the keys.
2196
         */
2197
10
        ms->alloced = (list_size + 1) / 2;
2198
2199
        /* ms->alloced describes how many keys will be stored at
2200
           ms->temparray, but we also need to store the values.  Hence,
2201
           ms->alloced is capped at half of MERGESTATE_TEMP_SIZE. */
2202
10
        if (MERGESTATE_TEMP_SIZE / 2 < ms->alloced)
2203
0
            ms->alloced = MERGESTATE_TEMP_SIZE / 2;
2204
10
        ms->a.values = &ms->temparray[ms->alloced];
2205
10
    }
2206
124k
    else {
2207
124k
        ms->alloced = MERGESTATE_TEMP_SIZE;
2208
124k
        ms->a.values = NULL;
2209
124k
    }
2210
124k
    ms->a.keys = ms->temparray;
2211
124k
    ms->n = 0;
2212
124k
    ms->min_gallop = MIN_GALLOP;
2213
124k
    ms->listlen = list_size;
2214
124k
    ms->basekeys = lo->keys;
2215
2216
    /* State for generating minrun values. See listsort.txt. */
2217
124k
    ms->mr_e = 0;
2218
126k
    while (list_size >> ms->mr_e >= MAX_MINRUN) {
2219
1.49k
        ++ms->mr_e;
2220
1.49k
    }
2221
124k
    ms->mr_mask = (1 << ms->mr_e) - 1;
2222
124k
    ms->mr_current = 0;
2223
124k
}
2224
2225
/* Free all the temp memory owned by the MergeState.  This must be called
2226
 * when you're done with a MergeState, and may be called before then if
2227
 * you want to free the temp memory early.
2228
 */
2229
static void
2230
merge_freemem(MergeState *ms)
2231
124k
{
2232
124k
    assert(ms != NULL);
2233
124k
    if (ms->a.keys != ms->temparray) {
2234
0
        PyMem_Free(ms->a.keys);
2235
0
        ms->a.keys = NULL;
2236
0
    }
2237
124k
}
2238
2239
/* Ensure enough temp memory for 'need' array slots is available.
2240
 * Returns 0 on success and -1 if the memory can't be gotten.
2241
 */
2242
static int
2243
merge_getmem(MergeState *ms, Py_ssize_t need)
2244
0
{
2245
0
    int multiplier;
2246
2247
0
    assert(ms != NULL);
2248
0
    if (need <= ms->alloced)
2249
0
        return 0;
2250
2251
0
    multiplier = ms->a.values != NULL ? 2 : 1;
2252
2253
    /* Don't realloc!  That can cost cycles to copy the old data, but
2254
     * we don't care what's in the block.
2255
     */
2256
0
    merge_freemem(ms);
2257
0
    if ((size_t)need > PY_SSIZE_T_MAX / sizeof(PyObject *) / multiplier) {
2258
0
        PyErr_NoMemory();
2259
0
        return -1;
2260
0
    }
2261
0
    ms->a.keys = (PyObject **)PyMem_Malloc(multiplier * need
2262
0
                                          * sizeof(PyObject *));
2263
0
    if (ms->a.keys != NULL) {
2264
0
        ms->alloced = need;
2265
0
        if (ms->a.values != NULL)
2266
0
            ms->a.values = &ms->a.keys[need];
2267
0
        return 0;
2268
0
    }
2269
0
    PyErr_NoMemory();
2270
0
    return -1;
2271
0
}
2272
1.68k
#define MERGE_GETMEM(MS, NEED) ((NEED) <= (MS)->alloced ? 0 :   \
2273
1.68k
                                merge_getmem(MS, NEED))
2274
2275
/* Merge the na elements starting at ssa with the nb elements starting at
2276
 * ssb.keys = ssa.keys + na in a stable way, in-place.  na and nb must be > 0.
2277
 * Must also have that ssa.keys[na-1] belongs at the end of the merge, and
2278
 * should have na <= nb.  See listsort.txt for more info.  Return 0 if
2279
 * successful, -1 if error.
2280
 */
2281
static Py_ssize_t
2282
merge_lo(MergeState *ms, sortslice ssa, Py_ssize_t na,
2283
         sortslice ssb, Py_ssize_t nb)
2284
884
{
2285
884
    Py_ssize_t k;
2286
884
    sortslice dest;
2287
884
    int result = -1;            /* guilty until proved innocent */
2288
884
    Py_ssize_t min_gallop;
2289
2290
884
    assert(ms && ssa.keys && ssb.keys && na > 0 && nb > 0);
2291
884
    assert(ssa.keys + na == ssb.keys);
2292
884
    if (MERGE_GETMEM(ms, na) < 0)
2293
0
        return -1;
2294
884
    sortslice_memcpy(&ms->a, 0, &ssa, 0, na);
2295
884
    dest = ssa;
2296
884
    ssa = ms->a;
2297
2298
884
    sortslice_copy_incr(&dest, &ssb);
2299
884
    --nb;
2300
884
    if (nb == 0)
2301
0
        goto Succeed;
2302
884
    if (na == 1)
2303
0
        goto CopyB;
2304
2305
884
    min_gallop = ms->min_gallop;
2306
2.01k
    for (;;) {
2307
2.01k
        Py_ssize_t acount = 0;          /* # of times A won in a row */
2308
2.01k
        Py_ssize_t bcount = 0;          /* # of times B won in a row */
2309
2310
        /* Do the straightforward thing until (if ever) one run
2311
         * appears to win consistently.
2312
         */
2313
56.5k
        for (;;) {
2314
56.5k
            assert(na > 1 && nb > 0);
2315
56.5k
            k = ISLT(ssb.keys[0], ssa.keys[0]);
2316
56.5k
            if (k) {
2317
28.1k
                if (k < 0)
2318
0
                    goto Fail;
2319
28.1k
                sortslice_copy_incr(&dest, &ssb);
2320
28.1k
                ++bcount;
2321
28.1k
                acount = 0;
2322
28.1k
                --nb;
2323
28.1k
                if (nb == 0)
2324
255
                    goto Succeed;
2325
27.9k
                if (bcount >= min_gallop)
2326
782
                    break;
2327
27.9k
            }
2328
28.3k
            else {
2329
28.3k
                sortslice_copy_incr(&dest, &ssa);
2330
28.3k
                ++acount;
2331
28.3k
                bcount = 0;
2332
28.3k
                --na;
2333
28.3k
                if (na == 1)
2334
357
                    goto CopyB;
2335
28.0k
                if (acount >= min_gallop)
2336
620
                    break;
2337
28.0k
            }
2338
56.5k
        }
2339
2340
        /* One run is winning so consistently that galloping may
2341
         * be a huge win.  So try that, and continue galloping until
2342
         * (if ever) neither run appears to be winning consistently
2343
         * anymore.
2344
         */
2345
1.40k
        ++min_gallop;
2346
2.16k
        do {
2347
2.16k
            assert(na > 1 && nb > 0);
2348
2.16k
            min_gallop -= min_gallop > 1;
2349
2.16k
            ms->min_gallop = min_gallop;
2350
2.16k
            k = gallop_right(ms, ssb.keys[0], ssa.keys, na, 0);
2351
2.16k
            acount = k;
2352
2.16k
            if (k) {
2353
1.27k
                if (k < 0)
2354
0
                    goto Fail;
2355
1.27k
                sortslice_memcpy(&dest, 0, &ssa, 0, k);
2356
1.27k
                sortslice_advance(&dest, k);
2357
1.27k
                sortslice_advance(&ssa, k);
2358
1.27k
                na -= k;
2359
1.27k
                if (na == 1)
2360
91
                    goto CopyB;
2361
                /* na==0 is impossible now if the comparison
2362
                 * function is consistent, but we can't assume
2363
                 * that it is.
2364
                 */
2365
1.18k
                if (na == 0)
2366
0
                    goto Succeed;
2367
1.18k
            }
2368
2.07k
            sortslice_copy_incr(&dest, &ssb);
2369
2.07k
            --nb;
2370
2.07k
            if (nb == 0)
2371
70
                goto Succeed;
2372
2373
2.00k
            k = gallop_left(ms, ssa.keys[0], ssb.keys, nb, 0);
2374
2.00k
            bcount = k;
2375
2.00k
            if (k) {
2376
1.22k
                if (k < 0)
2377
0
                    goto Fail;
2378
1.22k
                sortslice_memmove(&dest, 0, &ssb, 0, k);
2379
1.22k
                sortslice_advance(&dest, k);
2380
1.22k
                sortslice_advance(&ssb, k);
2381
1.22k
                nb -= k;
2382
1.22k
                if (nb == 0)
2383
80
                    goto Succeed;
2384
1.22k
            }
2385
1.92k
            sortslice_copy_incr(&dest, &ssa);
2386
1.92k
            --na;
2387
1.92k
            if (na == 1)
2388
31
                goto CopyB;
2389
1.92k
        } while (acount >= MIN_GALLOP || bcount >= MIN_GALLOP);
2390
1.13k
        ++min_gallop;           /* penalize it for leaving galloping mode */
2391
1.13k
        ms->min_gallop = min_gallop;
2392
1.13k
    }
2393
405
Succeed:
2394
405
    result = 0;
2395
405
Fail:
2396
405
    if (na)
2397
405
        sortslice_memcpy(&dest, 0, &ssa, 0, na);
2398
405
    return result;
2399
479
CopyB:
2400
479
    assert(na == 1 && nb > 0);
2401
    /* The last element of ssa belongs at the end of the merge. */
2402
479
    sortslice_memmove(&dest, 0, &ssb, 0, nb);
2403
479
    sortslice_copy(&dest, nb, &ssa, 0);
2404
479
    return 0;
2405
479
}
2406
2407
/* Merge the na elements starting at pa with the nb elements starting at
2408
 * ssb.keys = ssa.keys + na in a stable way, in-place.  na and nb must be > 0.
2409
 * Must also have that ssa.keys[na-1] belongs at the end of the merge, and
2410
 * should have na >= nb.  See listsort.txt for more info.  Return 0 if
2411
 * successful, -1 if error.
2412
 */
2413
static Py_ssize_t
2414
merge_hi(MergeState *ms, sortslice ssa, Py_ssize_t na,
2415
         sortslice ssb, Py_ssize_t nb)
2416
804
{
2417
804
    Py_ssize_t k;
2418
804
    sortslice dest, basea, baseb;
2419
804
    int result = -1;            /* guilty until proved innocent */
2420
804
    Py_ssize_t min_gallop;
2421
2422
804
    assert(ms && ssa.keys && ssb.keys && na > 0 && nb > 0);
2423
804
    assert(ssa.keys + na == ssb.keys);
2424
804
    if (MERGE_GETMEM(ms, nb) < 0)
2425
0
        return -1;
2426
804
    dest = ssb;
2427
804
    sortslice_advance(&dest, nb-1);
2428
804
    sortslice_memcpy(&ms->a, 0, &ssb, 0, nb);
2429
804
    basea = ssa;
2430
804
    baseb = ms->a;
2431
804
    ssb.keys = ms->a.keys + nb - 1;
2432
804
    if (ssb.values != NULL)
2433
0
        ssb.values = ms->a.values + nb - 1;
2434
804
    sortslice_advance(&ssa, na - 1);
2435
2436
804
    sortslice_copy_decr(&dest, &ssa);
2437
804
    --na;
2438
804
    if (na == 0)
2439
0
        goto Succeed;
2440
804
    if (nb == 1)
2441
0
        goto CopyA;
2442
2443
804
    min_gallop = ms->min_gallop;
2444
1.57k
    for (;;) {
2445
1.57k
        Py_ssize_t acount = 0;          /* # of times A won in a row */
2446
1.57k
        Py_ssize_t bcount = 0;          /* # of times B won in a row */
2447
2448
        /* Do the straightforward thing until (if ever) one run
2449
         * appears to win consistently.
2450
         */
2451
31.8k
        for (;;) {
2452
31.8k
            assert(na > 0 && nb > 1);
2453
31.8k
            k = ISLT(ssb.keys[0], ssa.keys[0]);
2454
31.8k
            if (k) {
2455
18.7k
                if (k < 0)
2456
0
                    goto Fail;
2457
18.7k
                sortslice_copy_decr(&dest, &ssa);
2458
18.7k
                ++acount;
2459
18.7k
                bcount = 0;
2460
18.7k
                --na;
2461
18.7k
                if (na == 0)
2462
335
                    goto Succeed;
2463
18.4k
                if (acount >= min_gallop)
2464
795
                    break;
2465
18.4k
            }
2466
13.1k
            else {
2467
13.1k
                sortslice_copy_decr(&dest, &ssb);
2468
13.1k
                ++bcount;
2469
13.1k
                acount = 0;
2470
13.1k
                --nb;
2471
13.1k
                if (nb == 1)
2472
56
                    goto CopyA;
2473
13.0k
                if (bcount >= min_gallop)
2474
385
                    break;
2475
13.0k
            }
2476
31.8k
        }
2477
2478
        /* One run is winning so consistently that galloping may
2479
         * be a huge win.  So try that, and continue galloping until
2480
         * (if ever) neither run appears to be winning consistently
2481
         * anymore.
2482
         */
2483
1.18k
        ++min_gallop;
2484
2.70k
        do {
2485
2.70k
            assert(na > 0 && nb > 1);
2486
2.70k
            min_gallop -= min_gallop > 1;
2487
2.70k
            ms->min_gallop = min_gallop;
2488
2.70k
            k = gallop_right(ms, ssb.keys[0], basea.keys, na, na-1);
2489
2.70k
            if (k < 0)
2490
0
                goto Fail;
2491
2.70k
            k = na - k;
2492
2.70k
            acount = k;
2493
2.70k
            if (k) {
2494
1.90k
                sortslice_advance(&dest, -k);
2495
1.90k
                sortslice_advance(&ssa, -k);
2496
1.90k
                sortslice_memmove(&dest, 1, &ssa, 1, k);
2497
1.90k
                na -= k;
2498
1.90k
                if (na == 0)
2499
196
                    goto Succeed;
2500
1.90k
            }
2501
2.50k
            sortslice_copy_decr(&dest, &ssb);
2502
2.50k
            --nb;
2503
2.50k
            if (nb == 1)
2504
14
                goto CopyA;
2505
2506
2.49k
            k = gallop_left(ms, ssa.keys[0], baseb.keys, nb, nb-1);
2507
2.49k
            if (k < 0)
2508
0
                goto Fail;
2509
2.49k
            k = nb - k;
2510
2.49k
            bcount = k;
2511
2.49k
            if (k) {
2512
1.83k
                sortslice_advance(&dest, -k);
2513
1.83k
                sortslice_advance(&ssb, -k);
2514
1.83k
                sortslice_memcpy(&dest, 1, &ssb, 1, k);
2515
1.83k
                nb -= k;
2516
1.83k
                if (nb == 1)
2517
30
                    goto CopyA;
2518
                /* nb==0 is impossible now if the comparison
2519
                 * function is consistent, but we can't assume
2520
                 * that it is.
2521
                 */
2522
1.80k
                if (nb == 0)
2523
0
                    goto Succeed;
2524
1.80k
            }
2525
2.46k
            sortslice_copy_decr(&dest, &ssa);
2526
2.46k
            --na;
2527
2.46k
            if (na == 0)
2528
173
                goto Succeed;
2529
2.46k
        } while (acount >= MIN_GALLOP || bcount >= MIN_GALLOP);
2530
767
        ++min_gallop;           /* penalize it for leaving galloping mode */
2531
767
        ms->min_gallop = min_gallop;
2532
767
    }
2533
704
Succeed:
2534
704
    result = 0;
2535
704
Fail:
2536
704
    if (nb)
2537
704
        sortslice_memcpy(&dest, -(nb-1), &baseb, 0, nb);
2538
704
    return result;
2539
100
CopyA:
2540
100
    assert(nb == 1 && na > 0);
2541
    /* The first element of ssb belongs at the front of the merge. */
2542
100
    sortslice_memmove(&dest, 1-na, &ssa, 1-na, na);
2543
100
    sortslice_advance(&dest, -na);
2544
100
    sortslice_advance(&ssa, -na);
2545
100
    sortslice_copy(&dest, 0, &ssb, 0);
2546
100
    return 0;
2547
100
}
2548
2549
/* Merge the two runs at stack indices i and i+1.
2550
 * Returns 0 on success, -1 on error.
2551
 */
2552
static Py_ssize_t
2553
merge_at(MergeState *ms, Py_ssize_t i)
2554
1.68k
{
2555
1.68k
    sortslice ssa, ssb;
2556
1.68k
    Py_ssize_t na, nb;
2557
1.68k
    Py_ssize_t k;
2558
2559
1.68k
    assert(ms != NULL);
2560
1.68k
    assert(ms->n >= 2);
2561
1.68k
    assert(i >= 0);
2562
1.68k
    assert(i == ms->n - 2 || i == ms->n - 3);
2563
2564
1.68k
    ssa = ms->pending[i].base;
2565
1.68k
    na = ms->pending[i].len;
2566
1.68k
    ssb = ms->pending[i+1].base;
2567
1.68k
    nb = ms->pending[i+1].len;
2568
1.68k
    assert(na > 0 && nb > 0);
2569
1.68k
    assert(ssa.keys + na == ssb.keys);
2570
2571
    /* Record the length of the combined runs; if i is the 3rd-last
2572
     * run now, also slide over the last run (which isn't involved
2573
     * in this merge).  The current run i+1 goes away in any case.
2574
     */
2575
1.68k
    ms->pending[i].len = na + nb;
2576
1.68k
    if (i == ms->n - 3)
2577
0
        ms->pending[i+1] = ms->pending[i+2];
2578
1.68k
    --ms->n;
2579
2580
    /* Where does b start in a?  Elements in a before that can be
2581
     * ignored (already in place).
2582
     */
2583
1.68k
    k = gallop_right(ms, *ssb.keys, ssa.keys, na, 0);
2584
1.68k
    if (k < 0)
2585
0
        return -1;
2586
1.68k
    sortslice_advance(&ssa, k);
2587
1.68k
    na -= k;
2588
1.68k
    if (na == 0)
2589
0
        return 0;
2590
2591
    /* Where does a end in b?  Elements in b after that can be
2592
     * ignored (already in place).
2593
     */
2594
1.68k
    nb = gallop_left(ms, ssa.keys[na-1], ssb.keys, nb, nb-1);
2595
1.68k
    if (nb <= 0)
2596
0
        return nb;
2597
2598
    /* Merge what remains of the runs, using a temp array with
2599
     * min(na, nb) elements.
2600
     */
2601
1.68k
    if (na <= nb)
2602
884
        return merge_lo(ms, ssa, na, ssb, nb);
2603
804
    else
2604
804
        return merge_hi(ms, ssa, na, ssb, nb);
2605
1.68k
}
2606
2607
/* Two adjacent runs begin at index s1. The first run has length n1, and
2608
 * the second run (starting at index s1+n1) has length n2. The list has total
2609
 * length n.
2610
 * Compute the "power" of the first run. See listsort.txt for details.
2611
 */
2612
static int
2613
powerloop(Py_ssize_t s1, Py_ssize_t n1, Py_ssize_t n2, Py_ssize_t n)
2614
1.68k
{
2615
1.68k
    int result = 0;
2616
1.68k
    assert(s1 >= 0);
2617
1.68k
    assert(n1 > 0 && n2 > 0);
2618
1.68k
    assert(s1 + n1 + n2 <= n);
2619
    /* midpoints a and b:
2620
     * a = s1 + n1/2
2621
     * b = s1 + n1 + n2/2 = a + (n1 + n2)/2
2622
     *
2623
     * Those may not be integers, though, because of the "/2". So we work with
2624
     * 2*a and 2*b instead, which are necessarily integers. It makes no
2625
     * difference to the outcome, since the bits in the expansion of (2*i)/n
2626
     * are merely shifted one position from those of i/n.
2627
     */
2628
1.68k
    Py_ssize_t a = 2 * s1 + n1;  /* 2*a */
2629
1.68k
    Py_ssize_t b = a + n1 + n2;  /* 2*b */
2630
    /* Emulate a/n and b/n one bit a time, until bits differ. */
2631
2.15k
    for (;;) {
2632
2.15k
        ++result;
2633
2.15k
        if (a >= n) {  /* both quotient bits are 1 */
2634
235
            assert(b >= a);
2635
235
            a -= n;
2636
235
            b -= n;
2637
235
        }
2638
1.92k
        else if (b >= n) {  /* a/n bit is 0, b/n bit is 1 */
2639
1.68k
            break;
2640
1.68k
        } /* else both quotient bits are 0 */
2641
2.15k
        assert(a < b && b < n);
2642
470
        a <<= 1;
2643
470
        b <<= 1;
2644
470
    }
2645
1.68k
    return result;
2646
1.68k
}
2647
2648
/* The next run has been identified, of length n2.
2649
 * If there's already a run on the stack, apply the "powersort" merge strategy:
2650
 * compute the topmost run's "power" (depth in a conceptual binary merge tree)
2651
 * and merge adjacent runs on the stack with greater power. See listsort.txt
2652
 * for more info.
2653
 *
2654
 * It's the caller's responsibility to push the new run on the stack when this
2655
 * returns.
2656
 *
2657
 * Returns 0 on success, -1 on error.
2658
 */
2659
static int
2660
found_new_run(MergeState *ms, Py_ssize_t n2)
2661
86.2k
{
2662
86.2k
    assert(ms);
2663
86.2k
    if (ms->n) {
2664
1.68k
        assert(ms->n > 0);
2665
1.68k
        struct s_slice *p = ms->pending;
2666
1.68k
        Py_ssize_t s1 = p[ms->n - 1].base.keys - ms->basekeys; /* start index */
2667
1.68k
        Py_ssize_t n1 = p[ms->n - 1].len;
2668
1.68k
        int power = powerloop(s1, n1, n2, ms->listlen);
2669
1.88k
        while (ms->n > 1 && p[ms->n - 2].power > power) {
2670
196
            if (merge_at(ms, ms->n - 2) < 0)
2671
0
                return -1;
2672
196
        }
2673
1.68k
        assert(ms->n < 2 || p[ms->n - 2].power < power);
2674
1.68k
        p[ms->n - 1].power = power;
2675
1.68k
    }
2676
86.2k
    return 0;
2677
86.2k
}
2678
2679
/* Regardless of invariants, merge all runs on the stack until only one
2680
 * remains.  This is used at the end of the mergesort.
2681
 *
2682
 * Returns 0 on success, -1 on error.
2683
 */
2684
static int
2685
merge_force_collapse(MergeState *ms)
2686
84.5k
{
2687
84.5k
    struct s_slice *p = ms->pending;
2688
2689
84.5k
    assert(ms);
2690
86.0k
    while (ms->n > 1) {
2691
1.49k
        Py_ssize_t n = ms->n - 2;
2692
1.49k
        if (n > 0 && p[n-1].len < p[n+1].len)
2693
0
            --n;
2694
1.49k
        if (merge_at(ms, n) < 0)
2695
0
            return -1;
2696
1.49k
    }
2697
84.5k
    return 0;
2698
84.5k
}
2699
2700
/* Return the next minrun value to use. See listsort.txt. */
2701
Py_LOCAL_INLINE(Py_ssize_t)
2702
minrun_next(MergeState *ms)
2703
86.2k
{
2704
86.2k
    ms->mr_current += ms->listlen;
2705
86.2k
    assert(ms->mr_current >= 0); /* no overflow */
2706
86.2k
    Py_ssize_t result = ms->mr_current >> ms->mr_e;
2707
86.2k
    ms->mr_current &= ms->mr_mask;
2708
86.2k
    return result;
2709
86.2k
}
2710
2711
/* Here we define custom comparison functions to optimize for the cases one commonly
2712
 * encounters in practice: homogeneous lists, often of one of the basic types. */
2713
2714
/* This struct holds the comparison function and helper functions
2715
 * selected in the pre-sort check. */
2716
2717
/* These are the special case compare functions.
2718
 * ms->key_compare will always point to one of these: */
2719
2720
/* Heterogeneous compare: default, always safe to fall back on. */
2721
static int
2722
safe_object_compare(PyObject *v, PyObject *w, MergeState *ms)
2723
0
{
2724
    /* No assumptions necessary! */
2725
0
    return PyObject_RichCompareBool(v, w, Py_LT);
2726
0
}
2727
2728
/* Homogeneous compare: safe for any two comparable objects of the same type.
2729
 * (ms->key_richcompare is set to ob_type->tp_richcompare in the
2730
 *  pre-sort check.)
2731
 */
2732
static int
2733
unsafe_object_compare(PyObject *v, PyObject *w, MergeState *ms)
2734
595k
{
2735
595k
    PyObject *res_obj; int res;
2736
2737
    /* No assumptions, because we check first: */
2738
595k
    if (Py_TYPE(v)->tp_richcompare != ms->key_richcompare)
2739
0
        return PyObject_RichCompareBool(v, w, Py_LT);
2740
2741
595k
    assert(ms->key_richcompare != NULL);
2742
595k
    res_obj = (*(ms->key_richcompare))(v, w, Py_LT);
2743
2744
595k
    if (res_obj == Py_NotImplemented) {
2745
0
        Py_DECREF(res_obj);
2746
0
        return PyObject_RichCompareBool(v, w, Py_LT);
2747
0
    }
2748
595k
    if (res_obj == NULL)
2749
0
        return -1;
2750
2751
595k
    if (PyBool_Check(res_obj)) {
2752
595k
        res = (res_obj == Py_True);
2753
595k
    }
2754
0
    else {
2755
0
        res = PyObject_IsTrue(res_obj);
2756
0
    }
2757
595k
    Py_DECREF(res_obj);
2758
2759
    /* Note that we can't assert
2760
     *     res == PyObject_RichCompareBool(v, w, Py_LT);
2761
     * because of evil compare functions like this:
2762
     *     lambda a, b:  int(random.random() * 3) - 1)
2763
     * (which is actually in test_sort.py) */
2764
595k
    return res;
2765
595k
}
2766
2767
/* Latin string compare: safe for any two latin (one byte per char) strings. */
2768
static int
2769
unsafe_latin_compare(PyObject *v, PyObject *w, MergeState *ms)
2770
697k
{
2771
697k
    Py_ssize_t len;
2772
697k
    int res;
2773
2774
    /* Modified from Objects/unicodeobject.c:unicode_compare, assuming: */
2775
697k
    assert(Py_IS_TYPE(v, &PyUnicode_Type));
2776
697k
    assert(Py_IS_TYPE(w, &PyUnicode_Type));
2777
697k
    assert(PyUnicode_KIND(v) == PyUnicode_KIND(w));
2778
697k
    assert(PyUnicode_KIND(v) == PyUnicode_1BYTE_KIND);
2779
2780
697k
    len = Py_MIN(PyUnicode_GET_LENGTH(v), PyUnicode_GET_LENGTH(w));
2781
697k
    res = memcmp(PyUnicode_DATA(v), PyUnicode_DATA(w), len);
2782
2783
697k
    res = (res != 0 ?
2784
644k
           res < 0 :
2785
697k
           PyUnicode_GET_LENGTH(v) < PyUnicode_GET_LENGTH(w));
2786
2787
697k
    assert(res == PyObject_RichCompareBool(v, w, Py_LT));;
2788
697k
    return res;
2789
697k
}
2790
2791
/* Bounded int compare: compare any two longs that fit in a single machine word. */
2792
static int
2793
unsafe_long_compare(PyObject *v, PyObject *w, MergeState *ms)
2794
55.9k
{
2795
55.9k
    PyLongObject *vl, *wl;
2796
55.9k
    intptr_t v0, w0;
2797
55.9k
    int res;
2798
2799
    /* Modified from Objects/longobject.c:long_compare, assuming: */
2800
55.9k
    assert(Py_IS_TYPE(v, &PyLong_Type));
2801
55.9k
    assert(Py_IS_TYPE(w, &PyLong_Type));
2802
55.9k
    assert(_PyLong_IsCompact((PyLongObject *)v));
2803
55.9k
    assert(_PyLong_IsCompact((PyLongObject *)w));
2804
2805
55.9k
    vl = (PyLongObject*)v;
2806
55.9k
    wl = (PyLongObject*)w;
2807
2808
55.9k
    v0 = _PyLong_CompactValue(vl);
2809
55.9k
    w0 = _PyLong_CompactValue(wl);
2810
2811
55.9k
    res = v0 < w0;
2812
55.9k
    assert(res == PyObject_RichCompareBool(v, w, Py_LT));
2813
55.9k
    return res;
2814
55.9k
}
2815
2816
/* Float compare: compare any two floats. */
2817
static int
2818
unsafe_float_compare(PyObject *v, PyObject *w, MergeState *ms)
2819
0
{
2820
0
    int res;
2821
2822
    /* Modified from Objects/floatobject.c:float_richcompare, assuming: */
2823
0
    assert(Py_IS_TYPE(v, &PyFloat_Type));
2824
0
    assert(Py_IS_TYPE(w, &PyFloat_Type));
2825
2826
0
    res = PyFloat_AS_DOUBLE(v) < PyFloat_AS_DOUBLE(w);
2827
0
    assert(res == PyObject_RichCompareBool(v, w, Py_LT));
2828
0
    return res;
2829
0
}
2830
2831
/* Tuple compare: compare *any* two tuples, using
2832
 * ms->tuple_elem_compare to compare the first elements, which is set
2833
 * using the same pre-sort check as we use for ms->key_compare,
2834
 * but run on the list [x[0] for x in L]. This allows us to optimize compares
2835
 * on two levels (as long as [x[0] for x in L] is type-homogeneous.) The idea is
2836
 * that most tuple compares don't involve x[1:]. */
2837
static int
2838
unsafe_tuple_compare(PyObject *v, PyObject *w, MergeState *ms)
2839
732
{
2840
732
    PyTupleObject *vt, *wt;
2841
732
    Py_ssize_t i, vlen, wlen;
2842
732
    int k;
2843
2844
    /* Modified from Objects/tupleobject.c:tuplerichcompare, assuming: */
2845
732
    assert(Py_IS_TYPE(v, &PyTuple_Type));
2846
732
    assert(Py_IS_TYPE(w, &PyTuple_Type));
2847
732
    assert(Py_SIZE(v) > 0);
2848
732
    assert(Py_SIZE(w) > 0);
2849
2850
732
    vt = (PyTupleObject *)v;
2851
732
    wt = (PyTupleObject *)w;
2852
2853
732
    vlen = Py_SIZE(vt);
2854
732
    wlen = Py_SIZE(wt);
2855
2856
732
    for (i = 0; i < vlen && i < wlen; i++) {
2857
732
        k = PyObject_RichCompareBool(vt->ob_item[i], wt->ob_item[i], Py_EQ);
2858
732
        if (k < 0)
2859
0
            return -1;
2860
732
        if (!k)
2861
732
            break;
2862
732
    }
2863
2864
732
    if (i >= vlen || i >= wlen)
2865
0
        return vlen < wlen;
2866
2867
732
    if (i == 0)
2868
732
        return ms->tuple_elem_compare(vt->ob_item[i], wt->ob_item[i], ms);
2869
0
    else
2870
0
        return PyObject_RichCompareBool(vt->ob_item[i], wt->ob_item[i], Py_LT);
2871
732
}
2872
2873
/* An adaptive, stable, natural mergesort.  See listsort.txt.
2874
 * Returns Py_None on success, NULL on error.  Even in case of error, the
2875
 * list will be some permutation of its input state (nothing is lost or
2876
 * duplicated).
2877
 */
2878
/*[clinic input]
2879
@permit_long_docstring_body
2880
@critical_section
2881
list.sort
2882
2883
    *
2884
    key as keyfunc: object = None
2885
    reverse: bool = False
2886
2887
Sort the list in ascending order and return None.
2888
2889
The sort is in-place (i.e. the list itself is modified) and stable (i.e. the
2890
order of two equal elements is maintained).
2891
2892
If a key function is given, apply it once to each list item and sort them,
2893
ascending or descending, according to their function values.
2894
2895
The reverse flag can be set to sort in descending order.
2896
[clinic start generated code]*/
2897
2898
static PyObject *
2899
list_sort_impl(PyListObject *self, PyObject *keyfunc, int reverse)
2900
/*[clinic end generated code: output=57b9f9c5e23fbe42 input=e4f6b6069181ad7d]*/
2901
124k
{
2902
124k
    MergeState ms;
2903
124k
    Py_ssize_t nremaining;
2904
124k
    Py_ssize_t minrun;
2905
124k
    sortslice lo;
2906
124k
    Py_ssize_t saved_ob_size, saved_allocated;
2907
124k
    PyObject **saved_ob_item;
2908
124k
    PyObject **final_ob_item;
2909
124k
    PyObject *result = NULL;            /* guilty until proved innocent */
2910
124k
    Py_ssize_t i;
2911
124k
    PyObject **keys;
2912
2913
124k
    assert(self != NULL);
2914
124k
    assert(PyList_Check(self));
2915
124k
    if (keyfunc == Py_None)
2916
31.3k
        keyfunc = NULL;
2917
2918
    /* The list is temporarily made empty, so that mutations performed
2919
     * by comparison functions can't affect the slice of memory we're
2920
     * sorting (allowing mutations during sorting is a core-dump
2921
     * factory, since ob_item may change).
2922
     */
2923
124k
    saved_ob_size = Py_SIZE(self);
2924
124k
    saved_ob_item = self->ob_item;
2925
124k
    saved_allocated = self->allocated;
2926
124k
    Py_SET_SIZE(self, 0);
2927
124k
    FT_ATOMIC_STORE_PTR_RELEASE(self->ob_item, NULL);
2928
124k
    self->allocated = -1; /* any operation will reset it to >= 0 */
2929
2930
124k
    if (keyfunc == NULL) {
2931
124k
        keys = NULL;
2932
124k
        lo.keys = saved_ob_item;
2933
124k
        lo.values = NULL;
2934
124k
    }
2935
10
    else {
2936
10
        if (saved_ob_size < MERGESTATE_TEMP_SIZE/2)
2937
            /* Leverage stack space we allocated but won't otherwise use */
2938
10
            keys = &ms.temparray[saved_ob_size+1];
2939
0
        else {
2940
0
            keys = PyMem_Malloc(sizeof(PyObject *) * saved_ob_size);
2941
0
            if (keys == NULL) {
2942
0
                PyErr_NoMemory();
2943
0
                goto keyfunc_fail;
2944
0
            }
2945
0
        }
2946
2947
31
        for (i = 0; i < saved_ob_size ; i++) {
2948
21
            keys[i] = PyObject_CallOneArg(keyfunc, saved_ob_item[i]);
2949
21
            if (keys[i] == NULL) {
2950
0
                for (i=i-1 ; i>=0 ; i--)
2951
0
                    Py_DECREF(keys[i]);
2952
0
                if (saved_ob_size >= MERGESTATE_TEMP_SIZE/2)
2953
0
                    PyMem_Free(keys);
2954
0
                goto keyfunc_fail;
2955
0
            }
2956
21
        }
2957
2958
10
        lo.keys = keys;
2959
10
        lo.values = saved_ob_item;
2960
10
    }
2961
2962
2963
    /* The pre-sort check: here's where we decide which compare function to use.
2964
     * How much optimization is safe? We test for homogeneity with respect to
2965
     * several properties that are expensive to check at compare-time, and
2966
     * set ms appropriately. */
2967
124k
    if (saved_ob_size > 1) {
2968
        /* Assume the first element is representative of the whole list. */
2969
84.5k
        int keys_are_in_tuples = (Py_IS_TYPE(lo.keys[0], &PyTuple_Type) &&
2970
4
                                  Py_SIZE(lo.keys[0]) > 0);
2971
2972
84.5k
        PyTypeObject* key_type = (keys_are_in_tuples ?
2973
4
                                  Py_TYPE(PyTuple_GET_ITEM(lo.keys[0], 0)) :
2974
84.5k
                                  Py_TYPE(lo.keys[0]));
2975
2976
84.5k
        int keys_are_all_same_type = 1;
2977
84.5k
        int strings_are_latin = 1;
2978
84.5k
        int ints_are_bounded = 1;
2979
2980
        /* Prove that assumption by checking every key. */
2981
575k
        for (i=0; i < saved_ob_size; i++) {
2982
2983
490k
            if (keys_are_in_tuples &&
2984
176
                !(Py_IS_TYPE(lo.keys[i], &PyTuple_Type) && Py_SIZE(lo.keys[i]) != 0)) {
2985
0
                keys_are_in_tuples = 0;
2986
0
                keys_are_all_same_type = 0;
2987
0
                break;
2988
0
            }
2989
2990
            /* Note: for lists of tuples, key is the first element of the tuple
2991
             * lo.keys[i], not lo.keys[i] itself! We verify type-homogeneity
2992
             * for lists of tuples in the if-statement directly above. */
2993
490k
            PyObject *key = (keys_are_in_tuples ?
2994
176
                             PyTuple_GET_ITEM(lo.keys[i], 0) :
2995
490k
                             lo.keys[i]);
2996
2997
490k
            if (!Py_IS_TYPE(key, key_type)) {
2998
0
                keys_are_all_same_type = 0;
2999
                /* If keys are in tuple we must loop over the whole list to make
3000
                   sure all items are tuples */
3001
0
                if (!keys_are_in_tuples) {
3002
0
                    break;
3003
0
                }
3004
0
            }
3005
3006
490k
            if (keys_are_all_same_type) {
3007
490k
                if (key_type == &PyLong_Type &&
3008
49.3k
                    ints_are_bounded &&
3009
49.3k
                    !_PyLong_IsCompact((PyLongObject *)key)) {
3010
3011
0
                    ints_are_bounded = 0;
3012
0
                }
3013
490k
                else if (key_type == &PyUnicode_Type &&
3014
441k
                         strings_are_latin &&
3015
610k
                         PyUnicode_KIND(key) != PyUnicode_1BYTE_KIND) {
3016
3017
14.2k
                        strings_are_latin = 0;
3018
14.2k
                    }
3019
490k
                }
3020
490k
            }
3021
3022
        /* Choose the best compare, given what we now know about the keys. */
3023
84.5k
        if (keys_are_all_same_type) {
3024
3025
84.5k
            if (key_type == &PyUnicode_Type && strings_are_latin) {
3026
50.3k
                ms.key_compare = unsafe_latin_compare;
3027
50.3k
            }
3028
34.2k
            else if (key_type == &PyLong_Type && ints_are_bounded) {
3029
19.9k
                ms.key_compare = unsafe_long_compare;
3030
19.9k
            }
3031
14.2k
            else if (key_type == &PyFloat_Type) {
3032
0
                ms.key_compare = unsafe_float_compare;
3033
0
            }
3034
14.2k
            else if ((ms.key_richcompare = key_type->tp_richcompare) != NULL) {
3035
14.2k
                ms.key_compare = unsafe_object_compare;
3036
14.2k
            }
3037
0
            else {
3038
0
                ms.key_compare = safe_object_compare;
3039
0
            }
3040
84.5k
        }
3041
0
        else {
3042
0
            ms.key_compare = safe_object_compare;
3043
0
        }
3044
3045
84.5k
        if (keys_are_in_tuples) {
3046
            /* Make sure we're not dealing with tuples of tuples
3047
             * (remember: here, key_type refers list [key[0] for key in keys]) */
3048
4
            if (key_type == &PyTuple_Type) {
3049
0
                ms.tuple_elem_compare = safe_object_compare;
3050
0
            }
3051
4
            else {
3052
4
                ms.tuple_elem_compare = ms.key_compare;
3053
4
            }
3054
3055
4
            ms.key_compare = unsafe_tuple_compare;
3056
4
        }
3057
84.5k
    }
3058
    /* End of pre-sort check: ms is now set properly! */
3059
3060
124k
    merge_init(&ms, saved_ob_size, keys != NULL, &lo);
3061
3062
124k
    nremaining = saved_ob_size;
3063
124k
    if (nremaining < 2)
3064
39.9k
        goto succeed;
3065
3066
    /* Reverse sort stability achieved by initially reversing the list,
3067
    applying a stable forward sort, then reversing the final result. */
3068
84.5k
    if (reverse) {
3069
3
        if (keys != NULL)
3070
0
            reverse_slice(&keys[0], &keys[saved_ob_size]);
3071
3
        reverse_slice(&saved_ob_item[0], &saved_ob_item[saved_ob_size]);
3072
3
    }
3073
3074
    /* March over the array once, left to right, finding natural runs,
3075
     * and extending short natural runs to minrun elements.
3076
     */
3077
86.2k
    do {
3078
86.2k
        Py_ssize_t n;
3079
3080
        /* Identify next run. */
3081
86.2k
        n = count_run(&ms, &lo, nremaining);
3082
86.2k
        if (n < 0)
3083
0
            goto fail;
3084
        /* If short, extend to min(minrun, nremaining). */
3085
86.2k
        minrun = minrun_next(&ms);
3086
86.2k
        if (n < minrun) {
3087
36.6k
            const Py_ssize_t force = nremaining <= minrun ?
3088
34.9k
                              nremaining : minrun;
3089
36.6k
            if (binarysort(&ms, &lo, force, n) < 0)
3090
0
                goto fail;
3091
36.6k
            n = force;
3092
36.6k
        }
3093
        /* Maybe merge pending runs. */
3094
86.2k
        assert(ms.n == 0 || ms.pending[ms.n -1].base.keys +
3095
86.2k
                            ms.pending[ms.n-1].len == lo.keys);
3096
86.2k
        if (found_new_run(&ms, n) < 0)
3097
0
            goto fail;
3098
        /* Push new run on stack. */
3099
86.2k
        assert(ms.n < MAX_MERGE_PENDING);
3100
86.2k
        ms.pending[ms.n].base = lo;
3101
86.2k
        ms.pending[ms.n].len = n;
3102
86.2k
        ++ms.n;
3103
        /* Advance to find next run. */
3104
86.2k
        sortslice_advance(&lo, n);
3105
86.2k
        nremaining -= n;
3106
86.2k
    } while (nremaining);
3107
3108
84.5k
    if (merge_force_collapse(&ms) < 0)
3109
0
        goto fail;
3110
84.5k
    assert(ms.n == 1);
3111
84.5k
    assert(keys == NULL
3112
84.5k
           ? ms.pending[0].base.keys == saved_ob_item
3113
84.5k
           : ms.pending[0].base.keys == &keys[0]);
3114
84.5k
    assert(ms.pending[0].len == saved_ob_size);
3115
84.5k
    lo = ms.pending[0].base;
3116
3117
124k
succeed:
3118
124k
    result = Py_None;
3119
124k
fail:
3120
124k
    if (keys != NULL) {
3121
31
        for (i = 0; i < saved_ob_size; i++)
3122
21
            Py_DECREF(keys[i]);
3123
10
        if (saved_ob_size >= MERGESTATE_TEMP_SIZE/2)
3124
0
            PyMem_Free(keys);
3125
10
    }
3126
3127
124k
    if (self->allocated != -1 && result != NULL) {
3128
        /* The user mucked with the list during the sort,
3129
         * and we don't already have another error to report.
3130
         */
3131
0
        PyErr_SetString(PyExc_ValueError, "list modified during sort");
3132
0
        result = NULL;
3133
0
    }
3134
3135
124k
    if (reverse && saved_ob_size > 1)
3136
3
        reverse_slice(saved_ob_item, saved_ob_item + saved_ob_size);
3137
3138
124k
    merge_freemem(&ms);
3139
3140
124k
keyfunc_fail:
3141
124k
    final_ob_item = self->ob_item;
3142
124k
    i = Py_SIZE(self);
3143
124k
    Py_SET_SIZE(self, saved_ob_size);
3144
124k
    FT_ATOMIC_STORE_PTR_RELEASE(self->ob_item, saved_ob_item);
3145
124k
    FT_ATOMIC_STORE_SSIZE_RELAXED(self->allocated, saved_allocated);
3146
124k
    if (final_ob_item != NULL) {
3147
        /* we cannot use list_clear() for this because it does not
3148
           guarantee that the list is really empty when it returns */
3149
0
        while (--i >= 0) {
3150
0
            Py_XDECREF(final_ob_item[i]);
3151
0
        }
3152
#ifdef Py_GIL_DISABLED
3153
        ensure_shared_on_resize(self);
3154
        bool use_qsbr = _PyObject_GC_IS_SHARED(self);
3155
#else
3156
0
        bool use_qsbr = false;
3157
0
#endif
3158
0
        free_list_items(final_ob_item, use_qsbr);
3159
0
    }
3160
124k
    return Py_XNewRef(result);
3161
124k
}
3162
#undef IFLT
3163
#undef ISLT
3164
3165
int
3166
PyList_Sort(PyObject *v)
3167
93.2k
{
3168
93.2k
    if (v == NULL || !PyList_Check(v)) {
3169
0
        PyErr_BadInternalCall();
3170
0
        return -1;
3171
0
    }
3172
93.2k
    Py_BEGIN_CRITICAL_SECTION(v);
3173
93.2k
    v = list_sort_impl((PyListObject *)v, NULL, 0);
3174
93.2k
    Py_END_CRITICAL_SECTION();
3175
93.2k
    if (v == NULL)
3176
0
        return -1;
3177
93.2k
    Py_DECREF(v);
3178
93.2k
    return 0;
3179
93.2k
}
3180
3181
/*[clinic input]
3182
@critical_section
3183
list.reverse
3184
3185
Reverse *IN PLACE*.
3186
[clinic start generated code]*/
3187
3188
static PyObject *
3189
list_reverse_impl(PyListObject *self)
3190
/*[clinic end generated code: output=482544fc451abea9 input=04ac8e0c6a66e4d9]*/
3191
58
{
3192
58
    if (Py_SIZE(self) > 1)
3193
58
        reverse_slice(self->ob_item, self->ob_item + Py_SIZE(self));
3194
58
    Py_RETURN_NONE;
3195
58
}
3196
3197
int
3198
PyList_Reverse(PyObject *v)
3199
0
{
3200
0
    PyListObject *self = (PyListObject *)v;
3201
3202
0
    if (v == NULL || !PyList_Check(v)) {
3203
0
        PyErr_BadInternalCall();
3204
0
        return -1;
3205
0
    }
3206
0
    Py_BEGIN_CRITICAL_SECTION(self);
3207
0
    if (Py_SIZE(self) > 1) {
3208
0
        reverse_slice(self->ob_item, self->ob_item + Py_SIZE(self));
3209
0
    }
3210
0
    Py_END_CRITICAL_SECTION()
3211
0
    return 0;
3212
0
}
3213
3214
PyObject *
3215
PyList_AsTuple(PyObject *v)
3216
54.1k
{
3217
54.1k
    if (v == NULL || !PyList_Check(v)) {
3218
0
        PyErr_BadInternalCall();
3219
0
        return NULL;
3220
0
    }
3221
54.1k
    PyObject *ret;
3222
54.1k
    PyListObject *self = (PyListObject *)v;
3223
54.1k
    Py_BEGIN_CRITICAL_SECTION(self);
3224
54.1k
    ret = PyTuple_FromArray(self->ob_item, Py_SIZE(v));
3225
54.1k
    Py_END_CRITICAL_SECTION();
3226
54.1k
    return ret;
3227
54.1k
}
3228
3229
PyObject *
3230
_PyList_AsTupleAndClear(PyListObject *self)
3231
620
{
3232
620
    assert(self != NULL);
3233
620
    PyObject *ret;
3234
620
    if (self->ob_item == NULL) {
3235
0
        return PyTuple_New(0);
3236
0
    }
3237
620
    Py_BEGIN_CRITICAL_SECTION(self);
3238
620
    PyObject **items = self->ob_item;
3239
620
    Py_ssize_t size = Py_SIZE(self);
3240
620
    self->ob_item = NULL;
3241
620
    Py_SET_SIZE(self, 0);
3242
620
    ret = _PyTuple_FromArraySteal(items, size);
3243
620
    free_list_items(items, false);
3244
620
    Py_END_CRITICAL_SECTION();
3245
620
    return ret;
3246
620
}
3247
3248
PyObject *
3249
_PyList_FromStackRefStealOnSuccess(const _PyStackRef *src, Py_ssize_t n)
3250
7.94M
{
3251
7.94M
    if (n == 0) {
3252
7.93M
        return PyList_New(0);
3253
7.93M
    }
3254
3255
11.1k
    PyListObject *list = (PyListObject *)PyList_New(n);
3256
11.1k
    if (list == NULL) {
3257
0
        return NULL;
3258
0
    }
3259
3260
11.1k
    PyObject **dst = list->ob_item;
3261
27.1k
    for (Py_ssize_t i = 0; i < n; i++) {
3262
16.0k
        dst[i] = PyStackRef_AsPyObjectSteal(src[i]);
3263
16.0k
    }
3264
3265
11.1k
    return (PyObject *)list;
3266
11.1k
}
3267
3268
/*[clinic input]
3269
list.index
3270
3271
    value: object
3272
    start: slice_index(accept={int}) = 0
3273
    stop: slice_index(accept={int}, c_default="PY_SSIZE_T_MAX") = sys.maxsize
3274
    /
3275
3276
Return first index of value.
3277
3278
Raises ValueError if the value is not present.
3279
[clinic start generated code]*/
3280
3281
static PyObject *
3282
list_index_impl(PyListObject *self, PyObject *value, Py_ssize_t start,
3283
                Py_ssize_t stop)
3284
/*[clinic end generated code: output=ec51b88787e4e481 input=40ec5826303a0eb1]*/
3285
0
{
3286
0
    if (start < 0) {
3287
0
        start += Py_SIZE(self);
3288
0
        if (start < 0)
3289
0
            start = 0;
3290
0
    }
3291
0
    if (stop < 0) {
3292
0
        stop += Py_SIZE(self);
3293
0
        if (stop < 0)
3294
0
            stop = 0;
3295
0
    }
3296
0
    for (Py_ssize_t i = start; i < stop; i++) {
3297
0
        PyObject *obj = list_get_item_ref(self, i);
3298
0
        if (obj == NULL) {
3299
            // out-of-bounds
3300
0
            break;
3301
0
        }
3302
0
        int cmp = PyObject_RichCompareBool(obj, value, Py_EQ);
3303
0
        Py_DECREF(obj);
3304
0
        if (cmp > 0)
3305
0
            return PyLong_FromSsize_t(i);
3306
0
        else if (cmp < 0)
3307
0
            return NULL;
3308
0
    }
3309
0
    PyErr_SetString(PyExc_ValueError, "list.index(x): x not in list");
3310
0
    return NULL;
3311
0
}
3312
3313
/*[clinic input]
3314
list.count
3315
3316
     value: object
3317
     /
3318
3319
Return number of occurrences of value.
3320
[clinic start generated code]*/
3321
3322
static PyObject *
3323
list_count_impl(PyListObject *self, PyObject *value)
3324
/*[clinic end generated code: output=eff66f14aef2df86 input=3bdc3a5e6f749565]*/
3325
0
{
3326
0
    Py_ssize_t count = 0;
3327
0
    for (Py_ssize_t i = 0; ; i++) {
3328
0
        PyObject *obj = list_get_item_ref(self, i);
3329
0
        if (obj == NULL) {
3330
            // out-of-bounds
3331
0
            break;
3332
0
        }
3333
0
        if (obj == value) {
3334
0
           count++;
3335
0
           Py_DECREF(obj);
3336
0
           continue;
3337
0
        }
3338
0
        int cmp = PyObject_RichCompareBool(obj, value, Py_EQ);
3339
0
        Py_DECREF(obj);
3340
0
        if (cmp > 0)
3341
0
            count++;
3342
0
        else if (cmp < 0)
3343
0
            return NULL;
3344
0
    }
3345
0
    return PyLong_FromSsize_t(count);
3346
0
}
3347
3348
/*[clinic input]
3349
@critical_section
3350
list.remove
3351
3352
     value: object
3353
     /
3354
3355
Remove first occurrence of value.
3356
3357
Raises ValueError if the value is not present.
3358
[clinic start generated code]*/
3359
3360
static PyObject *
3361
list_remove_impl(PyListObject *self, PyObject *value)
3362
/*[clinic end generated code: output=b9b76a6633b18778 input=26c813dbb95aa93b]*/
3363
1.37k
{
3364
1.37k
    Py_ssize_t i;
3365
3366
1.37k
    for (i = 0; i < Py_SIZE(self); i++) {
3367
1.37k
        PyObject *obj = self->ob_item[i];
3368
1.37k
        Py_INCREF(obj);
3369
1.37k
        int cmp = PyObject_RichCompareBool(obj, value, Py_EQ);
3370
1.37k
        Py_DECREF(obj);
3371
1.37k
        if (cmp > 0) {
3372
1.37k
            if (list_ass_slice_lock_held(self, i, i+1, NULL) == 0)
3373
1.37k
                Py_RETURN_NONE;
3374
0
            return NULL;
3375
1.37k
        }
3376
0
        else if (cmp < 0)
3377
0
            return NULL;
3378
1.37k
    }
3379
0
    PyErr_SetString(PyExc_ValueError, "list.remove(x): x not in list");
3380
0
    return NULL;
3381
1.37k
}
3382
3383
static int
3384
list_traverse(PyObject *self, visitproc visit, void *arg)
3385
26.9M
{
3386
26.9M
    PyListObject *o = (PyListObject *)self;
3387
26.9M
    Py_ssize_t i;
3388
3389
105M
    for (i = Py_SIZE(o); --i >= 0; )
3390
79.0M
        Py_VISIT(o->ob_item[i]);
3391
26.9M
    return 0;
3392
26.9M
}
3393
3394
static PyObject *
3395
list_richcompare_impl(PyObject *v, PyObject *w, int op)
3396
226k
{
3397
226k
    PyListObject *vl, *wl;
3398
226k
    Py_ssize_t i;
3399
3400
226k
    if (!PyList_Check(v) || !PyList_Check(w))
3401
329
        Py_RETURN_NOTIMPLEMENTED;
3402
3403
226k
    vl = (PyListObject *)v;
3404
226k
    wl = (PyListObject *)w;
3405
3406
226k
    if (Py_SIZE(vl) != Py_SIZE(wl) && (op == Py_EQ || op == Py_NE)) {
3407
        /* Shortcut: if the lengths differ, the lists differ */
3408
113k
        if (op == Py_EQ)
3409
113k
            Py_RETURN_FALSE;
3410
0
        else
3411
0
            Py_RETURN_TRUE;
3412
113k
    }
3413
3414
    /* Search for the first index where items are different */
3415
114k
    for (i = 0; i < Py_SIZE(vl) && i < Py_SIZE(wl); i++) {
3416
112k
        PyObject *vitem = vl->ob_item[i];
3417
112k
        PyObject *witem = wl->ob_item[i];
3418
112k
        if (vitem == witem) {
3419
907
            continue;
3420
907
        }
3421
3422
111k
        Py_INCREF(vitem);
3423
111k
        Py_INCREF(witem);
3424
111k
        int k = PyObject_RichCompareBool(vitem, witem, Py_EQ);
3425
111k
        Py_DECREF(vitem);
3426
111k
        Py_DECREF(witem);
3427
111k
        if (k < 0)
3428
0
            return NULL;
3429
111k
        if (!k)
3430
110k
            break;
3431
111k
    }
3432
3433
113k
    if (i >= Py_SIZE(vl) || i >= Py_SIZE(wl)) {
3434
        /* No more items to compare -- compare sizes */
3435
2.17k
        Py_RETURN_RICHCOMPARE(Py_SIZE(vl), Py_SIZE(wl), op);
3436
2.17k
    }
3437
3438
    /* We have an item that differs -- shortcuts for EQ/NE */
3439
110k
    if (op == Py_EQ) {
3440
110k
        Py_RETURN_FALSE;
3441
110k
    }
3442
7
    if (op == Py_NE) {
3443
7
        Py_RETURN_TRUE;
3444
7
    }
3445
3446
    /* Compare the final item again using the proper operator */
3447
0
    PyObject *vitem = vl->ob_item[i];
3448
0
    PyObject *witem = wl->ob_item[i];
3449
0
    Py_INCREF(vitem);
3450
0
    Py_INCREF(witem);
3451
0
    PyObject *result = PyObject_RichCompare(vl->ob_item[i], wl->ob_item[i], op);
3452
0
    Py_DECREF(vitem);
3453
0
    Py_DECREF(witem);
3454
0
    return result;
3455
7
}
3456
3457
static PyObject *
3458
list_richcompare(PyObject *v, PyObject *w, int op)
3459
226k
{
3460
226k
    PyObject *ret;
3461
226k
    Py_BEGIN_CRITICAL_SECTION2(v, w);
3462
226k
    ret = list_richcompare_impl(v, w, op);
3463
226k
    Py_END_CRITICAL_SECTION2()
3464
226k
    return ret;
3465
226k
}
3466
3467
/*[clinic input]
3468
list.__init__
3469
3470
    iterable: object(c_default="NULL") = ()
3471
    /
3472
3473
Built-in mutable sequence.
3474
3475
If no argument is given, the constructor creates a new empty list.
3476
The argument must be an iterable if specified.
3477
[clinic start generated code]*/
3478
3479
static int
3480
list___init___impl(PyListObject *self, PyObject *iterable)
3481
/*[clinic end generated code: output=0f3c21379d01de48 input=b3f3fe7206af8f6b]*/
3482
328k
{
3483
    /* Verify list invariants established by PyType_GenericAlloc() */
3484
328k
    assert(0 <= Py_SIZE(self));
3485
328k
    assert(Py_SIZE(self) <= self->allocated || self->allocated == -1);
3486
328k
    assert(self->ob_item != NULL ||
3487
328k
           self->allocated == 0 || self->allocated == -1);
3488
3489
    /* Empty previous contents */
3490
328k
    if (self->ob_item != NULL) {
3491
0
        Py_BEGIN_CRITICAL_SECTION(self);
3492
0
        list_clear(self);
3493
0
        Py_END_CRITICAL_SECTION();
3494
0
    }
3495
328k
    if (iterable != NULL) {
3496
326k
        if (_list_extend(self, iterable) < 0) {
3497
0
            return -1;
3498
0
        }
3499
326k
    }
3500
328k
    return 0;
3501
328k
}
3502
3503
static PyObject *
3504
list_vectorcall(PyObject *type, PyObject * const*args,
3505
                size_t nargsf, PyObject *kwnames)
3506
326k
{
3507
326k
    if (!_PyArg_NoKwnames("list", kwnames)) {
3508
0
        return NULL;
3509
0
    }
3510
326k
    Py_ssize_t nargs = PyVectorcall_NARGS(nargsf);
3511
326k
    if (!_PyArg_CheckPositional("list", nargs, 0, 1)) {
3512
0
        return NULL;
3513
0
    }
3514
3515
326k
    PyObject *list = PyType_GenericAlloc(_PyType_CAST(type), 0);
3516
326k
    if (list == NULL) {
3517
0
        return NULL;
3518
0
    }
3519
326k
    if (nargs) {
3520
326k
        if (list___init___impl((PyListObject *)list, args[0])) {
3521
0
            Py_DECREF(list);
3522
0
            return NULL;
3523
0
        }
3524
326k
    }
3525
326k
    return list;
3526
326k
}
3527
3528
3529
/*[clinic input]
3530
list.__sizeof__
3531
3532
Return the size of the list in memory, in bytes.
3533
[clinic start generated code]*/
3534
3535
static PyObject *
3536
list___sizeof___impl(PyListObject *self)
3537
/*[clinic end generated code: output=3417541f95f9a53e input=b8030a5d5ce8a187]*/
3538
0
{
3539
0
    size_t res = _PyObject_SIZE(Py_TYPE(self));
3540
0
    Py_ssize_t allocated = FT_ATOMIC_LOAD_SSIZE_RELAXED(self->allocated);
3541
0
    res += (size_t)allocated * sizeof(void*);
3542
0
    return PyLong_FromSize_t(res);
3543
0
}
3544
3545
static PyObject *list_iter(PyObject *seq);
3546
static PyObject *list_subscript(PyObject*, PyObject*);
3547
3548
static PyMethodDef list_methods[] = {
3549
    {"__getitem__", list_subscript, METH_O|METH_COEXIST,
3550
     PyDoc_STR("__getitem__($self, index, /)\n--\n\nReturn self[index].")},
3551
    LIST___REVERSED___METHODDEF
3552
    LIST___SIZEOF___METHODDEF
3553
    PY_LIST_CLEAR_METHODDEF
3554
    LIST_COPY_METHODDEF
3555
    LIST_APPEND_METHODDEF
3556
    LIST_INSERT_METHODDEF
3557
    LIST_EXTEND_METHODDEF
3558
    LIST_POP_METHODDEF
3559
    LIST_REMOVE_METHODDEF
3560
    LIST_INDEX_METHODDEF
3561
    LIST_COUNT_METHODDEF
3562
    LIST_REVERSE_METHODDEF
3563
    LIST_SORT_METHODDEF
3564
    {"__class_getitem__", Py_GenericAlias, METH_O|METH_CLASS, PyDoc_STR("See PEP 585")},
3565
    {NULL,              NULL}           /* sentinel */
3566
};
3567
3568
static PySequenceMethods list_as_sequence = {
3569
    list_length,                                /* sq_length */
3570
    list_concat,                                /* sq_concat */
3571
    list_repeat,                                /* sq_repeat */
3572
    list_item,                                  /* sq_item */
3573
    0,                                          /* sq_slice */
3574
    list_ass_item,                              /* sq_ass_item */
3575
    0,                                          /* sq_ass_slice */
3576
    list_contains,                              /* sq_contains */
3577
    list_inplace_concat,                        /* sq_inplace_concat */
3578
    list_inplace_repeat,                        /* sq_inplace_repeat */
3579
};
3580
3581
static inline PyObject *
3582
list_slice_step_lock_held(PyListObject *a, Py_ssize_t start, Py_ssize_t step, Py_ssize_t len)
3583
28
{
3584
28
    PyListObject *np = (PyListObject *)list_new_prealloc(len);
3585
28
    if (np == NULL) {
3586
0
        return NULL;
3587
0
    }
3588
28
    size_t cur;
3589
28
    Py_ssize_t i;
3590
28
    PyObject **src = a->ob_item;
3591
28
    PyObject **dest = np->ob_item;
3592
280
    for (cur = start, i = 0; i < len;
3593
252
            cur += (size_t)step, i++) {
3594
252
        PyObject *v = src[cur];
3595
252
        dest[i] = Py_NewRef(v);
3596
252
    }
3597
28
    Py_SET_SIZE(np, len);
3598
28
    return (PyObject *)np;
3599
28
}
3600
3601
static PyObject *
3602
list_slice_wrap(PyListObject *aa, Py_ssize_t start, Py_ssize_t stop, Py_ssize_t step)
3603
1.66M
{
3604
1.66M
    PyObject *res = NULL;
3605
1.66M
    Py_BEGIN_CRITICAL_SECTION(aa);
3606
1.66M
    Py_ssize_t len = PySlice_AdjustIndices(Py_SIZE(aa), &start, &stop, step);
3607
1.66M
    if (len <= 0) {
3608
115
        res = PyList_New(0);
3609
115
    }
3610
1.66M
    else if (step == 1) {
3611
1.66M
        res = list_slice_lock_held(aa, start, stop);
3612
1.66M
    }
3613
28
    else {
3614
28
        res = list_slice_step_lock_held(aa, start, step, len);
3615
28
    }
3616
1.66M
    Py_END_CRITICAL_SECTION();
3617
1.66M
    return res;
3618
1.66M
}
3619
3620
static inline PyObject*
3621
list_slice_subscript(PyObject* self, PyObject* item)
3622
1.66M
{
3623
1.66M
    assert(PyList_Check(self));
3624
1.66M
    assert(PySlice_Check(item));
3625
1.66M
    Py_ssize_t start, stop, step;
3626
1.66M
    if (PySlice_Unpack(item, &start, &stop, &step) < 0) {
3627
0
        return NULL;
3628
0
    }
3629
1.66M
    return list_slice_wrap((PyListObject *)self, start, stop, step);
3630
1.66M
}
3631
3632
PyObject *
3633
_PyList_SliceSubscript(PyObject* _self, PyObject* item)
3634
1.66M
{
3635
1.66M
    return list_slice_subscript(_self, item);
3636
1.66M
}
3637
3638
static PyObject *
3639
list_subscript(PyObject* _self, PyObject* item)
3640
2.29M
{
3641
2.29M
    PyListObject* self = (PyListObject*)_self;
3642
2.29M
    if (_PyIndex_Check(item)) {
3643
2.29M
        Py_ssize_t i;
3644
2.29M
        i = PyNumber_AsSsize_t(item, PyExc_IndexError);
3645
2.29M
        if (i == -1 && PyErr_Occurred())
3646
0
            return NULL;
3647
2.29M
        if (i < 0)
3648
2.04k
            i += PyList_GET_SIZE(self);
3649
2.29M
        return list_item((PyObject *)self, i);
3650
2.29M
    }
3651
1.10k
    else if (PySlice_Check(item)) {
3652
1.10k
        return list_slice_subscript(_self, item);
3653
1.10k
    }
3654
0
    else {
3655
0
        PyErr_Format(PyExc_TypeError,
3656
0
                     "list indices must be integers or slices, not %.200s",
3657
0
                     Py_TYPE(item)->tp_name);
3658
0
        return NULL;
3659
0
    }
3660
2.29M
}
3661
3662
static Py_ssize_t
3663
adjust_slice_indexes(PyListObject *lst,
3664
                     Py_ssize_t *start, Py_ssize_t *stop,
3665
                     Py_ssize_t step)
3666
14.9k
{
3667
14.9k
    Py_ssize_t slicelength = PySlice_AdjustIndices(Py_SIZE(lst), start, stop,
3668
14.9k
                                                   step);
3669
3670
    /* Make sure s[5:2] = [..] inserts at the right place:
3671
        before 5, not before 2. */
3672
14.9k
    if ((step < 0 && *start < *stop) ||
3673
14.9k
        (step > 0 && *start > *stop))
3674
0
        *stop = *start;
3675
3676
14.9k
    return slicelength;
3677
14.9k
}
3678
3679
static int
3680
list_ass_subscript_lock_held(PyObject *_self, PyObject *item, PyObject *value)
3681
1.68M
{
3682
1.68M
    _Py_CRITICAL_SECTION_ASSERT_OBJECT_LOCKED(_self);
3683
3684
1.68M
    PyListObject *self = (PyListObject *)_self;
3685
1.68M
    if (_PyIndex_Check(item)) {
3686
1.67M
        Py_ssize_t i = PyNumber_AsSsize_t(item, PyExc_IndexError);
3687
1.67M
        if (i == -1 && PyErr_Occurred())
3688
0
            return -1;
3689
1.67M
        if (i < 0)
3690
1.66M
            i += PyList_GET_SIZE(self);
3691
1.67M
        return list_ass_item_lock_held(self, i, value);
3692
1.67M
    }
3693
14.9k
    else if (PySlice_Check(item)) {
3694
14.9k
        Py_ssize_t start, stop, step;
3695
3696
14.9k
        if (PySlice_Unpack(item, &start, &stop, &step) < 0) {
3697
0
            return -1;
3698
0
        }
3699
3700
14.9k
        if (value == NULL) {
3701
            /* delete slice */
3702
7
            PyObject **garbage;
3703
7
            size_t cur;
3704
7
            Py_ssize_t i;
3705
7
            int res;
3706
3707
7
            Py_ssize_t slicelength = adjust_slice_indexes(self, &start, &stop,
3708
7
                                                          step);
3709
3710
7
            if (step == 1)
3711
7
                return list_ass_slice_lock_held(self, start, stop, value);
3712
3713
0
            if (slicelength <= 0)
3714
0
                return 0;
3715
3716
0
            if (step < 0) {
3717
0
                stop = start + 1;
3718
0
                start = stop + step*(slicelength - 1) - 1;
3719
0
                step = -step;
3720
0
            }
3721
3722
0
            garbage = (PyObject**)
3723
0
                PyMem_Malloc(slicelength*sizeof(PyObject*));
3724
0
            if (!garbage) {
3725
0
                PyErr_NoMemory();
3726
0
                return -1;
3727
0
            }
3728
3729
            /* drawing pictures might help understand these for
3730
               loops. Basically, we memmove the parts of the
3731
               list that are *not* part of the slice: step-1
3732
               items for each item that is part of the slice,
3733
               and then tail end of the list that was not
3734
               covered by the slice */
3735
0
            for (cur = start, i = 0;
3736
0
                 cur < (size_t)stop;
3737
0
                 cur += step, i++) {
3738
0
                Py_ssize_t lim = step - 1;
3739
3740
0
                garbage[i] = PyList_GET_ITEM(self, cur);
3741
3742
0
                if (cur + step >= (size_t)Py_SIZE(self)) {
3743
0
                    lim = Py_SIZE(self) - cur - 1;
3744
0
                }
3745
3746
0
                memmove(self->ob_item + cur - i,
3747
0
                    self->ob_item + cur + 1,
3748
0
                    lim * sizeof(PyObject *));
3749
0
            }
3750
0
            cur = start + (size_t)slicelength * step;
3751
0
            if (cur < (size_t)Py_SIZE(self)) {
3752
0
                memmove(self->ob_item + cur - slicelength,
3753
0
                    self->ob_item + cur,
3754
0
                    (Py_SIZE(self) - cur) *
3755
0
                     sizeof(PyObject *));
3756
0
            }
3757
3758
0
            Py_SET_SIZE(self, Py_SIZE(self) - slicelength);
3759
0
            res = list_resize(self, Py_SIZE(self));
3760
3761
0
            for (i = 0; i < slicelength; i++) {
3762
0
                Py_DECREF(garbage[i]);
3763
0
            }
3764
0
            PyMem_Free(garbage);
3765
3766
0
            return res;
3767
0
        }
3768
14.9k
        else {
3769
            /* assign slice */
3770
14.9k
            PyObject *ins, *seq;
3771
14.9k
            PyObject **garbage, **seqitems, **selfitems;
3772
14.9k
            Py_ssize_t i;
3773
14.9k
            size_t cur;
3774
3775
            /* protect against a[::-1] = a */
3776
14.9k
            if (self == (PyListObject*)value) {
3777
0
                seq = list_slice_lock_held((PyListObject *)value, 0,
3778
0
                                            Py_SIZE(value));
3779
0
            }
3780
14.9k
            else {
3781
14.9k
                seq = PySequence_Fast(value,
3782
14.9k
                                      "must assign iterable "
3783
14.9k
                                      "to extended slice");
3784
14.9k
            }
3785
14.9k
            if (!seq)
3786
0
                return -1;
3787
3788
14.9k
            Py_ssize_t slicelength = adjust_slice_indexes(self, &start, &stop,
3789
14.9k
                                                          step);
3790
3791
14.9k
            if (step == 1) {
3792
14.9k
                int res = list_ass_slice_lock_held(self, start, stop, seq);
3793
14.9k
                Py_DECREF(seq);
3794
14.9k
                return res;
3795
14.9k
            }
3796
3797
0
            if (PySequence_Fast_GET_SIZE(seq) != slicelength) {
3798
0
                PyErr_Format(PyExc_ValueError,
3799
0
                    "attempt to assign sequence of "
3800
0
                    "size %zd to extended slice of "
3801
0
                    "size %zd",
3802
0
                         PySequence_Fast_GET_SIZE(seq),
3803
0
                         slicelength);
3804
0
                Py_DECREF(seq);
3805
0
                return -1;
3806
0
            }
3807
3808
0
            if (!slicelength) {
3809
0
                Py_DECREF(seq);
3810
0
                return 0;
3811
0
            }
3812
3813
0
            garbage = (PyObject**)
3814
0
                PyMem_Malloc(slicelength*sizeof(PyObject*));
3815
0
            if (!garbage) {
3816
0
                Py_DECREF(seq);
3817
0
                PyErr_NoMemory();
3818
0
                return -1;
3819
0
            }
3820
3821
0
            selfitems = self->ob_item;
3822
0
            seqitems = PySequence_Fast_ITEMS(seq);
3823
0
            for (cur = start, i = 0; i < slicelength;
3824
0
                 cur += (size_t)step, i++) {
3825
0
                garbage[i] = selfitems[cur];
3826
0
                ins = Py_NewRef(seqitems[i]);
3827
0
                selfitems[cur] = ins;
3828
0
            }
3829
3830
0
            for (i = 0; i < slicelength; i++) {
3831
0
                Py_DECREF(garbage[i]);
3832
0
            }
3833
3834
0
            PyMem_Free(garbage);
3835
0
            Py_DECREF(seq);
3836
3837
0
            return 0;
3838
0
        }
3839
14.9k
    }
3840
0
    else {
3841
0
        PyErr_Format(PyExc_TypeError,
3842
0
                     "list indices must be integers or slices, not %.200s",
3843
0
                     Py_TYPE(item)->tp_name);
3844
0
        return -1;
3845
0
    }
3846
1.68M
}
3847
3848
static int
3849
list_ass_subscript(PyObject *self, PyObject *item, PyObject *value)
3850
1.68M
{
3851
1.68M
    int res;
3852
#ifdef Py_GIL_DISABLED
3853
    if (PySlice_Check(item) && value != NULL && PyList_CheckExact(value)) {
3854
        Py_BEGIN_CRITICAL_SECTION2(self, value);
3855
        res = list_ass_subscript_lock_held(self, item, value);
3856
        Py_END_CRITICAL_SECTION2();
3857
        return res;
3858
    }
3859
#endif
3860
1.68M
    Py_BEGIN_CRITICAL_SECTION(self);
3861
1.68M
    res = list_ass_subscript_lock_held(self, item, value);
3862
1.68M
    Py_END_CRITICAL_SECTION();
3863
1.68M
    return res;
3864
1.68M
}
3865
3866
static PyMappingMethods list_as_mapping = {
3867
    list_length,
3868
    list_subscript,
3869
    list_ass_subscript
3870
};
3871
3872
PyTypeObject PyList_Type = {
3873
    PyVarObject_HEAD_INIT(&PyType_Type, 0)
3874
    "list",
3875
    sizeof(PyListObject),
3876
    0,
3877
    list_dealloc,                               /* tp_dealloc */
3878
    0,                                          /* tp_vectorcall_offset */
3879
    0,                                          /* tp_getattr */
3880
    0,                                          /* tp_setattr */
3881
    0,                                          /* tp_as_async */
3882
    list_repr,                                  /* tp_repr */
3883
    0,                                          /* tp_as_number */
3884
    &list_as_sequence,                          /* tp_as_sequence */
3885
    &list_as_mapping,                           /* tp_as_mapping */
3886
    PyObject_HashNotImplemented,                /* tp_hash */
3887
    0,                                          /* tp_call */
3888
    0,                                          /* tp_str */
3889
    PyObject_GenericGetAttr,                    /* tp_getattro */
3890
    0,                                          /* tp_setattro */
3891
    0,                                          /* tp_as_buffer */
3892
    Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC |
3893
        Py_TPFLAGS_BASETYPE | Py_TPFLAGS_LIST_SUBCLASS |
3894
        _Py_TPFLAGS_MATCH_SELF | Py_TPFLAGS_SEQUENCE,  /* tp_flags */
3895
    list___init____doc__,                       /* tp_doc */
3896
    list_traverse,                              /* tp_traverse */
3897
    list_clear_slot,                            /* tp_clear */
3898
    list_richcompare,                           /* tp_richcompare */
3899
    0,                                          /* tp_weaklistoffset */
3900
    list_iter,                                  /* tp_iter */
3901
    0,                                          /* tp_iternext */
3902
    list_methods,                               /* tp_methods */
3903
    0,                                          /* tp_members */
3904
    0,                                          /* tp_getset */
3905
    0,                                          /* tp_base */
3906
    0,                                          /* tp_dict */
3907
    0,                                          /* tp_descr_get */
3908
    0,                                          /* tp_descr_set */
3909
    0,                                          /* tp_dictoffset */
3910
    list___init__,                              /* tp_init */
3911
    PyType_GenericAlloc,                        /* tp_alloc */
3912
    PyType_GenericNew,                          /* tp_new */
3913
    PyObject_GC_Del,                            /* tp_free */
3914
    .tp_vectorcall = list_vectorcall,
3915
    .tp_version_tag = _Py_TYPE_VERSION_LIST,
3916
};
3917
3918
/*********************** List Iterator **************************/
3919
3920
static void listiter_dealloc(PyObject *);
3921
static int listiter_traverse(PyObject *, visitproc, void *);
3922
static PyObject *listiter_next(PyObject *);
3923
static PyObject *listiter_len(PyObject *, PyObject *);
3924
static PyObject *listiter_reduce_general(void *_it, int forward);
3925
static PyObject *listiter_reduce(PyObject *, PyObject *);
3926
static PyObject *listiter_setstate(PyObject *, PyObject *state);
3927
3928
PyDoc_STRVAR(length_hint_doc, "Private method returning an estimate of len(list(it)).");
3929
PyDoc_STRVAR(reduce_doc, "Return state information for pickling.");
3930
PyDoc_STRVAR(setstate_doc, "Set state information for unpickling.");
3931
3932
static PyMethodDef listiter_methods[] = {
3933
    {"__length_hint__", listiter_len, METH_NOARGS, length_hint_doc},
3934
    {"__reduce__", listiter_reduce, METH_NOARGS, reduce_doc},
3935
    {"__setstate__", listiter_setstate, METH_O, setstate_doc},
3936
    {NULL,              NULL}           /* sentinel */
3937
};
3938
3939
PyTypeObject PyListIter_Type = {
3940
    PyVarObject_HEAD_INIT(&PyType_Type, 0)
3941
    "list_iterator",                            /* tp_name */
3942
    sizeof(_PyListIterObject),                  /* tp_basicsize */
3943
    0,                                          /* tp_itemsize */
3944
    /* methods */
3945
    listiter_dealloc,               /* tp_dealloc */
3946
    0,                                          /* tp_vectorcall_offset */
3947
    0,                                          /* tp_getattr */
3948
    0,                                          /* tp_setattr */
3949
    0,                                          /* tp_as_async */
3950
    0,                                          /* tp_repr */
3951
    0,                                          /* tp_as_number */
3952
    0,                                          /* tp_as_sequence */
3953
    0,                                          /* tp_as_mapping */
3954
    0,                                          /* tp_hash */
3955
    0,                                          /* tp_call */
3956
    0,                                          /* tp_str */
3957
    PyObject_GenericGetAttr,                    /* tp_getattro */
3958
    0,                                          /* tp_setattro */
3959
    0,                                          /* tp_as_buffer */
3960
    Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC,/* tp_flags */
3961
    0,                                          /* tp_doc */
3962
    listiter_traverse,                          /* tp_traverse */
3963
    0,                                          /* tp_clear */
3964
    0,                                          /* tp_richcompare */
3965
    0,                                          /* tp_weaklistoffset */
3966
    PyObject_SelfIter,                          /* tp_iter */
3967
    listiter_next,                              /* tp_iternext */
3968
    listiter_methods,                           /* tp_methods */
3969
    0,                                          /* tp_members */
3970
};
3971
3972
3973
static PyObject *
3974
list_iter(PyObject *seq)
3975
351k
{
3976
351k
    if (!PyList_Check(seq)) {
3977
0
        PyErr_BadInternalCall();
3978
0
        return NULL;
3979
0
    }
3980
351k
    _PyListIterObject *it = _Py_FREELIST_POP(_PyListIterObject, list_iters);
3981
351k
    if (it == NULL) {
3982
29
        it = PyObject_GC_New(_PyListIterObject, &PyListIter_Type);
3983
29
        if (it == NULL) {
3984
0
            return NULL;
3985
0
        }
3986
29
    }
3987
351k
    it->it_index = 0;
3988
351k
    it->it_seq = (PyListObject *)Py_NewRef(seq);
3989
351k
    _PyObject_GC_TRACK(it);
3990
351k
    return (PyObject *)it;
3991
351k
}
3992
3993
static void
3994
listiter_dealloc(PyObject *self)
3995
351k
{
3996
351k
    _PyListIterObject *it = (_PyListIterObject *)self;
3997
351k
    _PyObject_GC_UNTRACK(it);
3998
351k
    Py_XDECREF(it->it_seq);
3999
351k
    assert(Py_IS_TYPE(self, &PyListIter_Type));
4000
351k
    _Py_FREELIST_FREE(list_iters, it, PyObject_GC_Del);
4001
351k
}
4002
4003
static int
4004
listiter_traverse(PyObject *it, visitproc visit, void *arg)
4005
0
{
4006
0
    Py_VISIT(((_PyListIterObject *)it)->it_seq);
4007
0
    return 0;
4008
0
}
4009
4010
static PyObject *
4011
listiter_next(PyObject *self)
4012
37.6M
{
4013
37.6M
    _PyListIterObject *it = (_PyListIterObject *)self;
4014
37.6M
    Py_ssize_t index = FT_ATOMIC_LOAD_SSIZE_RELAXED(it->it_index);
4015
37.6M
    if (index < 0) {
4016
153
        return NULL;
4017
153
    }
4018
4019
37.6M
    PyObject *item = list_get_item_ref(it->it_seq, index);
4020
37.6M
    if (item == NULL) {
4021
        // out-of-bounds
4022
349k
        FT_ATOMIC_STORE_SSIZE_RELAXED(it->it_index, -1);
4023
349k
#ifndef Py_GIL_DISABLED
4024
349k
        PyListObject *seq = it->it_seq;
4025
349k
        it->it_seq = NULL;
4026
349k
        Py_DECREF(seq);
4027
349k
#endif
4028
349k
        return NULL;
4029
349k
    }
4030
37.2M
    FT_ATOMIC_STORE_SSIZE_RELAXED(it->it_index, index + 1);
4031
37.2M
    return item;
4032
37.6M
}
4033
4034
static PyObject *
4035
listiter_len(PyObject *self, PyObject *Py_UNUSED(ignored))
4036
0
{
4037
0
    assert(self != NULL);
4038
0
    _PyListIterObject *it = (_PyListIterObject *)self;
4039
0
    Py_ssize_t index = FT_ATOMIC_LOAD_SSIZE_RELAXED(it->it_index);
4040
0
    if (index >= 0) {
4041
0
        Py_ssize_t len = PyList_GET_SIZE(it->it_seq) - index;
4042
0
        if (len >= 0)
4043
0
            return PyLong_FromSsize_t(len);
4044
0
    }
4045
0
    return PyLong_FromLong(0);
4046
0
}
4047
4048
static PyObject *
4049
listiter_reduce(PyObject *it, PyObject *Py_UNUSED(ignored))
4050
0
{
4051
0
    return listiter_reduce_general(it, 1);
4052
0
}
4053
4054
static PyObject *
4055
listiter_setstate(PyObject *self, PyObject *state)
4056
0
{
4057
0
    _PyListIterObject *it = (_PyListIterObject *)self;
4058
0
    Py_ssize_t index = PyLong_AsSsize_t(state);
4059
0
    if (index == -1 && PyErr_Occurred())
4060
0
        return NULL;
4061
0
    if (it->it_seq != NULL) {
4062
0
        if (index < -1)
4063
0
            index = -1;
4064
0
        else if (index > PyList_GET_SIZE(it->it_seq))
4065
0
            index = PyList_GET_SIZE(it->it_seq); /* iterator exhausted */
4066
0
        FT_ATOMIC_STORE_SSIZE_RELAXED(it->it_index, index);
4067
0
    }
4068
0
    Py_RETURN_NONE;
4069
0
}
4070
4071
/*********************** List Reverse Iterator **************************/
4072
4073
typedef struct {
4074
    PyObject_HEAD
4075
    Py_ssize_t it_index;
4076
    PyListObject *it_seq; /* Set to NULL when iterator is exhausted */
4077
} listreviterobject;
4078
4079
static void listreviter_dealloc(PyObject *);
4080
static int listreviter_traverse(PyObject *, visitproc, void *);
4081
static PyObject *listreviter_next(PyObject *);
4082
static PyObject *listreviter_len(PyObject *, PyObject *);
4083
static PyObject *listreviter_reduce(PyObject *, PyObject *);
4084
static PyObject *listreviter_setstate(PyObject *, PyObject *);
4085
4086
static PyMethodDef listreviter_methods[] = {
4087
    {"__length_hint__", listreviter_len, METH_NOARGS, length_hint_doc},
4088
    {"__reduce__", listreviter_reduce, METH_NOARGS, reduce_doc},
4089
    {"__setstate__", listreviter_setstate, METH_O, setstate_doc},
4090
    {NULL,              NULL}           /* sentinel */
4091
};
4092
4093
PyTypeObject PyListRevIter_Type = {
4094
    PyVarObject_HEAD_INIT(&PyType_Type, 0)
4095
    "list_reverseiterator",                     /* tp_name */
4096
    sizeof(listreviterobject),                  /* tp_basicsize */
4097
    0,                                          /* tp_itemsize */
4098
    /* methods */
4099
    listreviter_dealloc,                        /* tp_dealloc */
4100
    0,                                          /* tp_vectorcall_offset */
4101
    0,                                          /* tp_getattr */
4102
    0,                                          /* tp_setattr */
4103
    0,                                          /* tp_as_async */
4104
    0,                                          /* tp_repr */
4105
    0,                                          /* tp_as_number */
4106
    0,                                          /* tp_as_sequence */
4107
    0,                                          /* tp_as_mapping */
4108
    0,                                          /* tp_hash */
4109
    0,                                          /* tp_call */
4110
    0,                                          /* tp_str */
4111
    PyObject_GenericGetAttr,                    /* tp_getattro */
4112
    0,                                          /* tp_setattro */
4113
    0,                                          /* tp_as_buffer */
4114
    Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC,/* tp_flags */
4115
    0,                                          /* tp_doc */
4116
    listreviter_traverse,                       /* tp_traverse */
4117
    0,                                          /* tp_clear */
4118
    0,                                          /* tp_richcompare */
4119
    0,                                          /* tp_weaklistoffset */
4120
    PyObject_SelfIter,                          /* tp_iter */
4121
    listreviter_next,                           /* tp_iternext */
4122
    listreviter_methods,                /* tp_methods */
4123
    0,
4124
};
4125
4126
/*[clinic input]
4127
list.__reversed__
4128
4129
Return a reverse iterator over the list.
4130
[clinic start generated code]*/
4131
4132
static PyObject *
4133
list___reversed___impl(PyListObject *self)
4134
/*[clinic end generated code: output=b166f073208c888c input=eadb6e17f8a6a280]*/
4135
80
{
4136
80
    listreviterobject *it;
4137
4138
80
    it = PyObject_GC_New(listreviterobject, &PyListRevIter_Type);
4139
80
    if (it == NULL)
4140
0
        return NULL;
4141
80
    assert(PyList_Check(self));
4142
80
    it->it_index = PyList_GET_SIZE(self) - 1;
4143
80
    it->it_seq = (PyListObject*)Py_NewRef(self);
4144
80
    PyObject_GC_Track(it);
4145
80
    return (PyObject *)it;
4146
80
}
4147
4148
static void
4149
listreviter_dealloc(PyObject *self)
4150
80
{
4151
80
    listreviterobject *it = (listreviterobject *)self;
4152
80
    PyObject_GC_UnTrack(it);
4153
80
    Py_XDECREF(it->it_seq);
4154
80
    PyObject_GC_Del(it);
4155
80
}
4156
4157
static int
4158
listreviter_traverse(PyObject *it, visitproc visit, void *arg)
4159
0
{
4160
0
    Py_VISIT(((listreviterobject *)it)->it_seq);
4161
0
    return 0;
4162
0
}
4163
4164
static PyObject *
4165
listreviter_next(PyObject *self)
4166
211
{
4167
211
    listreviterobject *it = (listreviterobject *)self;
4168
211
    assert(it != NULL);
4169
211
    Py_ssize_t index = FT_ATOMIC_LOAD_SSIZE_RELAXED(it->it_index);
4170
211
    if (index < 0) {
4171
58
        return NULL;
4172
58
    }
4173
4174
153
    PyListObject *seq = it->it_seq;
4175
153
    assert(PyList_Check(seq));
4176
153
    PyObject *item = list_get_item_ref(seq, index);
4177
153
    if (item != NULL) {
4178
153
        FT_ATOMIC_STORE_SSIZE_RELAXED(it->it_index, index - 1);
4179
153
        return item;
4180
153
    }
4181
0
    FT_ATOMIC_STORE_SSIZE_RELAXED(it->it_index, -1);
4182
0
#ifndef Py_GIL_DISABLED
4183
0
    it->it_seq = NULL;
4184
0
    Py_DECREF(seq);
4185
0
#endif
4186
0
    return NULL;
4187
153
}
4188
4189
static PyObject *
4190
listreviter_len(PyObject *self, PyObject *Py_UNUSED(ignored))
4191
0
{
4192
0
    listreviterobject *it = (listreviterobject *)self;
4193
0
    Py_ssize_t index = FT_ATOMIC_LOAD_SSIZE_RELAXED(it->it_index);
4194
0
    Py_ssize_t len = index + 1;
4195
0
    if (it->it_seq == NULL || PyList_GET_SIZE(it->it_seq) < len)
4196
0
        len = 0;
4197
0
    return PyLong_FromSsize_t(len);
4198
0
}
4199
4200
static PyObject *
4201
listreviter_reduce(PyObject *it, PyObject *Py_UNUSED(ignored))
4202
0
{
4203
0
    return listiter_reduce_general(it, 0);
4204
0
}
4205
4206
static PyObject *
4207
listreviter_setstate(PyObject *self, PyObject *state)
4208
0
{
4209
0
    listreviterobject *it = (listreviterobject *)self;
4210
0
    Py_ssize_t index = PyLong_AsSsize_t(state);
4211
0
    if (index == -1 && PyErr_Occurred())
4212
0
        return NULL;
4213
0
    if (it->it_seq != NULL) {
4214
0
        if (index < -1)
4215
0
            index = -1;
4216
0
        else if (index > PyList_GET_SIZE(it->it_seq) - 1)
4217
0
            index = PyList_GET_SIZE(it->it_seq) - 1;
4218
0
        FT_ATOMIC_STORE_SSIZE_RELAXED(it->it_index, index);
4219
0
    }
4220
0
    Py_RETURN_NONE;
4221
0
}
4222
4223
/* common pickling support */
4224
4225
static PyObject *
4226
listiter_reduce_general(void *_it, int forward)
4227
0
{
4228
0
    PyObject *list;
4229
0
    PyObject *iter;
4230
4231
    /* _PyEval_GetBuiltin can invoke arbitrary code,
4232
     * call must be before access of iterator pointers.
4233
     * see issue #101765 */
4234
4235
0
    if (forward) {
4236
0
        iter = _PyEval_GetBuiltin(&_Py_ID(iter));
4237
0
        _PyListIterObject *it = (_PyListIterObject *)_it;
4238
0
        Py_ssize_t idx = FT_ATOMIC_LOAD_SSIZE_RELAXED(it->it_index);
4239
0
        if (idx >= 0) {
4240
0
            return Py_BuildValue("N(O)n", iter, it->it_seq, idx);
4241
0
        }
4242
0
    } else {
4243
0
        iter = _PyEval_GetBuiltin(&_Py_ID(reversed));
4244
0
        listreviterobject *it = (listreviterobject *)_it;
4245
0
        Py_ssize_t idx = FT_ATOMIC_LOAD_SSIZE_RELAXED(it->it_index);
4246
0
        if (idx >= 0) {
4247
0
            return Py_BuildValue("N(O)n", iter, it->it_seq, idx);
4248
0
        }
4249
0
    }
4250
    /* empty iterator, create an empty list */
4251
0
    list = PyList_New(0);
4252
0
    if (list == NULL)
4253
0
        return NULL;
4254
0
    return Py_BuildValue("N(N)", iter, list);
4255
0
}