Coverage Report

Created: 2025-07-11 06:24

/src/cpython/Objects/listobject.c
Line
Count
Source (jump to first uncovered line)
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_pyatomic_ft_wrappers.h"
10
#include "pycore_interp.h"        // PyInterpreterState.list
11
#include "pycore_list.h"          // struct _Py_list_freelist, _PyListIterObject
12
#include "pycore_long.h"          // _PyLong_DigitCount
13
#include "pycore_modsupport.h"    // _PyArg_NoKwnames()
14
#include "pycore_object.h"        // _PyObject_GC_TRACK(), _PyDebugAllocatorStats()
15
#include "pycore_stackref.h"      // _Py_TryIncrefCompareStackRef()
16
#include "pycore_tuple.h"         // _PyTuple_FromArray()
17
#include "pycore_typeobject.h"    // _Py_TYPE_VERSION_LIST
18
#include "pycore_setobject.h"     // _PySet_NextEntry()
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
107M
{
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
107M
    PyMem_Free(items);
72
107M
#endif
73
107M
}
74
75
static void
76
ensure_shared_on_resize(PyListObject *self)
77
79.4M
{
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
79.4M
}
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
95.0M
{
110
95.0M
    size_t new_allocated, target_bytes;
111
95.0M
    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
95.0M
    if (allocated >= newsize && newsize >= (allocated >> 1)) {
118
15.6M
        assert(self->ob_item != NULL || newsize == 0);
119
15.6M
        Py_SET_SIZE(self, newsize);
120
15.6M
        return 0;
121
15.6M
    }
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
79.4M
    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
79.4M
    if (newsize - Py_SIZE(self) > (Py_ssize_t)(new_allocated - newsize))
138
9.21k
        new_allocated = ((size_t)newsize + 3) & ~(size_t)3;
139
140
79.4M
    if (newsize == 0)
141
2.31k
        new_allocated = 0;
142
143
79.4M
    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
79.4M
    PyObject **items;
172
79.4M
    if (new_allocated <= (size_t)PY_SSIZE_T_MAX / sizeof(PyObject *)) {
173
79.4M
        target_bytes = new_allocated * sizeof(PyObject *);
174
79.4M
        items = (PyObject **)PyMem_Realloc(self->ob_item, target_bytes);
175
79.4M
    }
176
0
    else {
177
        // integer overflow
178
0
        items = NULL;
179
0
    }
180
79.4M
    if (items == NULL) {
181
0
        PyErr_NoMemory();
182
0
        return -1;
183
0
    }
184
79.4M
    self->ob_item = items;
185
79.4M
    Py_SET_SIZE(self, newsize);
186
79.4M
    self->allocated = new_allocated;
187
79.4M
#endif
188
79.4M
    return 0;
189
79.4M
}
190
191
static int
192
list_preallocate_exact(PyListObject *self, Py_ssize_t size)
193
5.80M
{
194
5.80M
    PyObject **items;
195
5.80M
    assert(self->ob_item == NULL);
196
5.80M
    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
5.80M
    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
5.80M
    items = PyMem_New(PyObject*, size);
214
5.80M
    if (items == NULL) {
215
0
        PyErr_NoMemory();
216
0
        return -1;
217
0
    }
218
5.80M
#endif
219
5.80M
    FT_ATOMIC_STORE_PTR_RELEASE(self->ob_item, items);
220
5.80M
    self->allocated = size;
221
5.80M
    return 0;
222
5.80M
}
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
239M
{
237
239M
    if (size < 0) {
238
0
        PyErr_BadInternalCall();
239
0
        return NULL;
240
0
    }
241
242
239M
    PyListObject *op = _Py_FREELIST_POP(PyListObject, lists);
243
239M
    if (op == NULL) {
244
29.1M
        op = PyObject_GC_New(PyListObject, &PyList_Type);
245
29.1M
        if (op == NULL) {
246
0
            return NULL;
247
0
        }
248
29.1M
    }
249
239M
    if (size <= 0) {
250
202M
        op->ob_item = NULL;
251
202M
    }
252
37.2M
    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
37.2M
        op->ob_item = (PyObject **) PyMem_Calloc(size, sizeof(PyObject *));
263
37.2M
#endif
264
37.2M
        if (op->ob_item == NULL) {
265
0
            Py_DECREF(op);
266
0
            return PyErr_NoMemory();
267
0
        }
268
37.2M
    }
269
239M
    Py_SET_SIZE(op, size);
270
239M
    op->allocated = size;
271
239M
    _PyObject_GC_TRACK(op);
272
239M
    return (PyObject *) op;
273
239M
}
274
275
static PyObject *
276
list_new_prealloc(Py_ssize_t size)
277
13.5M
{
278
13.5M
    assert(size > 0);
279
13.5M
    PyListObject *op = (PyListObject *) PyList_New(0);
280
13.5M
    if (op == NULL) {
281
0
        return NULL;
282
0
    }
283
13.5M
    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
13.5M
    op->ob_item = PyMem_New(PyObject *, size);
293
13.5M
    if (op->ob_item == NULL) {
294
0
        Py_DECREF(op);
295
0
        return PyErr_NoMemory();
296
0
    }
297
13.5M
#endif
298
13.5M
    op->allocated = size;
299
13.5M
    return (PyObject *) op;
300
13.5M
}
301
302
Py_ssize_t
303
PyList_Size(PyObject *op)
304
84.4k
{
305
84.4k
    if (!PyList_Check(op)) {
306
0
        PyErr_BadInternalCall();
307
0
        return -1;
308
0
    }
309
84.4k
    else {
310
84.4k
        return PyList_GET_SIZE(op);
311
84.4k
    }
312
84.4k
}
313
314
static inline int
315
valid_index(Py_ssize_t i, Py_ssize_t limit)
316
194M
{
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
194M
    return (size_t) i < (size_t) limit;
325
194M
}
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
150M
{
377
150M
    if (!valid_index(i, Py_SIZE(op))) {
378
27.9M
        return NULL;
379
27.9M
    }
380
122M
    return Py_NewRef(PyList_GET_ITEM(op, i));
381
150M
}
382
#endif
383
384
PyObject *
385
PyList_GetItem(PyObject *op, Py_ssize_t i)
386
444
{
387
444
    if (!PyList_Check(op)) {
388
0
        PyErr_BadInternalCall();
389
0
        return NULL;
390
0
    }
391
444
    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
444
    return ((PyListObject *)op) -> ob_item[i];
397
444
}
398
399
PyObject *
400
PyList_GetItemRef(PyObject *op, Py_ssize_t i)
401
78.6k
{
402
78.6k
    if (!PyList_Check(op)) {
403
0
        PyErr_SetString(PyExc_TypeError, "expected a list");
404
0
        return NULL;
405
0
    }
406
78.6k
    PyObject *item = list_get_item_ref((PyListObject *)op, i);
407
78.6k
    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
78.6k
    return item;
413
78.6k
}
414
415
PyObject *
416
_PyList_GetItemRef(PyListObject *list, Py_ssize_t i)
417
1.88k
{
418
1.88k
    return list_get_item_ref(list, i);
419
1.88k
}
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
20.1k
{
451
20.1k
    if (!PyList_Check(op)) {
452
0
        Py_XDECREF(newitem);
453
0
        PyErr_BadInternalCall();
454
0
        return -1;
455
0
    }
456
20.1k
    int ret;
457
20.1k
    PyListObject *self = ((PyListObject *)op);
458
20.1k
    Py_BEGIN_CRITICAL_SECTION(self);
459
20.1k
    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
20.1k
    PyObject *tmp = self->ob_item[i];
467
20.1k
    FT_ATOMIC_STORE_PTR_RELEASE(self->ob_item[i], newitem);
468
20.1k
    Py_XDECREF(tmp);
469
20.1k
    ret = 0;
470
20.1k
end:;
471
20.1k
    Py_END_CRITICAL_SECTION();
472
20.1k
    return ret;
473
20.1k
}
474
475
static int
476
ins1(PyListObject *self, Py_ssize_t where, PyObject *v)
477
76
{
478
76
    Py_ssize_t i, n = Py_SIZE(self);
479
76
    PyObject **items;
480
76
    if (v == NULL) {
481
0
        PyErr_BadInternalCall();
482
0
        return -1;
483
0
    }
484
485
76
    assert((size_t)n + 1 < PY_SSIZE_T_MAX);
486
76
    if (list_resize(self, n+1) < 0)
487
0
        return -1;
488
489
76
    if (where < 0) {
490
0
        where += n;
491
0
        if (where < 0)
492
0
            where = 0;
493
0
    }
494
76
    if (where > n)
495
0
        where = n;
496
76
    items = self->ob_item;
497
462
    for (i = n; --i >= where; )
498
386
        FT_ATOMIC_STORE_PTR_RELAXED(items[i+1], items[i]);
499
76
    FT_ATOMIC_STORE_PTR_RELEASE(items[where], Py_NewRef(v));
500
76
    return 0;
501
76
}
502
503
int
504
PyList_Insert(PyObject *op, Py_ssize_t where, PyObject *newitem)
505
16
{
506
16
    if (!PyList_Check(op)) {
507
0
        PyErr_BadInternalCall();
508
0
        return -1;
509
0
    }
510
16
    PyListObject *self = (PyListObject *)op;
511
16
    int err;
512
16
    Py_BEGIN_CRITICAL_SECTION(self);
513
16
    err = ins1(self, where, newitem);
514
16
    Py_END_CRITICAL_SECTION();
515
16
    return err;
516
16
}
517
518
/* internal, used by _PyList_AppendTakeRef */
519
int
520
_PyList_AppendTakeRefListResize(PyListObject *self, PyObject *newitem)
521
58.2M
{
522
58.2M
    Py_ssize_t len = Py_SIZE(self);
523
58.2M
    assert(self->allocated == -1 || self->allocated == len);
524
58.2M
    if (list_resize(self, len + 1) < 0) {
525
0
        Py_DECREF(newitem);
526
0
        return -1;
527
0
    }
528
58.2M
    FT_ATOMIC_STORE_PTR_RELEASE(self->ob_item[len], newitem);
529
58.2M
    return 0;
530
58.2M
}
531
532
int
533
PyList_Append(PyObject *op, PyObject *newitem)
534
157M
{
535
157M
    if (PyList_Check(op) && (newitem != NULL)) {
536
157M
        int ret;
537
157M
        Py_BEGIN_CRITICAL_SECTION(op);
538
157M
        ret = _PyList_AppendTakeRef((PyListObject *)op, Py_NewRef(newitem));
539
157M
        Py_END_CRITICAL_SECTION();
540
157M
        return ret;
541
157M
    }
542
0
    PyErr_BadInternalCall();
543
0
    return -1;
544
157M
}
545
546
/* Methods */
547
548
static void
549
list_dealloc(PyObject *self)
550
259M
{
551
259M
    PyListObject *op = (PyListObject *)self;
552
259M
    Py_ssize_t i;
553
259M
    PyObject_GC_UnTrack(op);
554
259M
    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
94.6M
        i = Py_SIZE(op);
560
1.54G
        while (--i >= 0) {
561
1.44G
            Py_XDECREF(op->ob_item[i]);
562
1.44G
        }
563
94.6M
        free_list_items(op->ob_item, false);
564
94.6M
        op->ob_item = NULL;
565
94.6M
    }
566
259M
    if (PyList_CheckExact(op)) {
567
248M
        _Py_FREELIST_FREE(lists, op, PyObject_GC_Del);
568
248M
    }
569
10.8M
    else {
570
10.8M
        PyObject_GC_Del(op);
571
10.8M
    }
572
259M
}
573
574
static PyObject *
575
list_repr_impl(PyListObject *v)
576
3.83M
{
577
3.83M
    int res = Py_ReprEnter((PyObject*)v);
578
3.83M
    if (res != 0) {
579
0
        return (res > 0 ? PyUnicode_FromString("[...]") : NULL);
580
0
    }
581
582
    /* "[" + "1" + ", 2" * (len - 1) + "]" */
583
3.83M
    Py_ssize_t prealloc = 1 + 1 + (2 + 1) * (Py_SIZE(v) - 1) + 1;
584
3.83M
    PyUnicodeWriter *writer = PyUnicodeWriter_Create(prealloc);
585
3.83M
    PyObject *item = NULL;
586
3.83M
    if (writer == NULL) {
587
0
        goto error;
588
0
    }
589
590
3.83M
    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
10.7M
    for (Py_ssize_t i = 0; i < Py_SIZE(v); ++i) {
597
        /* Hold a strong reference since repr(item) can mutate the list */
598
6.90M
        item = Py_NewRef(v->ob_item[i]);
599
600
6.90M
        if (i > 0) {
601
3.06M
            if (PyUnicodeWriter_WriteChar(writer, ',') < 0) {
602
0
                goto error;
603
0
            }
604
3.06M
            if (PyUnicodeWriter_WriteChar(writer, ' ') < 0) {
605
0
                goto error;
606
0
            }
607
3.06M
        }
608
609
6.90M
        if (PyUnicodeWriter_WriteRepr(writer, item) < 0) {
610
0
            goto error;
611
0
        }
612
6.90M
        Py_CLEAR(item);
613
6.90M
    }
614
615
3.83M
    if (PyUnicodeWriter_WriteChar(writer, ']') < 0) {
616
0
        goto error;
617
0
    }
618
619
3.83M
    Py_ReprLeave((PyObject *)v);
620
3.83M
    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
3.83M
}
628
629
static PyObject *
630
list_repr(PyObject *self)
631
3.84M
{
632
3.84M
    if (PyList_GET_SIZE(self) == 0) {
633
4.09k
        return PyUnicode_FromString("[]");
634
4.09k
    }
635
3.83M
    PyListObject *v = (PyListObject *)self;
636
3.83M
    PyObject *ret = NULL;
637
3.83M
    Py_BEGIN_CRITICAL_SECTION(v);
638
3.83M
    ret = list_repr_impl(v);
639
3.83M
    Py_END_CRITICAL_SECTION();
640
3.83M
    return ret;
641
3.84M
}
642
643
static Py_ssize_t
644
list_length(PyObject *a)
645
47.4M
{
646
47.4M
    return PyList_GET_SIZE(a);
647
47.4M
}
648
649
static int
650
list_contains(PyObject *aa, PyObject *el)
651
2.23k
{
652
653
28.4k
    for (Py_ssize_t i = 0; ; i++) {
654
28.4k
        PyObject *item = list_get_item_ref((PyListObject *)aa, i);
655
28.4k
        if (item == NULL) {
656
            // out-of-bounds
657
1.62k
            return 0;
658
1.62k
        }
659
26.7k
        int cmp = PyObject_RichCompareBool(item, el, Py_EQ);
660
26.7k
        Py_DECREF(item);
661
26.7k
        if (cmp != 0) {
662
608
            return cmp;
663
608
        }
664
26.7k
    }
665
0
    return 0;
666
2.23k
}
667
668
static PyObject *
669
list_item(PyObject *aa, Py_ssize_t i)
670
14.7M
{
671
14.7M
    PyListObject *a = (PyListObject *)aa;
672
14.7M
    if (!valid_index(i, PyList_GET_SIZE(a))) {
673
940
        PyErr_SetObject(PyExc_IndexError, &_Py_STR(list_err));
674
940
        return NULL;
675
940
    }
676
14.7M
    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
14.7M
    item = Py_NewRef(a->ob_item[i]);
685
14.7M
#endif
686
14.7M
    return item;
687
14.7M
}
688
689
static PyObject *
690
list_slice_lock_held(PyListObject *a, Py_ssize_t ilow, Py_ssize_t ihigh)
691
1.59M
{
692
1.59M
    PyListObject *np;
693
1.59M
    PyObject **src, **dest;
694
1.59M
    Py_ssize_t i, len;
695
1.59M
    len = ihigh - ilow;
696
1.59M
    if (len <= 0) {
697
0
        return PyList_New(0);
698
0
    }
699
1.59M
    np = (PyListObject *) list_new_prealloc(len);
700
1.59M
    if (np == NULL)
701
0
        return NULL;
702
703
1.59M
    src = a->ob_item + ilow;
704
1.59M
    dest = np->ob_item;
705
28.6M
    for (i = 0; i < len; i++) {
706
27.0M
        PyObject *v = src[i];
707
27.0M
        dest[i] = Py_NewRef(v);
708
27.0M
    }
709
1.59M
    Py_SET_SIZE(np, len);
710
1.59M
    return (PyObject *)np;
711
1.59M
}
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
19.1M
{
742
19.1M
    Py_ssize_t size;
743
19.1M
    Py_ssize_t i;
744
19.1M
    PyObject **src, **dest;
745
19.1M
    PyListObject *np;
746
19.1M
    assert((size_t)Py_SIZE(a) + (size_t)Py_SIZE(b) < PY_SSIZE_T_MAX);
747
19.1M
    size = Py_SIZE(a) + Py_SIZE(b);
748
19.1M
    if (size == 0) {
749
7.30M
        return PyList_New(0);
750
7.30M
    }
751
11.8M
    np = (PyListObject *) list_new_prealloc(size);
752
11.8M
    if (np == NULL) {
753
0
        return NULL;
754
0
    }
755
11.8M
    src = a->ob_item;
756
11.8M
    dest = np->ob_item;
757
594M
    for (i = 0; i < Py_SIZE(a); i++) {
758
582M
        PyObject *v = src[i];
759
582M
        dest[i] = Py_NewRef(v);
760
582M
    }
761
11.8M
    src = b->ob_item;
762
11.8M
    dest = np->ob_item + Py_SIZE(a);
763
200M
    for (i = 0; i < Py_SIZE(b); i++) {
764
188M
        PyObject *v = src[i];
765
188M
        dest[i] = Py_NewRef(v);
766
188M
    }
767
11.8M
    Py_SET_SIZE(np, size);
768
11.8M
    return (PyObject *)np;
769
11.8M
}
770
771
static PyObject *
772
list_concat(PyObject *aa, PyObject *bb)
773
19.1M
{
774
19.1M
    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
19.1M
    PyListObject *a = (PyListObject *)aa;
781
19.1M
    PyListObject *b = (PyListObject *)bb;
782
19.1M
    PyObject *ret;
783
19.1M
    Py_BEGIN_CRITICAL_SECTION2(a, b);
784
19.1M
    ret = list_concat_lock_held(a, b);
785
19.1M
    Py_END_CRITICAL_SECTION2();
786
19.1M
    return ret;
787
19.1M
}
788
789
static PyObject *
790
list_repeat_lock_held(PyListObject *a, Py_ssize_t n)
791
14.4k
{
792
14.4k
    const Py_ssize_t input_size = Py_SIZE(a);
793
14.4k
    if (input_size == 0 || n <= 0)
794
2.40k
        return PyList_New(0);
795
12.0k
    assert(n > 0);
796
797
12.0k
    if (input_size > PY_SSIZE_T_MAX / n)
798
0
        return PyErr_NoMemory();
799
12.0k
    Py_ssize_t output_size = input_size * n;
800
801
12.0k
    PyListObject *np = (PyListObject *) list_new_prealloc(output_size);
802
12.0k
    if (np == NULL)
803
0
        return NULL;
804
805
12.0k
    PyObject **dest = np->ob_item;
806
12.0k
    if (input_size == 1) {
807
12.0k
        PyObject *elem = a->ob_item[0];
808
12.0k
        _Py_RefcntAdd(elem, n);
809
12.0k
        PyObject **dest_end = dest + output_size;
810
11.1M
        while (dest < dest_end) {
811
11.1M
            *dest++ = elem;
812
11.1M
        }
813
12.0k
    }
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
12.0k
    Py_SET_SIZE(np, output_size);
828
12.0k
    return (PyObject *) np;
829
12.0k
}
830
831
static PyObject *
832
list_repeat(PyObject *aa, Py_ssize_t n)
833
14.4k
{
834
14.4k
    PyObject *ret;
835
14.4k
    PyListObject *a = (PyListObject *)aa;
836
14.4k
    Py_BEGIN_CRITICAL_SECTION(a);
837
14.4k
    ret = list_repeat_lock_held(a, n);
838
14.4k
    Py_END_CRITICAL_SECTION();
839
14.4k
    return ret;
840
14.4k
}
841
842
static void
843
list_clear_impl(PyListObject *a, bool is_resize)
844
12.7M
{
845
12.7M
    PyObject **items = a->ob_item;
846
12.7M
    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
12.7M
    Py_ssize_t i = Py_SIZE(a);
853
12.7M
    Py_SET_SIZE(a, 0);
854
12.7M
    FT_ATOMIC_STORE_PTR_RELEASE(a->ob_item, NULL);
855
12.7M
    a->allocated = 0;
856
25.6M
    while (--i >= 0) {
857
12.8M
        Py_XDECREF(items[i]);
858
12.8M
    }
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
12.7M
    bool use_qsbr = false;
866
12.7M
#endif
867
12.7M
    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
12.7M
}
871
872
static void
873
list_clear(PyListObject *a)
874
12.7M
{
875
12.7M
    list_clear_impl(a, true);
876
12.7M
}
877
878
static int
879
list_clear_slot(PyObject *self)
880
0
{
881
0
    list_clear_impl((PyListObject *)self, false);
882
0
    return 0;
883
0
}
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
4.03M
{
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
4.03M
    PyObject *recycle_on_stack[8];
901
4.03M
    PyObject **recycle = recycle_on_stack; /* will allocate more if needed */
902
4.03M
    PyObject **item;
903
4.03M
    PyObject **vitem = NULL;
904
4.03M
    PyObject *v_as_SF = NULL; /* PySequence_Fast(v) */
905
4.03M
    Py_ssize_t n; /* # of elements in replacement list */
906
4.03M
    Py_ssize_t norig; /* # of elements in list getting replaced */
907
4.03M
    Py_ssize_t d; /* Change in size */
908
4.03M
    Py_ssize_t k;
909
4.03M
    size_t s;
910
4.03M
    int result = -1;            /* guilty until proved innocent */
911
4.03M
#define b ((PyListObject *)v)
912
4.03M
    if (v == NULL)
913
3.85M
        n = 0;
914
184k
    else {
915
184k
        v_as_SF = PySequence_Fast(v, "can only assign an iterable");
916
184k
        if(v_as_SF == NULL)
917
0
            goto Error;
918
184k
        n = PySequence_Fast_GET_SIZE(v_as_SF);
919
184k
        vitem = PySequence_Fast_ITEMS(v_as_SF);
920
184k
    }
921
4.03M
    if (ilow < 0)
922
0
        ilow = 0;
923
4.03M
    else if (ilow > Py_SIZE(a))
924
0
        ilow = Py_SIZE(a);
925
926
4.03M
    if (ihigh < ilow)
927
0
        ihigh = ilow;
928
4.03M
    else if (ihigh > Py_SIZE(a))
929
0
        ihigh = Py_SIZE(a);
930
931
4.03M
    norig = ihigh - ilow;
932
4.03M
    assert(norig >= 0);
933
4.03M
    d = n - norig;
934
4.03M
    if (Py_SIZE(a) + d == 0) {
935
543k
        Py_XDECREF(v_as_SF);
936
543k
        list_clear(a);
937
543k
        return 0;
938
543k
    }
939
3.49M
    item = a->ob_item;
940
    /* recycle the items that we are about to remove */
941
3.49M
    s = norig * sizeof(PyObject *);
942
    /* If norig == 0, item might be NULL, in which case we may not memcpy from it. */
943
3.49M
    if (s) {
944
3.33M
        if (s > sizeof(recycle_on_stack)) {
945
100
            recycle = (PyObject **)PyMem_Malloc(s);
946
100
            if (recycle == NULL) {
947
0
                PyErr_NoMemory();
948
0
                goto Error;
949
0
            }
950
100
        }
951
3.33M
        memcpy(recycle, &item[ilow], s);
952
3.33M
    }
953
954
3.49M
    if (d < 0) { /* Delete -d items */
955
3.33M
        Py_ssize_t tail;
956
3.33M
        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
3.33M
        memmove(&item[ihigh+d], &item[ihigh], tail);
960
3.33M
        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
3.33M
        item = a->ob_item;
966
3.33M
    }
967
165k
    else if (d > 0) { /* Insert d items */
968
165k
        k = Py_SIZE(a);
969
165k
        if (list_resize(a, k+d) < 0)
970
0
            goto Error;
971
165k
        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
165k
        memmove(&item[ihigh+d], &item[ihigh],
975
165k
            (k - ihigh)*sizeof(PyObject *));
976
165k
    }
977
3.66M
    for (k = 0; k < n; k++, ilow++) {
978
165k
        PyObject *w = vitem[k];
979
165k
        FT_ATOMIC_STORE_PTR_RELEASE(item[ilow], Py_XNewRef(w));
980
165k
    }
981
6.83M
    for (k = norig - 1; k >= 0; --k)
982
3.33M
        Py_XDECREF(recycle[k]);
983
3.49M
    result = 0;
984
3.49M
 Error:
985
3.49M
    if (recycle != recycle_on_stack)
986
100
        PyMem_Free(recycle);
987
3.49M
    Py_XDECREF(v_as_SF);
988
3.49M
    return result;
989
3.49M
#undef b
990
3.49M
}
991
992
static int
993
list_ass_slice(PyListObject *a, Py_ssize_t ilow, Py_ssize_t ihigh, PyObject *v)
994
3.85M
{
995
3.85M
    int ret;
996
3.85M
    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
3.85M
    else if (v != NULL && PyList_CheckExact(v)) {
1010
167
        Py_BEGIN_CRITICAL_SECTION2(a, v);
1011
167
        ret = list_ass_slice_lock_held(a, ilow, ihigh, v);
1012
167
        Py_END_CRITICAL_SECTION2();
1013
167
    }
1014
3.85M
    else {
1015
3.85M
        Py_BEGIN_CRITICAL_SECTION(a);
1016
3.85M
        ret = list_ass_slice_lock_held(a, ilow, ihigh, v);
1017
3.85M
        Py_END_CRITICAL_SECTION();
1018
3.85M
    }
1019
3.85M
    return ret;
1020
3.85M
}
1021
1022
int
1023
PyList_SetSlice(PyObject *a, Py_ssize_t ilow, Py_ssize_t ihigh, PyObject *v)
1024
3.85M
{
1025
3.85M
    if (!PyList_Check(a)) {
1026
0
        PyErr_BadInternalCall();
1027
0
        return -1;
1028
0
    }
1029
3.85M
    return list_ass_slice((PyListObject *)a, ilow, ihigh, v);
1030
3.85M
}
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
8.42k
{
1085
8.42k
    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
8.42k
    PyObject *tmp = a->ob_item[i];
1091
8.42k
    if (v == NULL) {
1092
5.50k
        Py_ssize_t size = Py_SIZE(a);
1093
5.50k
        for (Py_ssize_t idx = i; idx < size - 1; idx++) {
1094
0
            FT_ATOMIC_STORE_PTR_RELAXED(a->ob_item[idx], a->ob_item[idx + 1]);
1095
0
        }
1096
5.50k
        Py_SET_SIZE(a, size - 1);
1097
5.50k
    }
1098
2.91k
    else {
1099
2.91k
        FT_ATOMIC_STORE_PTR_RELEASE(a->ob_item[i], Py_NewRef(v));
1100
2.91k
    }
1101
8.42k
    Py_DECREF(tmp);
1102
8.42k
    return 0;
1103
8.42k
}
1104
1105
static int
1106
list_ass_item(PyObject *aa, Py_ssize_t i, PyObject *v)
1107
5.40k
{
1108
5.40k
    int ret;
1109
5.40k
    PyListObject *a = (PyListObject *)aa;
1110
5.40k
    Py_BEGIN_CRITICAL_SECTION(a);
1111
5.40k
    ret = list_ass_item_lock_held(a, i, v);
1112
5.40k
    Py_END_CRITICAL_SECTION();
1113
5.40k
    return ret;
1114
5.40k
}
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
60
{
1131
60
    if (ins1(self, index, object) == 0) {
1132
60
        Py_RETURN_NONE;
1133
60
    }
1134
0
    return NULL;
1135
60
}
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
156
{
1148
156
    list_clear(self);
1149
156
    Py_RETURN_NONE;
1150
156
}
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
19.3M
{
1180
19.3M
    if (_PyList_AppendTakeRef(self, Py_NewRef(object)) < 0) {
1181
0
        return NULL;
1182
0
    }
1183
19.3M
    Py_RETURN_NONE;
1184
19.3M
}
1185
1186
static int
1187
list_extend_fast(PyListObject *self, PyObject *iterable)
1188
21.4M
{
1189
21.4M
    Py_ssize_t n = PySequence_Fast_GET_SIZE(iterable);
1190
21.4M
    if (n == 0) {
1191
        /* short circuit when iterable is empty */
1192
8.47M
        return 0;
1193
8.47M
    }
1194
1195
13.0M
    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
13.0M
    assert(m < PY_SSIZE_T_MAX - n);
1199
13.0M
    if (self->ob_item == NULL) {
1200
1.28M
        if (list_preallocate_exact(self, n) < 0) {
1201
0
            return -1;
1202
0
        }
1203
1.28M
        Py_SET_SIZE(self, n);
1204
1.28M
    }
1205
11.7M
    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
13.0M
    PyObject **src = PySequence_Fast_ITEMS(iterable);
1216
13.0M
    PyObject **dest = self->ob_item + m;
1217
41.3M
    for (Py_ssize_t i = 0; i < n; i++) {
1218
28.3M
        PyObject *o = src[i];
1219
28.3M
        FT_ATOMIC_STORE_PTR_RELEASE(dest[i], Py_NewRef(o));
1220
28.3M
    }
1221
13.0M
    return 0;
1222
13.0M
}
1223
1224
static int
1225
list_extend_iter_lock_held(PyListObject *self, PyObject *iterable)
1226
5.18M
{
1227
5.18M
    PyObject *it = PyObject_GetIter(iterable);
1228
5.18M
    if (it == NULL) {
1229
0
        return -1;
1230
0
    }
1231
5.18M
    PyObject *(*iternext)(PyObject *) = *Py_TYPE(it)->tp_iternext;
1232
1233
    /* Guess a result list size. */
1234
5.18M
    Py_ssize_t n = PyObject_LengthHint(iterable, 8);
1235
5.18M
    if (n < 0) {
1236
0
        Py_DECREF(it);
1237
0
        return -1;
1238
0
    }
1239
1240
5.18M
    Py_ssize_t m = Py_SIZE(self);
1241
5.18M
    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
5.18M
    else if (self->ob_item == NULL) {
1248
4.88M
        if (n && list_preallocate_exact(self, n) < 0)
1249
0
            goto error;
1250
4.88M
    }
1251
301k
    else {
1252
        /* Make room. */
1253
301k
        if (list_resize(self, m + n) < 0) {
1254
0
            goto error;
1255
0
        }
1256
1257
        /* Make the list sane again. */
1258
301k
        Py_SET_SIZE(self, m);
1259
301k
    }
1260
1261
    /* Run iterator to exhaustion. */
1262
57.4M
    for (;;) {
1263
57.4M
        PyObject *item = iternext(it);
1264
57.4M
        if (item == NULL) {
1265
5.18M
            if (PyErr_Occurred()) {
1266
691
                if (PyErr_ExceptionMatches(PyExc_StopIteration))
1267
0
                    PyErr_Clear();
1268
691
                else
1269
691
                    goto error;
1270
691
            }
1271
5.18M
            break;
1272
5.18M
        }
1273
1274
52.2M
        if (Py_SIZE(self) < self->allocated) {
1275
51.4M
            Py_ssize_t len = Py_SIZE(self);
1276
51.4M
            FT_ATOMIC_STORE_PTR_RELEASE(self->ob_item[len], item);  // steals item ref
1277
51.4M
            Py_SET_SIZE(self, len + 1);
1278
51.4M
        }
1279
802k
        else {
1280
802k
            if (_PyList_AppendTakeRef(self, item) < 0)
1281
0
                goto error;
1282
802k
        }
1283
52.2M
    }
1284
1285
    /* Cut back result list if initial guess was too large. */
1286
5.18M
    if (Py_SIZE(self) < self->allocated) {
1287
3.86M
        if (list_resize(self, Py_SIZE(self)) < 0)
1288
0
            goto error;
1289
3.86M
    }
1290
1291
5.18M
    Py_DECREF(it);
1292
5.18M
    return 0;
1293
1294
691
  error:
1295
691
    Py_DECREF(it);
1296
691
    return -1;
1297
5.18M
}
1298
1299
static int
1300
list_extend_lock_held(PyListObject *self, PyObject *iterable)
1301
21.4M
{
1302
21.4M
    PyObject *seq = PySequence_Fast(iterable, "argument must be iterable");
1303
21.4M
    if (!seq) {
1304
0
        return -1;
1305
0
    }
1306
1307
21.4M
    int res = list_extend_fast(self, seq);
1308
21.4M
    Py_DECREF(seq);
1309
21.4M
    return res;
1310
21.4M
}
1311
1312
static int
1313
list_extend_set(PyListObject *self, PySetObject *other)
1314
18.2k
{
1315
18.2k
    Py_ssize_t m = Py_SIZE(self);
1316
18.2k
    Py_ssize_t n = PySet_GET_SIZE(other);
1317
18.2k
    Py_ssize_t r = m + n;
1318
18.2k
    if (r == 0) {
1319
846
        return 0;
1320
846
    }
1321
17.3k
    if (list_resize(self, r) < 0) {
1322
0
        return -1;
1323
0
    }
1324
1325
17.3k
    assert(self->ob_item != NULL);
1326
    /* populate the end of self with iterable's items */
1327
17.3k
    Py_ssize_t setpos = 0;
1328
17.3k
    Py_hash_t hash;
1329
17.3k
    PyObject *key;
1330
17.3k
    PyObject **dest = self->ob_item + m;
1331
95.8k
    while (_PySet_NextEntryRef((PyObject *)other, &setpos, &key, &hash)) {
1332
78.4k
        FT_ATOMIC_STORE_PTR_RELEASE(*dest, key);
1333
78.4k
        dest++;
1334
78.4k
    }
1335
17.3k
    Py_SET_SIZE(self, r);
1336
17.3k
    return 0;
1337
17.3k
}
1338
1339
static int
1340
list_extend_dict(PyListObject *self, PyDictObject *dict, int which_item)
1341
285
{
1342
    // which_item: 0 for keys and 1 for values
1343
285
    Py_ssize_t m = Py_SIZE(self);
1344
285
    Py_ssize_t n = PyDict_GET_SIZE(dict);
1345
285
    Py_ssize_t r = m + n;
1346
285
    if (r == 0) {
1347
0
        return 0;
1348
0
    }
1349
285
    if (list_resize(self, r) < 0) {
1350
0
        return -1;
1351
0
    }
1352
1353
285
    assert(self->ob_item != NULL);
1354
285
    PyObject **dest = self->ob_item + m;
1355
285
    Py_ssize_t pos = 0;
1356
285
    PyObject *keyvalue[2];
1357
1.34k
    while (_PyDict_Next((PyObject *)dict, &pos, &keyvalue[0], &keyvalue[1], NULL)) {
1358
1.05k
        PyObject *obj = keyvalue[which_item];
1359
1.05k
        Py_INCREF(obj);
1360
1.05k
        FT_ATOMIC_STORE_PTR_RELEASE(*dest, obj);
1361
1.05k
        dest++;
1362
1.05k
    }
1363
1364
285
    Py_SET_SIZE(self, r);
1365
285
    return 0;
1366
285
}
1367
1368
static int
1369
list_extend_dictitems(PyListObject *self, PyDictObject *dict)
1370
0
{
1371
0
    Py_ssize_t m = Py_SIZE(self);
1372
0
    Py_ssize_t n = PyDict_GET_SIZE(dict);
1373
0
    Py_ssize_t r = m + n;
1374
0
    if (r == 0) {
1375
0
        return 0;
1376
0
    }
1377
0
    if (list_resize(self, r) < 0) {
1378
0
        return -1;
1379
0
    }
1380
1381
0
    assert(self->ob_item != NULL);
1382
0
    PyObject **dest = self->ob_item + m;
1383
0
    Py_ssize_t pos = 0;
1384
0
    Py_ssize_t i = 0;
1385
0
    PyObject *key, *value;
1386
0
    while (_PyDict_Next((PyObject *)dict, &pos, &key, &value, NULL)) {
1387
0
        PyObject *item = PyTuple_Pack(2, key, value);
1388
0
        if (item == NULL) {
1389
0
            Py_SET_SIZE(self, m + i);
1390
0
            return -1;
1391
0
        }
1392
0
        FT_ATOMIC_STORE_PTR_RELEASE(*dest, item);
1393
0
        dest++;
1394
0
        i++;
1395
0
    }
1396
1397
0
    Py_SET_SIZE(self, r);
1398
0
    return 0;
1399
0
}
1400
1401
static int
1402
_list_extend(PyListObject *self, PyObject *iterable)
1403
26.6M
{
1404
    // Special case:
1405
    // lists and tuples which can use PySequence_Fast ops
1406
26.6M
    int res = -1;
1407
26.6M
    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
26.6M
    else if (PyList_CheckExact(iterable)) {
1413
9.72M
        Py_BEGIN_CRITICAL_SECTION2(self, iterable);
1414
9.72M
        res = list_extend_lock_held(self, iterable);
1415
9.72M
        Py_END_CRITICAL_SECTION2();
1416
9.72M
    }
1417
16.9M
    else if (PyTuple_CheckExact(iterable)) {
1418
11.7M
        Py_BEGIN_CRITICAL_SECTION(self);
1419
11.7M
        res = list_extend_lock_held(self, iterable);
1420
11.7M
        Py_END_CRITICAL_SECTION();
1421
11.7M
    }
1422
5.20M
    else if (PyAnySet_CheckExact(iterable)) {
1423
18.2k
        Py_BEGIN_CRITICAL_SECTION2(self, iterable);
1424
18.2k
        res = list_extend_set(self, (PySetObject *)iterable);
1425
18.2k
        Py_END_CRITICAL_SECTION2();
1426
18.2k
    }
1427
5.18M
    else if (PyDict_CheckExact(iterable)) {
1428
285
        Py_BEGIN_CRITICAL_SECTION2(self, iterable);
1429
285
        res = list_extend_dict(self, (PyDictObject *)iterable, 0 /*keys*/);
1430
285
        Py_END_CRITICAL_SECTION2();
1431
285
    }
1432
5.18M
    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
5.18M
    else if (Py_IS_TYPE(iterable, &PyDictValues_Type)) {
1439
0
        PyDictObject *dict = ((_PyDictViewObject *)iterable)->dv_dict;
1440
0
        Py_BEGIN_CRITICAL_SECTION2(self, dict);
1441
0
        res = list_extend_dict(self, dict, 1 /*values*/);
1442
0
        Py_END_CRITICAL_SECTION2();
1443
0
    }
1444
5.18M
    else if (Py_IS_TYPE(iterable, &PyDictItems_Type)) {
1445
0
        PyDictObject *dict = ((_PyDictViewObject *)iterable)->dv_dict;
1446
0
        Py_BEGIN_CRITICAL_SECTION2(self, dict);
1447
0
        res = list_extend_dictitems(self, dict);
1448
0
        Py_END_CRITICAL_SECTION2();
1449
0
    }
1450
5.18M
    else {
1451
5.18M
        Py_BEGIN_CRITICAL_SECTION(self);
1452
5.18M
        res = list_extend_iter_lock_held(self, iterable);
1453
5.18M
        Py_END_CRITICAL_SECTION();
1454
5.18M
    }
1455
26.6M
    return res;
1456
26.6M
}
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
17.6M
{
1471
17.6M
    if (_list_extend(self, iterable) < 0) {
1472
691
        return NULL;
1473
691
    }
1474
17.6M
    Py_RETURN_NONE;
1475
17.6M
}
1476
1477
PyObject *
1478
_PyList_Extend(PyListObject *self, PyObject *iterable)
1479
16.4M
{
1480
16.4M
    return list_extend((PyObject*)self, iterable);
1481
16.4M
}
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
370
{
1511
370
    PyListObject *self = (PyListObject *)_self;
1512
370
    if (_list_extend(self, other) < 0) {
1513
0
        return NULL;
1514
0
    }
1515
370
    return Py_NewRef(self);
1516
370
}
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
29.6M
{
1534
29.6M
    PyObject *v;
1535
29.6M
    int status;
1536
1537
29.6M
    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
29.6M
    if (index < 0)
1543
11.9M
        index += Py_SIZE(self);
1544
29.6M
    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
29.6M
    PyObject **items = self->ob_item;
1550
29.6M
    v = items[index];
1551
29.6M
    const Py_ssize_t size_after_pop = Py_SIZE(self) - 1;
1552
29.6M
    if (size_after_pop == 0) {
1553
12.2M
        Py_INCREF(v);
1554
12.2M
        list_clear(self);
1555
12.2M
        status = 0;
1556
12.2M
    }
1557
17.3M
    else {
1558
17.3M
        if ((size_after_pop - index) > 0) {
1559
11.1M
            memmove(&items[index], &items[index+1], (size_after_pop - index) * sizeof(PyObject *));
1560
11.1M
        }
1561
17.3M
        status = list_resize(self, size_after_pop);
1562
17.3M
    }
1563
29.6M
    if (status >= 0) {
1564
29.6M
        return v; // and v now owns the reference the list had
1565
29.6M
    }
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
29.6M
}
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
94.1k
{
1578
94.1k
    assert(lo && hi);
1579
1580
94.1k
    --hi;
1581
380k
    while (lo < hi) {
1582
286k
        PyObject *t = *lo;
1583
286k
        *lo = *hi;
1584
286k
        *hi = t;
1585
286k
        ++lo;
1586
286k
        --hi;
1587
286k
    }
1588
94.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
27.8k
{
1611
27.8k
    s1->keys[i] = s2->keys[j];
1612
27.8k
    if (s1->values != NULL)
1613
26.9k
        s1->values[i] = s2->values[j];
1614
27.8k
}
1615
1616
Py_LOCAL_INLINE(void)
1617
sortslice_copy_incr(sortslice *dst, sortslice *src)
1618
452k
{
1619
452k
    *dst->keys++ = *src->keys++;
1620
452k
    if (dst->values != NULL)
1621
267k
        *dst->values++ = *src->values++;
1622
452k
}
1623
1624
Py_LOCAL_INLINE(void)
1625
sortslice_copy_decr(sortslice *dst, sortslice *src)
1626
229k
{
1627
229k
    *dst->keys-- = *src->keys--;
1628
229k
    if (dst->values != NULL)
1629
167k
        *dst->values-- = *src->values--;
1630
229k
}
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
152k
{
1637
152k
    memcpy(&s1->keys[i], &s2->keys[j], sizeof(PyObject *) * n);
1638
152k
    if (s1->values != NULL)
1639
123k
        memcpy(&s1->values[i], &s2->values[j], sizeof(PyObject *) * n);
1640
152k
}
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
104k
{
1646
104k
    memmove(&s1->keys[i], &s2->keys[j], sizeof(PyObject *) * n);
1647
104k
    if (s1->values != NULL)
1648
82.2k
        memmove(&s1->values[i], &s2->values[j], sizeof(PyObject *) * n);
1649
104k
}
1650
1651
Py_LOCAL_INLINE(void)
1652
sortslice_advance(sortslice *slice, Py_ssize_t n)
1653
623k
{
1654
623k
    slice->keys += n;
1655
623k
    if (slice->values != NULL)
1656
459k
        slice->values += n;
1657
623k
}
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
12.0M
#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
11.5M
#define IFLT(X, Y) if ((k = ISLT(X, Y)) < 0) goto fail;  \
1671
11.5M
           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
842k
#define MIN_GALLOP 7
1684
1685
/* Avoid malloc for small temp arrays. */
1686
1.64M
#define MERGESTATE_TEMP_SIZE 256
1687
1688
/* The largest value of minrun. This must be a power of 2, and >= 1 */
1689
696k
#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
109k
{
1770
109k
    Py_ssize_t k; /* for IFLT macro expansion */
1771
109k
    PyObject ** const a = ss->keys;
1772
109k
    PyObject ** const v = ss->values;
1773
109k
    const bool has_values = v != NULL;
1774
109k
    PyObject *pivot;
1775
109k
    Py_ssize_t M;
1776
1777
109k
    assert(0 <= ok && ok <= n && 1 <= n && n <= MAX_MINRUN);
1778
    /* assert a[:ok] is sorted */
1779
109k
    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
109k
    Py_ssize_t L, R;
1832
1.79M
    for (; ok < n; ++ok) {
1833
        /* set L to where a[ok] belongs */
1834
1.68M
        L = 0;
1835
1.68M
        R = ok;
1836
1.68M
        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
1.68M
        assert(L < R);
1843
7.10M
        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
7.10M
            M = (L + R) >> 1;
1847
7.10M
#if 1 // straightforward, but highly unpredictable branch on random data
1848
7.10M
            IFLT(pivot, a[M])
1849
2.96M
                R = M;
1850
4.14M
            else
1851
4.14M
                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
7.10M
        } while (L < R);
1866
1.68M
        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
12.4M
        for (M = ok; M > L; --M)
1873
10.7M
            a[M] = a[M - 1];
1874
1.68M
        a[L] = pivot;
1875
1.68M
        if (has_values) {
1876
1.19M
            pivot = v[ok];
1877
6.19M
            for (M = ok; M > L; --M)
1878
5.00M
                v[M] = v[M - 1];
1879
1.19M
            v[L] = pivot;
1880
1.19M
        }
1881
1.68M
    }
1882
109k
#endif // pick binary or regular insertion sort
1883
109k
    return 0;
1884
1885
0
 fail:
1886
0
    return -1;
1887
109k
}
1888
1889
static void
1890
sortslice_reverse(sortslice *s, Py_ssize_t n)
1891
57.0k
{
1892
57.0k
    reverse_slice(s->keys, &s->keys[n]);
1893
57.0k
    if (s->values != NULL)
1894
37.0k
        reverse_slice(s->values, &s->values[n]);
1895
57.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
219k
{
1908
219k
    Py_ssize_t k; /* used by IFLT macro expansion */
1909
219k
    Py_ssize_t n;
1910
219k
    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
219k
#define REVERSE_LAST_NEQ                        \
1919
219k
    if (neq) {                                  \
1920
4.45k
        sortslice slice = *slo;                 \
1921
4.45k
        ++neq;                                  \
1922
4.45k
        sortslice_advance(&slice, n - neq);     \
1923
4.45k
        sortslice_reverse(&slice, neq);         \
1924
4.45k
        neq = 0;                                \
1925
4.45k
    }
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
219k
#define IF_NEXT_LARGER  IFLT(lo[n-1], lo[n])
1933
2.64M
#define IF_NEXT_SMALLER IFLT(lo[n], lo[n-1])
1934
1935
219k
    assert(nremaining);
1936
    /* try ascending run first */
1937
2.29M
    for (n = 1; n < nremaining; ++n) {
1938
2.22M
        IF_NEXT_SMALLER
1939
147k
            break;
1940
2.22M
    }
1941
219k
    if (n == nremaining)
1942
72.5k
        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
147k
    if (n > 1) {
1954
103k
        IFLT(lo[0], lo[n-1])
1955
99.1k
            return n;
1956
4.63k
        sortslice_reverse(slo, n);
1957
4.63k
    }
1958
48.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
48.0k
    Py_ssize_t neq = 0;
1965
75.2k
    for ( ; n < nremaining; ++n) {
1966
55.0k
        IF_NEXT_SMALLER {
1967
            /* This ends the most recent run of equal elements, but still in
1968
             * the "descending" direction.
1969
             */
1970
11.1k
            REVERSE_LAST_NEQ
1971
11.1k
        }
1972
43.9k
        else {
1973
43.9k
            IF_NEXT_LARGER /* descending run is over */
1974
27.8k
                break;
1975
16.1k
            else /* not x < y and not y < x implies x == y */
1976
16.1k
                ++neq;
1977
43.9k
        }
1978
55.0k
    }
1979
48.0k
    REVERSE_LAST_NEQ
1980
48.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
387k
    for ( ; n < nremaining; ++n) {
1987
366k
        IF_NEXT_SMALLER
1988
26.6k
            break;
1989
366k
    }
1990
1991
48.0k
    return n;
1992
0
fail:
1993
0
    return -1;
1994
1995
48.0k
#undef REVERSE_LAST_NEQ
1996
48.0k
#undef IF_NEXT_SMALLER
1997
48.0k
#undef IF_NEXT_LARGER
1998
48.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
145k
{
2024
145k
    Py_ssize_t ofs;
2025
145k
    Py_ssize_t lastofs;
2026
145k
    Py_ssize_t k;
2027
2028
145k
    assert(key && a && n > 0 && hint >= 0 && hint < n);
2029
2030
145k
    a += hint;
2031
145k
    lastofs = 0;
2032
145k
    ofs = 1;
2033
145k
    IFLT(*a, key) {
2034
        /* a[hint] < key -- gallop right, until
2035
         * a[hint + lastofs] < key <= a[hint + ofs]
2036
         */
2037
84.5k
        const Py_ssize_t maxofs = n - hint;             /* &a[n-1] is highest */
2038
288k
        while (ofs < maxofs) {
2039
228k
            IFLT(a[ofs], key) {
2040
203k
                lastofs = ofs;
2041
203k
                assert(ofs <= (PY_SSIZE_T_MAX - 1) / 2);
2042
203k
                ofs = (ofs << 1) + 1;
2043
203k
            }
2044
24.9k
            else                /* key <= a[hint + ofs] */
2045
24.9k
                break;
2046
228k
        }
2047
84.5k
        if (ofs > maxofs)
2048
26.2k
            ofs = maxofs;
2049
        /* Translate back to offsets relative to &a[0]. */
2050
84.5k
        lastofs += hint;
2051
84.5k
        ofs += hint;
2052
84.5k
    }
2053
61.2k
    else {
2054
        /* key <= a[hint] -- gallop left, until
2055
         * a[hint - ofs] < key <= a[hint - lastofs]
2056
         */
2057
61.2k
        const Py_ssize_t maxofs = hint + 1;             /* &a[0] is lowest */
2058
203k
        while (ofs < maxofs) {
2059
183k
            IFLT(*(a-ofs), key)
2060
40.8k
                break;
2061
            /* key <= a[hint - ofs] */
2062
142k
            lastofs = ofs;
2063
142k
            assert(ofs <= (PY_SSIZE_T_MAX - 1) / 2);
2064
142k
            ofs = (ofs << 1) + 1;
2065
142k
        }
2066
61.2k
        if (ofs > maxofs)
2067
15.0k
            ofs = maxofs;
2068
        /* Translate back to positive offsets relative to &a[0]. */
2069
61.2k
        k = lastofs;
2070
61.2k
        lastofs = hint - ofs;
2071
61.2k
        ofs = hint - k;
2072
61.2k
    }
2073
145k
    a -= hint;
2074
2075
145k
    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
145k
    ++lastofs;
2081
438k
    while (lastofs < ofs) {
2082
292k
        Py_ssize_t m = lastofs + ((ofs - lastofs) >> 1);
2083
2084
292k
        IFLT(a[m], key)
2085
152k
            lastofs = m+1;              /* a[m] < key */
2086
140k
        else
2087
140k
            ofs = m;                    /* key <= a[m] */
2088
292k
    }
2089
145k
    assert(lastofs == ofs);             /* so a[ofs-1] < key <= a[ofs] */
2090
145k
    return ofs;
2091
2092
0
fail:
2093
0
    return -1;
2094
145k
}
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
163k
{
2113
163k
    Py_ssize_t ofs;
2114
163k
    Py_ssize_t lastofs;
2115
163k
    Py_ssize_t k;
2116
2117
163k
    assert(key && a && n > 0 && hint >= 0 && hint < n);
2118
2119
163k
    a += hint;
2120
163k
    lastofs = 0;
2121
163k
    ofs = 1;
2122
163k
    IFLT(key, *a) {
2123
        /* key < a[hint] -- gallop left, until
2124
         * a[hint - ofs] <= key < a[hint - lastofs]
2125
         */
2126
63.9k
        const Py_ssize_t maxofs = hint + 1;             /* &a[0] is lowest */
2127
145k
        while (ofs < maxofs) {
2128
92.0k
            IFLT(key, *(a-ofs)) {
2129
81.8k
                lastofs = ofs;
2130
81.8k
                assert(ofs <= (PY_SSIZE_T_MAX - 1) / 2);
2131
81.8k
                ofs = (ofs << 1) + 1;
2132
81.8k
            }
2133
10.2k
            else                /* a[hint - ofs] <= key */
2134
10.2k
                break;
2135
92.0k
        }
2136
63.9k
        if (ofs > maxofs)
2137
10.0k
            ofs = maxofs;
2138
        /* Translate back to positive offsets relative to &a[0]. */
2139
63.9k
        k = lastofs;
2140
63.9k
        lastofs = hint - ofs;
2141
63.9k
        ofs = hint - k;
2142
63.9k
    }
2143
99.4k
    else {
2144
        /* a[hint] <= key -- gallop right, until
2145
         * a[hint + lastofs] <= key < a[hint + ofs]
2146
        */
2147
99.4k
        const Py_ssize_t maxofs = n - hint;             /* &a[n-1] is highest */
2148
343k
        while (ofs < maxofs) {
2149
299k
            IFLT(key, a[ofs])
2150
56.0k
                break;
2151
            /* a[hint + ofs] <= key */
2152
243k
            lastofs = ofs;
2153
243k
            assert(ofs <= (PY_SSIZE_T_MAX - 1) / 2);
2154
243k
            ofs = (ofs << 1) + 1;
2155
243k
        }
2156
99.4k
        if (ofs > maxofs)
2157
25.6k
            ofs = maxofs;
2158
        /* Translate back to offsets relative to &a[0]. */
2159
99.4k
        lastofs += hint;
2160
99.4k
        ofs += hint;
2161
99.4k
    }
2162
163k
    a -= hint;
2163
2164
163k
    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
163k
    ++lastofs;
2170
442k
    while (lastofs < ofs) {
2171
279k
        Py_ssize_t m = lastofs + ((ofs - lastofs) >> 1);
2172
2173
279k
        IFLT(key, a[m])
2174
145k
            ofs = m;                    /* key < a[m] */
2175
133k
        else
2176
133k
            lastofs = m+1;              /* a[m] <= key */
2177
279k
    }
2178
163k
    assert(lastofs == ofs);             /* so a[ofs-1] <= key < a[ofs] */
2179
163k
    return ofs;
2180
2181
0
fail:
2182
0
    return -1;
2183
163k
}
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
675k
{
2190
675k
    assert(ms != NULL);
2191
675k
    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
482k
        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
482k
        if (MERGESTATE_TEMP_SIZE / 2 < ms->alloced)
2203
1.81k
            ms->alloced = MERGESTATE_TEMP_SIZE / 2;
2204
482k
        ms->a.values = &ms->temparray[ms->alloced];
2205
482k
    }
2206
192k
    else {
2207
192k
        ms->alloced = MERGESTATE_TEMP_SIZE;
2208
192k
        ms->a.values = NULL;
2209
192k
    }
2210
675k
    ms->a.keys = ms->temparray;
2211
675k
    ms->n = 0;
2212
675k
    ms->min_gallop = MIN_GALLOP;
2213
675k
    ms->listlen = list_size;
2214
675k
    ms->basekeys = lo->keys;
2215
2216
    /* State for generating minrun values. See listsort.txt. */
2217
675k
    ms->mr_e = 0;
2218
696k
    while (list_size >> ms->mr_e >= MAX_MINRUN) {
2219
21.4k
        ++ms->mr_e;
2220
21.4k
    }
2221
675k
    ms->mr_mask = (1 << ms->mr_e) - 1;
2222
675k
    ms->mr_current = 0;
2223
675k
}
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
679k
{
2232
679k
    assert(ms != NULL);
2233
679k
    if (ms->a.keys != ms->temparray) {
2234
3.95k
        PyMem_Free(ms->a.keys);
2235
3.95k
        ms->a.keys = NULL;
2236
3.95k
    }
2237
679k
}
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
3.95k
{
2245
3.95k
    int multiplier;
2246
2247
3.95k
    assert(ms != NULL);
2248
3.95k
    if (need <= ms->alloced)
2249
0
        return 0;
2250
2251
3.95k
    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
3.95k
    merge_freemem(ms);
2257
3.95k
    if ((size_t)need > PY_SSIZE_T_MAX / sizeof(PyObject *) / multiplier) {
2258
0
        PyErr_NoMemory();
2259
0
        return -1;
2260
0
    }
2261
3.95k
    ms->a.keys = (PyObject **)PyMem_Malloc(multiplier * need
2262
3.95k
                                          * sizeof(PyObject *));
2263
3.95k
    if (ms->a.keys != NULL) {
2264
3.95k
        ms->alloced = need;
2265
3.95k
        if (ms->a.values != NULL)
2266
3.53k
            ms->a.values = &ms->a.keys[need];
2267
3.95k
        return 0;
2268
3.95k
    }
2269
0
    PyErr_NoMemory();
2270
0
    return -1;
2271
3.95k
}
2272
57.4k
#define MERGE_GETMEM(MS, NEED) ((NEED) <= (MS)->alloced ? 0 :   \
2273
57.4k
                                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
37.9k
{
2285
37.9k
    Py_ssize_t k;
2286
37.9k
    sortslice dest;
2287
37.9k
    int result = -1;            /* guilty until proved innocent */
2288
37.9k
    Py_ssize_t min_gallop;
2289
2290
37.9k
    assert(ms && ssa.keys && ssb.keys && na > 0 && nb > 0);
2291
37.9k
    assert(ssa.keys + na == ssb.keys);
2292
37.9k
    if (MERGE_GETMEM(ms, na) < 0)
2293
0
        return -1;
2294
37.9k
    sortslice_memcpy(&ms->a, 0, &ssa, 0, na);
2295
37.9k
    dest = ssa;
2296
37.9k
    ssa = ms->a;
2297
2298
37.9k
    sortslice_copy_incr(&dest, &ssb);
2299
37.9k
    --nb;
2300
37.9k
    if (nb == 0)
2301
2.14k
        goto Succeed;
2302
35.8k
    if (na == 1)
2303
5.66k
        goto CopyB;
2304
2305
30.1k
    min_gallop = ms->min_gallop;
2306
45.1k
    for (;;) {
2307
45.1k
        Py_ssize_t acount = 0;          /* # of times A won in a row */
2308
45.1k
        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
304k
        for (;;) {
2314
304k
            assert(na > 1 && nb > 0);
2315
304k
            k = ISLT(ssb.keys[0], ssa.keys[0]);
2316
304k
            if (k) {
2317
164k
                if (k < 0)
2318
0
                    goto Fail;
2319
164k
                sortslice_copy_incr(&dest, &ssb);
2320
164k
                ++bcount;
2321
164k
                acount = 0;
2322
164k
                --nb;
2323
164k
                if (nb == 0)
2324
3.13k
                    goto Succeed;
2325
161k
                if (bcount >= min_gallop)
2326
21.3k
                    break;
2327
161k
            }
2328
140k
            else {
2329
140k
                sortslice_copy_incr(&dest, &ssa);
2330
140k
                ++acount;
2331
140k
                bcount = 0;
2332
140k
                --na;
2333
140k
                if (na == 1)
2334
2.20k
                    goto CopyB;
2335
137k
                if (acount >= min_gallop)
2336
18.4k
                    break;
2337
137k
            }
2338
304k
        }
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
39.8k
        ++min_gallop;
2346
69.0k
        do {
2347
69.0k
            assert(na > 1 && nb > 0);
2348
69.0k
            min_gallop -= min_gallop > 1;
2349
69.0k
            ms->min_gallop = min_gallop;
2350
69.0k
            k = gallop_right(ms, ssb.keys[0], ssa.keys, na, 0);
2351
69.0k
            acount = k;
2352
69.0k
            if (k) {
2353
40.3k
                if (k < 0)
2354
0
                    goto Fail;
2355
40.3k
                sortslice_memcpy(&dest, 0, &ssa, 0, k);
2356
40.3k
                sortslice_advance(&dest, k);
2357
40.3k
                sortslice_advance(&ssa, k);
2358
40.3k
                na -= k;
2359
40.3k
                if (na == 1)
2360
6.96k
                    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
33.4k
                if (na == 0)
2366
0
                    goto Succeed;
2367
33.4k
            }
2368
62.0k
            sortslice_copy_incr(&dest, &ssb);
2369
62.0k
            --nb;
2370
62.0k
            if (nb == 0)
2371
1.71k
                goto Succeed;
2372
2373
60.3k
            k = gallop_left(ms, ssa.keys[0], ssb.keys, nb, 0);
2374
60.3k
            bcount = k;
2375
60.3k
            if (k) {
2376
55.4k
                if (k < 0)
2377
0
                    goto Fail;
2378
55.4k
                sortslice_memmove(&dest, 0, &ssb, 0, k);
2379
55.4k
                sortslice_advance(&dest, k);
2380
55.4k
                sortslice_advance(&ssb, k);
2381
55.4k
                nb -= k;
2382
55.4k
                if (nb == 0)
2383
13.1k
                    goto Succeed;
2384
55.4k
            }
2385
47.1k
            sortslice_copy_incr(&dest, &ssa);
2386
47.1k
            --na;
2387
47.1k
            if (na == 1)
2388
2.97k
                goto CopyB;
2389
47.1k
        } while (acount >= MIN_GALLOP || bcount >= MIN_GALLOP);
2390
14.9k
        ++min_gallop;           /* penalize it for leaving galloping mode */
2391
14.9k
        ms->min_gallop = min_gallop;
2392
14.9k
    }
2393
20.1k
Succeed:
2394
20.1k
    result = 0;
2395
20.1k
Fail:
2396
20.1k
    if (na)
2397
20.1k
        sortslice_memcpy(&dest, 0, &ssa, 0, na);
2398
20.1k
    return result;
2399
17.8k
CopyB:
2400
17.8k
    assert(na == 1 && nb > 0);
2401
    /* The last element of ssa belongs at the end of the merge. */
2402
17.8k
    sortslice_memmove(&dest, 0, &ssb, 0, nb);
2403
17.8k
    sortslice_copy(&dest, nb, &ssa, 0);
2404
17.8k
    return 0;
2405
20.1k
}
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
19.4k
{
2417
19.4k
    Py_ssize_t k;
2418
19.4k
    sortslice dest, basea, baseb;
2419
19.4k
    int result = -1;            /* guilty until proved innocent */
2420
19.4k
    Py_ssize_t min_gallop;
2421
2422
19.4k
    assert(ms && ssa.keys && ssb.keys && na > 0 && nb > 0);
2423
19.4k
    assert(ssa.keys + na == ssb.keys);
2424
19.4k
    if (MERGE_GETMEM(ms, nb) < 0)
2425
0
        return -1;
2426
19.4k
    dest = ssb;
2427
19.4k
    sortslice_advance(&dest, nb-1);
2428
19.4k
    sortslice_memcpy(&ms->a, 0, &ssb, 0, nb);
2429
19.4k
    basea = ssa;
2430
19.4k
    baseb = ms->a;
2431
19.4k
    ssb.keys = ms->a.keys + nb - 1;
2432
19.4k
    if (ssb.values != NULL)
2433
16.5k
        ssb.values = ms->a.values + nb - 1;
2434
19.4k
    sortslice_advance(&ssa, na - 1);
2435
2436
19.4k
    sortslice_copy_decr(&dest, &ssa);
2437
19.4k
    --na;
2438
19.4k
    if (na == 0)
2439
0
        goto Succeed;
2440
19.4k
    if (nb == 1)
2441
691
        goto CopyA;
2442
2443
18.7k
    min_gallop = ms->min_gallop;
2444
23.8k
    for (;;) {
2445
23.8k
        Py_ssize_t acount = 0;          /* # of times A won in a row */
2446
23.8k
        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
161k
        for (;;) {
2452
161k
            assert(na > 0 && nb > 1);
2453
161k
            k = ISLT(ssb.keys[0], ssa.keys[0]);
2454
161k
            if (k) {
2455
72.7k
                if (k < 0)
2456
0
                    goto Fail;
2457
72.7k
                sortslice_copy_decr(&dest, &ssa);
2458
72.7k
                ++acount;
2459
72.7k
                bcount = 0;
2460
72.7k
                --na;
2461
72.7k
                if (na == 0)
2462
340
                    goto Succeed;
2463
72.3k
                if (acount >= min_gallop)
2464
10.3k
                    break;
2465
72.3k
            }
2466
88.6k
            else {
2467
88.6k
                sortslice_copy_decr(&dest, &ssb);
2468
88.6k
                ++bcount;
2469
88.6k
                acount = 0;
2470
88.6k
                --nb;
2471
88.6k
                if (nb == 1)
2472
290
                    goto CopyA;
2473
88.3k
                if (bcount >= min_gallop)
2474
12.8k
                    break;
2475
88.3k
            }
2476
161k
        }
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
23.2k
        ++min_gallop;
2484
36.8k
        do {
2485
36.8k
            assert(na > 0 && nb > 1);
2486
36.8k
            min_gallop -= min_gallop > 1;
2487
36.8k
            ms->min_gallop = min_gallop;
2488
36.8k
            k = gallop_right(ms, ssb.keys[0], basea.keys, na, na-1);
2489
36.8k
            if (k < 0)
2490
0
                goto Fail;
2491
36.8k
            k = na - k;
2492
36.8k
            acount = k;
2493
36.8k
            if (k) {
2494
21.0k
                sortslice_advance(&dest, -k);
2495
21.0k
                sortslice_advance(&ssa, -k);
2496
21.0k
                sortslice_memmove(&dest, 1, &ssa, 1, k);
2497
21.0k
                na -= k;
2498
21.0k
                if (na == 0)
2499
8.03k
                    goto Succeed;
2500
21.0k
            }
2501
28.8k
            sortslice_copy_decr(&dest, &ssb);
2502
28.8k
            --nb;
2503
28.8k
            if (nb == 1)
2504
740
                goto CopyA;
2505
2506
28.1k
            k = gallop_left(ms, ssa.keys[0], baseb.keys, nb, nb-1);
2507
28.1k
            if (k < 0)
2508
0
                goto Fail;
2509
28.1k
            k = nb - k;
2510
28.1k
            bcount = k;
2511
28.1k
            if (k) {
2512
24.8k
                sortslice_advance(&dest, -k);
2513
24.8k
                sortslice_advance(&ssb, -k);
2514
24.8k
                sortslice_memcpy(&dest, 1, &ssb, 1, k);
2515
24.8k
                nb -= k;
2516
24.8k
                if (nb == 1)
2517
8.30k
                    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
16.4k
                if (nb == 0)
2523
0
                    goto Succeed;
2524
16.4k
            }
2525
19.8k
            sortslice_copy_decr(&dest, &ssa);
2526
19.8k
            --na;
2527
19.8k
            if (na == 0)
2528
1.00k
                goto Succeed;
2529
19.8k
        } while (acount >= MIN_GALLOP || bcount >= MIN_GALLOP);
2530
5.13k
        ++min_gallop;           /* penalize it for leaving galloping mode */
2531
5.13k
        ms->min_gallop = min_gallop;
2532
5.13k
    }
2533
9.37k
Succeed:
2534
9.37k
    result = 0;
2535
9.37k
Fail:
2536
9.37k
    if (nb)
2537
9.37k
        sortslice_memcpy(&dest, -(nb-1), &baseb, 0, nb);
2538
9.37k
    return result;
2539
10.0k
CopyA:
2540
10.0k
    assert(nb == 1 && na > 0);
2541
    /* The first element of ssb belongs at the front of the merge. */
2542
10.0k
    sortslice_memmove(&dest, 1-na, &ssa, 1-na, na);
2543
10.0k
    sortslice_advance(&dest, -na);
2544
10.0k
    sortslice_advance(&ssa, -na);
2545
10.0k
    sortslice_copy(&dest, 0, &ssb, 0);
2546
10.0k
    return 0;
2547
9.37k
}
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
57.4k
{
2555
57.4k
    sortslice ssa, ssb;
2556
57.4k
    Py_ssize_t na, nb;
2557
57.4k
    Py_ssize_t k;
2558
2559
57.4k
    assert(ms != NULL);
2560
57.4k
    assert(ms->n >= 2);
2561
57.4k
    assert(i >= 0);
2562
57.4k
    assert(i == ms->n - 2 || i == ms->n - 3);
2563
2564
57.4k
    ssa = ms->pending[i].base;
2565
57.4k
    na = ms->pending[i].len;
2566
57.4k
    ssb = ms->pending[i+1].base;
2567
57.4k
    nb = ms->pending[i+1].len;
2568
57.4k
    assert(na > 0 && nb > 0);
2569
57.4k
    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
57.4k
    ms->pending[i].len = na + nb;
2576
57.4k
    if (i == ms->n - 3)
2577
262
        ms->pending[i+1] = ms->pending[i+2];
2578
57.4k
    --ms->n;
2579
2580
    /* Where does b start in a?  Elements in a before that can be
2581
     * ignored (already in place).
2582
     */
2583
57.4k
    k = gallop_right(ms, *ssb.keys, ssa.keys, na, 0);
2584
57.4k
    if (k < 0)
2585
0
        return -1;
2586
57.4k
    sortslice_advance(&ssa, k);
2587
57.4k
    na -= k;
2588
57.4k
    if (na == 0)
2589
21
        return 0;
2590
2591
    /* Where does a end in b?  Elements in b after that can be
2592
     * ignored (already in place).
2593
     */
2594
57.4k
    nb = gallop_left(ms, ssa.keys[na-1], ssb.keys, nb, nb-1);
2595
57.4k
    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
57.4k
    if (na <= nb)
2602
37.9k
        return merge_lo(ms, ssa, na, ssb, nb);
2603
19.4k
    else
2604
19.4k
        return merge_hi(ms, ssa, na, ssb, nb);
2605
57.4k
}
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
57.4k
{
2615
57.4k
    int result = 0;
2616
57.4k
    assert(s1 >= 0);
2617
57.4k
    assert(n1 > 0 && n2 > 0);
2618
57.4k
    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
57.4k
    Py_ssize_t a = 2 * s1 + n1;  /* 2*a */
2629
57.4k
    Py_ssize_t b = a + n1 + n2;  /* 2*b */
2630
    /* Emulate a/n and b/n one bit a time, until bits differ. */
2631
225k
    for (;;) {
2632
225k
        ++result;
2633
225k
        if (a >= n) {  /* both quotient bits are 1 */
2634
86.7k
            assert(b >= a);
2635
86.7k
            a -= n;
2636
86.7k
            b -= n;
2637
86.7k
        }
2638
139k
        else if (b >= n) {  /* a/n bit is 0, b/n bit is 1 */
2639
57.4k
            break;
2640
57.4k
        } /* else both quotient bits are 0 */
2641
168k
        assert(a < b && b < n);
2642
168k
        a <<= 1;
2643
168k
        b <<= 1;
2644
168k
    }
2645
57.4k
    return result;
2646
57.4k
}
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
219k
{
2662
219k
    assert(ms);
2663
219k
    if (ms->n) {
2664
57.4k
        assert(ms->n > 0);
2665
57.4k
        struct s_slice *p = ms->pending;
2666
57.4k
        Py_ssize_t s1 = p[ms->n - 1].base.keys - ms->basekeys; /* start index */
2667
57.4k
        Py_ssize_t n1 = p[ms->n - 1].len;
2668
57.4k
        int power = powerloop(s1, n1, n2, ms->listlen);
2669
94.9k
        while (ms->n > 1 && p[ms->n - 2].power > power) {
2670
37.4k
            if (merge_at(ms, ms->n - 2) < 0)
2671
0
                return -1;
2672
37.4k
        }
2673
57.4k
        assert(ms->n < 2 || p[ms->n - 2].power < power);
2674
57.4k
        p[ms->n - 1].power = power;
2675
57.4k
    }
2676
219k
    return 0;
2677
219k
}
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
162k
{
2687
162k
    struct s_slice *p = ms->pending;
2688
2689
162k
    assert(ms);
2690
182k
    while (ms->n > 1) {
2691
19.9k
        Py_ssize_t n = ms->n - 2;
2692
19.9k
        if (n > 0 && p[n-1].len < p[n+1].len)
2693
262
            --n;
2694
19.9k
        if (merge_at(ms, n) < 0)
2695
0
            return -1;
2696
19.9k
    }
2697
162k
    return 0;
2698
162k
}
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
219k
{
2704
219k
    ms->mr_current += ms->listlen;
2705
219k
    assert(ms->mr_current >= 0); /* no overflow */
2706
219k
    Py_ssize_t result = ms->mr_current >> ms->mr_e;
2707
219k
    ms->mr_current &= ms->mr_mask;
2708
219k
    return result;
2709
219k
}
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
7.95M
{
2735
7.95M
    PyObject *res_obj; int res;
2736
2737
    /* No assumptions, because we check first: */
2738
7.95M
    if (Py_TYPE(v)->tp_richcompare != ms->key_richcompare)
2739
0
        return PyObject_RichCompareBool(v, w, Py_LT);
2740
2741
7.95M
    assert(ms->key_richcompare != NULL);
2742
7.95M
    res_obj = (*(ms->key_richcompare))(v, w, Py_LT);
2743
2744
7.95M
    if (res_obj == Py_NotImplemented) {
2745
0
        Py_DECREF(res_obj);
2746
0
        return PyObject_RichCompareBool(v, w, Py_LT);
2747
0
    }
2748
7.95M
    if (res_obj == NULL)
2749
0
        return -1;
2750
2751
7.95M
    if (PyBool_Check(res_obj)) {
2752
7.95M
        res = (res_obj == Py_True);
2753
7.95M
    }
2754
0
    else {
2755
0
        res = PyObject_IsTrue(res_obj);
2756
0
    }
2757
7.95M
    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
7.95M
    return res;
2765
7.95M
}
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
229k
{
2771
229k
    Py_ssize_t len;
2772
229k
    int res;
2773
2774
    /* Modified from Objects/unicodeobject.c:unicode_compare, assuming: */
2775
229k
    assert(Py_IS_TYPE(v, &PyUnicode_Type));
2776
229k
    assert(Py_IS_TYPE(w, &PyUnicode_Type));
2777
229k
    assert(PyUnicode_KIND(v) == PyUnicode_KIND(w));
2778
229k
    assert(PyUnicode_KIND(v) == PyUnicode_1BYTE_KIND);
2779
2780
229k
    len = Py_MIN(PyUnicode_GET_LENGTH(v), PyUnicode_GET_LENGTH(w));
2781
229k
    res = memcmp(PyUnicode_DATA(v), PyUnicode_DATA(w), len);
2782
2783
229k
    res = (res != 0 ?
2784
225k
           res < 0 :
2785
229k
           PyUnicode_GET_LENGTH(v) < PyUnicode_GET_LENGTH(w));
2786
2787
229k
    assert(res == PyObject_RichCompareBool(v, w, Py_LT));;
2788
229k
    return res;
2789
229k
}
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
3.86M
{
2795
3.86M
    PyLongObject *vl, *wl;
2796
3.86M
    intptr_t v0, w0;
2797
3.86M
    int res;
2798
2799
    /* Modified from Objects/longobject.c:long_compare, assuming: */
2800
3.86M
    assert(Py_IS_TYPE(v, &PyLong_Type));
2801
3.86M
    assert(Py_IS_TYPE(w, &PyLong_Type));
2802
3.86M
    assert(_PyLong_IsCompact((PyLongObject *)v));
2803
3.86M
    assert(_PyLong_IsCompact((PyLongObject *)w));
2804
2805
3.86M
    vl = (PyLongObject*)v;
2806
3.86M
    wl = (PyLongObject*)w;
2807
2808
3.86M
    v0 = _PyLong_CompactValue(vl);
2809
3.86M
    w0 = _PyLong_CompactValue(wl);
2810
2811
3.86M
    res = v0 < w0;
2812
3.86M
    assert(res == PyObject_RichCompareBool(v, w, Py_LT));
2813
3.86M
    return res;
2814
3.86M
}
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
1.26k
{
2840
1.26k
    PyTupleObject *vt, *wt;
2841
1.26k
    Py_ssize_t i, vlen, wlen;
2842
1.26k
    int k;
2843
2844
    /* Modified from Objects/tupleobject.c:tuplerichcompare, assuming: */
2845
1.26k
    assert(Py_IS_TYPE(v, &PyTuple_Type));
2846
1.26k
    assert(Py_IS_TYPE(w, &PyTuple_Type));
2847
1.26k
    assert(Py_SIZE(v) > 0);
2848
1.26k
    assert(Py_SIZE(w) > 0);
2849
2850
1.26k
    vt = (PyTupleObject *)v;
2851
1.26k
    wt = (PyTupleObject *)w;
2852
2853
1.26k
    vlen = Py_SIZE(vt);
2854
1.26k
    wlen = Py_SIZE(wt);
2855
2856
1.28k
    for (i = 0; i < vlen && i < wlen; i++) {
2857
1.28k
        k = PyObject_RichCompareBool(vt->ob_item[i], wt->ob_item[i], Py_EQ);
2858
1.28k
        if (k < 0)
2859
0
            return -1;
2860
1.28k
        if (!k)
2861
1.26k
            break;
2862
1.28k
    }
2863
2864
1.26k
    if (i >= vlen || i >= wlen)
2865
0
        return vlen < wlen;
2866
2867
1.26k
    if (i == 0)
2868
1.25k
        return ms->tuple_elem_compare(vt->ob_item[i], wt->ob_item[i], ms);
2869
12
    else
2870
12
        return PyObject_RichCompareBool(vt->ob_item[i], wt->ob_item[i], Py_LT);
2871
1.26k
}
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
@critical_section
2880
list.sort
2881
2882
    *
2883
    key as keyfunc: object = None
2884
    reverse: bool = False
2885
2886
Sort the list in ascending order and return None.
2887
2888
The sort is in-place (i.e. the list itself is modified) and stable (i.e. the
2889
order of two equal elements is maintained).
2890
2891
If a key function is given, apply it once to each list item and sort them,
2892
ascending or descending, according to their function values.
2893
2894
The reverse flag can be set to sort in descending order.
2895
[clinic start generated code]*/
2896
2897
static PyObject *
2898
list_sort_impl(PyListObject *self, PyObject *keyfunc, int reverse)
2899
/*[clinic end generated code: output=57b9f9c5e23fbe42 input=667bf25d0e3a3676]*/
2900
675k
{
2901
675k
    MergeState ms;
2902
675k
    Py_ssize_t nremaining;
2903
675k
    Py_ssize_t minrun;
2904
675k
    sortslice lo;
2905
675k
    Py_ssize_t saved_ob_size, saved_allocated;
2906
675k
    PyObject **saved_ob_item;
2907
675k
    PyObject **final_ob_item;
2908
675k
    PyObject *result = NULL;            /* guilty until proved innocent */
2909
675k
    Py_ssize_t i;
2910
675k
    PyObject **keys;
2911
2912
675k
    assert(self != NULL);
2913
675k
    assert(PyList_Check(self));
2914
675k
    if (keyfunc == Py_None)
2915
179k
        keyfunc = NULL;
2916
2917
    /* The list is temporarily made empty, so that mutations performed
2918
     * by comparison functions can't affect the slice of memory we're
2919
     * sorting (allowing mutations during sorting is a core-dump
2920
     * factory, since ob_item may change).
2921
     */
2922
675k
    saved_ob_size = Py_SIZE(self);
2923
675k
    saved_ob_item = self->ob_item;
2924
675k
    saved_allocated = self->allocated;
2925
675k
    Py_SET_SIZE(self, 0);
2926
675k
    FT_ATOMIC_STORE_PTR_RELEASE(self->ob_item, NULL);
2927
675k
    self->allocated = -1; /* any operation will reset it to >= 0 */
2928
2929
675k
    if (keyfunc == NULL) {
2930
192k
        keys = NULL;
2931
192k
        lo.keys = saved_ob_item;
2932
192k
        lo.values = NULL;
2933
192k
    }
2934
482k
    else {
2935
482k
        if (saved_ob_size < MERGESTATE_TEMP_SIZE/2)
2936
            /* Leverage stack space we allocated but won't otherwise use */
2937
479k
            keys = &ms.temparray[saved_ob_size+1];
2938
3.33k
        else {
2939
3.33k
            keys = PyMem_Malloc(sizeof(PyObject *) * saved_ob_size);
2940
3.33k
            if (keys == NULL) {
2941
0
                PyErr_NoMemory();
2942
0
                goto keyfunc_fail;
2943
0
            }
2944
3.33k
        }
2945
2946
4.26M
        for (i = 0; i < saved_ob_size ; i++) {
2947
3.78M
            keys[i] = PyObject_CallOneArg(keyfunc, saved_ob_item[i]);
2948
3.78M
            if (keys[i] == NULL) {
2949
0
                for (i=i-1 ; i>=0 ; i--)
2950
0
                    Py_DECREF(keys[i]);
2951
0
                if (saved_ob_size >= MERGESTATE_TEMP_SIZE/2)
2952
0
                    PyMem_Free(keys);
2953
0
                goto keyfunc_fail;
2954
0
            }
2955
3.78M
        }
2956
2957
482k
        lo.keys = keys;
2958
482k
        lo.values = saved_ob_item;
2959
482k
    }
2960
2961
2962
    /* The pre-sort check: here's where we decide which compare function to use.
2963
     * How much optimization is safe? We test for homogeneity with respect to
2964
     * several properties that are expensive to check at compare-time, and
2965
     * set ms appropriately. */
2966
675k
    if (saved_ob_size > 1) {
2967
        /* Assume the first element is representative of the whole list. */
2968
162k
        int keys_are_in_tuples = (Py_IS_TYPE(lo.keys[0], &PyTuple_Type) &&
2969
162k
                                  Py_SIZE(lo.keys[0]) > 0);
2970
2971
162k
        PyTypeObject* key_type = (keys_are_in_tuples ?
2972
64
                                  Py_TYPE(PyTuple_GET_ITEM(lo.keys[0], 0)) :
2973
162k
                                  Py_TYPE(lo.keys[0]));
2974
2975
162k
        int keys_are_all_same_type = 1;
2976
162k
        int strings_are_latin = 1;
2977
162k
        int ints_are_bounded = 1;
2978
2979
        /* Prove that assumption by checking every key. */
2980
4.55M
        for (i=0; i < saved_ob_size; i++) {
2981
2982
4.39M
            if (keys_are_in_tuples &&
2983
4.39M
                !(Py_IS_TYPE(lo.keys[i], &PyTuple_Type) && Py_SIZE(lo.keys[i]) != 0)) {
2984
0
                keys_are_in_tuples = 0;
2985
0
                keys_are_all_same_type = 0;
2986
0
                break;
2987
0
            }
2988
2989
            /* Note: for lists of tuples, key is the first element of the tuple
2990
             * lo.keys[i], not lo.keys[i] itself! We verify type-homogeneity
2991
             * for lists of tuples in the if-statement directly above. */
2992
4.39M
            PyObject *key = (keys_are_in_tuples ?
2993
593
                             PyTuple_GET_ITEM(lo.keys[i], 0) :
2994
4.39M
                             lo.keys[i]);
2995
2996
4.39M
            if (!Py_IS_TYPE(key, key_type)) {
2997
0
                keys_are_all_same_type = 0;
2998
                /* If keys are in tuple we must loop over the whole list to make
2999
                   sure all items are tuples */
3000
0
                if (!keys_are_in_tuples) {
3001
0
                    break;
3002
0
                }
3003
0
            }
3004
3005
4.39M
            if (keys_are_all_same_type) {
3006
4.39M
                if (key_type == &PyLong_Type &&
3007
4.39M
                    ints_are_bounded &&
3008
4.39M
                    !_PyLong_IsCompact((PyLongObject *)key)) {
3009
3010
4.77k
                    ints_are_bounded = 0;
3011
4.77k
                }
3012
4.38M
                else if (key_type == &PyUnicode_Type &&
3013
4.38M
                         strings_are_latin &&
3014
4.38M
                         PyUnicode_KIND(key) != PyUnicode_1BYTE_KIND) {
3015
3016
5.88k
                        strings_are_latin = 0;
3017
5.88k
                    }
3018
4.39M
                }
3019
4.39M
            }
3020
3021
        /* Choose the best compare, given what we now know about the keys. */
3022
162k
        if (keys_are_all_same_type) {
3023
3024
162k
            if (key_type == &PyUnicode_Type && strings_are_latin) {
3025
10.7k
                ms.key_compare = unsafe_latin_compare;
3026
10.7k
            }
3027
151k
            else if (key_type == &PyLong_Type && ints_are_bounded) {
3028
68.2k
                ms.key_compare = unsafe_long_compare;
3029
68.2k
            }
3030
83.1k
            else if (key_type == &PyFloat_Type) {
3031
0
                ms.key_compare = unsafe_float_compare;
3032
0
            }
3033
83.1k
            else if ((ms.key_richcompare = key_type->tp_richcompare) != NULL) {
3034
83.1k
                ms.key_compare = unsafe_object_compare;
3035
83.1k
            }
3036
0
            else {
3037
0
                ms.key_compare = safe_object_compare;
3038
0
            }
3039
162k
        }
3040
0
        else {
3041
0
            ms.key_compare = safe_object_compare;
3042
0
        }
3043
3044
162k
        if (keys_are_in_tuples) {
3045
            /* Make sure we're not dealing with tuples of tuples
3046
             * (remember: here, key_type refers list [key[0] for key in keys]) */
3047
64
            if (key_type == &PyTuple_Type) {
3048
0
                ms.tuple_elem_compare = safe_object_compare;
3049
0
            }
3050
64
            else {
3051
64
                ms.tuple_elem_compare = ms.key_compare;
3052
64
            }
3053
3054
64
            ms.key_compare = unsafe_tuple_compare;
3055
64
        }
3056
162k
    }
3057
    /* End of pre-sort check: ms is now set properly! */
3058
3059
675k
    merge_init(&ms, saved_ob_size, keys != NULL, &lo);
3060
3061
675k
    nremaining = saved_ob_size;
3062
675k
    if (nremaining < 2)
3063
512k
        goto succeed;
3064
3065
    /* Reverse sort stability achieved by initially reversing the list,
3066
    applying a stable forward sort, then reversing the final result. */
3067
162k
    if (reverse) {
3068
0
        if (keys != NULL)
3069
0
            reverse_slice(&keys[0], &keys[saved_ob_size]);
3070
0
        reverse_slice(&saved_ob_item[0], &saved_ob_item[saved_ob_size]);
3071
0
    }
3072
3073
    /* March over the array once, left to right, finding natural runs,
3074
     * and extending short natural runs to minrun elements.
3075
     */
3076
219k
    do {
3077
219k
        Py_ssize_t n;
3078
3079
        /* Identify next run. */
3080
219k
        n = count_run(&ms, &lo, nremaining);
3081
219k
        if (n < 0)
3082
0
            goto fail;
3083
        /* If short, extend to min(minrun, nremaining). */
3084
219k
        minrun = minrun_next(&ms);
3085
219k
        if (n < minrun) {
3086
109k
            const Py_ssize_t force = nremaining <= minrun ?
3087
71.9k
                              nremaining : minrun;
3088
109k
            if (binarysort(&ms, &lo, force, n) < 0)
3089
0
                goto fail;
3090
109k
            n = force;
3091
109k
        }
3092
        /* Maybe merge pending runs. */
3093
219k
        assert(ms.n == 0 || ms.pending[ms.n -1].base.keys +
3094
219k
                            ms.pending[ms.n-1].len == lo.keys);
3095
219k
        if (found_new_run(&ms, n) < 0)
3096
0
            goto fail;
3097
        /* Push new run on stack. */
3098
219k
        assert(ms.n < MAX_MERGE_PENDING);
3099
219k
        ms.pending[ms.n].base = lo;
3100
219k
        ms.pending[ms.n].len = n;
3101
219k
        ++ms.n;
3102
        /* Advance to find next run. */
3103
219k
        sortslice_advance(&lo, n);
3104
219k
        nremaining -= n;
3105
219k
    } while (nremaining);
3106
3107
162k
    if (merge_force_collapse(&ms) < 0)
3108
0
        goto fail;
3109
162k
    assert(ms.n == 1);
3110
162k
    assert(keys == NULL
3111
162k
           ? ms.pending[0].base.keys == saved_ob_item
3112
162k
           : ms.pending[0].base.keys == &keys[0]);
3113
162k
    assert(ms.pending[0].len == saved_ob_size);
3114
162k
    lo = ms.pending[0].base;
3115
3116
675k
succeed:
3117
675k
    result = Py_None;
3118
675k
fail:
3119
675k
    if (keys != NULL) {
3120
4.26M
        for (i = 0; i < saved_ob_size; i++)
3121
3.78M
            Py_DECREF(keys[i]);
3122
482k
        if (saved_ob_size >= MERGESTATE_TEMP_SIZE/2)
3123
3.33k
            PyMem_Free(keys);
3124
482k
    }
3125
3126
675k
    if (self->allocated != -1 && result != NULL) {
3127
        /* The user mucked with the list during the sort,
3128
         * and we don't already have another error to report.
3129
         */
3130
0
        PyErr_SetString(PyExc_ValueError, "list modified during sort");
3131
0
        result = NULL;
3132
0
    }
3133
3134
675k
    if (reverse && saved_ob_size > 1)
3135
0
        reverse_slice(saved_ob_item, saved_ob_item + saved_ob_size);
3136
3137
675k
    merge_freemem(&ms);
3138
3139
675k
keyfunc_fail:
3140
675k
    final_ob_item = self->ob_item;
3141
675k
    i = Py_SIZE(self);
3142
675k
    Py_SET_SIZE(self, saved_ob_size);
3143
675k
    FT_ATOMIC_STORE_PTR_RELEASE(self->ob_item, saved_ob_item);
3144
675k
    FT_ATOMIC_STORE_SSIZE_RELAXED(self->allocated, saved_allocated);
3145
675k
    if (final_ob_item != NULL) {
3146
        /* we cannot use list_clear() for this because it does not
3147
           guarantee that the list is really empty when it returns */
3148
0
        while (--i >= 0) {
3149
0
            Py_XDECREF(final_ob_item[i]);
3150
0
        }
3151
#ifdef Py_GIL_DISABLED
3152
        ensure_shared_on_resize(self);
3153
        bool use_qsbr = _PyObject_GC_IS_SHARED(self);
3154
#else
3155
0
        bool use_qsbr = false;
3156
0
#endif
3157
0
        free_list_items(final_ob_item, use_qsbr);
3158
0
    }
3159
675k
    return Py_XNewRef(result);
3160
675k
}
3161
#undef IFLT
3162
#undef ISLT
3163
3164
int
3165
PyList_Sort(PyObject *v)
3166
13.2k
{
3167
13.2k
    if (v == NULL || !PyList_Check(v)) {
3168
0
        PyErr_BadInternalCall();
3169
0
        return -1;
3170
0
    }
3171
13.2k
    Py_BEGIN_CRITICAL_SECTION(v);
3172
13.2k
    v = list_sort_impl((PyListObject *)v, NULL, 0);
3173
13.2k
    Py_END_CRITICAL_SECTION();
3174
13.2k
    if (v == NULL)
3175
0
        return -1;
3176
13.2k
    Py_DECREF(v);
3177
13.2k
    return 0;
3178
13.2k
}
3179
3180
/*[clinic input]
3181
@critical_section
3182
list.reverse
3183
3184
Reverse *IN PLACE*.
3185
[clinic start generated code]*/
3186
3187
static PyObject *
3188
list_reverse_impl(PyListObject *self)
3189
/*[clinic end generated code: output=482544fc451abea9 input=04ac8e0c6a66e4d9]*/
3190
0
{
3191
0
    if (Py_SIZE(self) > 1)
3192
0
        reverse_slice(self->ob_item, self->ob_item + Py_SIZE(self));
3193
0
    Py_RETURN_NONE;
3194
0
}
3195
3196
int
3197
PyList_Reverse(PyObject *v)
3198
50
{
3199
50
    PyListObject *self = (PyListObject *)v;
3200
3201
50
    if (v == NULL || !PyList_Check(v)) {
3202
0
        PyErr_BadInternalCall();
3203
0
        return -1;
3204
0
    }
3205
50
    Py_BEGIN_CRITICAL_SECTION(self);
3206
50
    if (Py_SIZE(self) > 1) {
3207
50
        reverse_slice(self->ob_item, self->ob_item + Py_SIZE(self));
3208
50
    }
3209
50
    Py_END_CRITICAL_SECTION()
3210
50
    return 0;
3211
50
}
3212
3213
PyObject *
3214
PyList_AsTuple(PyObject *v)
3215
249k
{
3216
249k
    if (v == NULL || !PyList_Check(v)) {
3217
0
        PyErr_BadInternalCall();
3218
0
        return NULL;
3219
0
    }
3220
249k
    PyObject *ret;
3221
249k
    PyListObject *self = (PyListObject *)v;
3222
249k
    Py_BEGIN_CRITICAL_SECTION(self);
3223
249k
    ret = _PyTuple_FromArray(self->ob_item, Py_SIZE(v));
3224
249k
    Py_END_CRITICAL_SECTION();
3225
249k
    return ret;
3226
249k
}
3227
3228
PyObject *
3229
_PyList_AsTupleAndClear(PyListObject *self)
3230
0
{
3231
0
    assert(self != NULL);
3232
0
    PyObject *ret;
3233
0
    if (self->ob_item == NULL) {
3234
0
        return PyTuple_New(0);
3235
0
    }
3236
0
    Py_BEGIN_CRITICAL_SECTION(self);
3237
0
    PyObject **items = self->ob_item;
3238
0
    Py_ssize_t size = Py_SIZE(self);
3239
0
    self->ob_item = NULL;
3240
0
    Py_SET_SIZE(self, 0);
3241
0
    ret = _PyTuple_FromArraySteal(items, size);
3242
0
    free_list_items(items, false);
3243
0
    Py_END_CRITICAL_SECTION();
3244
0
    return ret;
3245
0
}
3246
3247
PyObject *
3248
_PyList_FromStackRefStealOnSuccess(const _PyStackRef *src, Py_ssize_t n)
3249
157M
{
3250
157M
    if (n == 0) {
3251
141M
        return PyList_New(0);
3252
141M
    }
3253
3254
15.2M
    PyListObject *list = (PyListObject *)PyList_New(n);
3255
15.2M
    if (list == NULL) {
3256
0
        return NULL;
3257
0
    }
3258
3259
15.2M
    PyObject **dst = list->ob_item;
3260
42.6M
    for (Py_ssize_t i = 0; i < n; i++) {
3261
27.4M
        dst[i] = PyStackRef_AsPyObjectSteal(src[i]);
3262
27.4M
    }
3263
3264
15.2M
    return (PyObject *)list;
3265
15.2M
}
3266
3267
/*[clinic input]
3268
list.index
3269
3270
    value: object
3271
    start: slice_index(accept={int}) = 0
3272
    stop: slice_index(accept={int}, c_default="PY_SSIZE_T_MAX") = sys.maxsize
3273
    /
3274
3275
Return first index of value.
3276
3277
Raises ValueError if the value is not present.
3278
[clinic start generated code]*/
3279
3280
static PyObject *
3281
list_index_impl(PyListObject *self, PyObject *value, Py_ssize_t start,
3282
                Py_ssize_t stop)
3283
/*[clinic end generated code: output=ec51b88787e4e481 input=40ec5826303a0eb1]*/
3284
0
{
3285
0
    if (start < 0) {
3286
0
        start += Py_SIZE(self);
3287
0
        if (start < 0)
3288
0
            start = 0;
3289
0
    }
3290
0
    if (stop < 0) {
3291
0
        stop += Py_SIZE(self);
3292
0
        if (stop < 0)
3293
0
            stop = 0;
3294
0
    }
3295
0
    for (Py_ssize_t i = start; i < stop; i++) {
3296
0
        PyObject *obj = list_get_item_ref(self, i);
3297
0
        if (obj == NULL) {
3298
            // out-of-bounds
3299
0
            break;
3300
0
        }
3301
0
        int cmp = PyObject_RichCompareBool(obj, value, Py_EQ);
3302
0
        Py_DECREF(obj);
3303
0
        if (cmp > 0)
3304
0
            return PyLong_FromSsize_t(i);
3305
0
        else if (cmp < 0)
3306
0
            return NULL;
3307
0
    }
3308
0
    PyErr_SetString(PyExc_ValueError, "list.index(x): x not in list");
3309
0
    return NULL;
3310
0
}
3311
3312
/*[clinic input]
3313
list.count
3314
3315
     value: object
3316
     /
3317
3318
Return number of occurrences of value.
3319
[clinic start generated code]*/
3320
3321
static PyObject *
3322
list_count_impl(PyListObject *self, PyObject *value)
3323
/*[clinic end generated code: output=eff66f14aef2df86 input=3bdc3a5e6f749565]*/
3324
0
{
3325
0
    Py_ssize_t count = 0;
3326
0
    for (Py_ssize_t i = 0; ; i++) {
3327
0
        PyObject *obj = list_get_item_ref(self, i);
3328
0
        if (obj == NULL) {
3329
            // out-of-bounds
3330
0
            break;
3331
0
        }
3332
0
        if (obj == value) {
3333
0
           count++;
3334
0
           Py_DECREF(obj);
3335
0
           continue;
3336
0
        }
3337
0
        int cmp = PyObject_RichCompareBool(obj, value, Py_EQ);
3338
0
        Py_DECREF(obj);
3339
0
        if (cmp > 0)
3340
0
            count++;
3341
0
        else if (cmp < 0)
3342
0
            return NULL;
3343
0
    }
3344
0
    return PyLong_FromSsize_t(count);
3345
0
}
3346
3347
/*[clinic input]
3348
@critical_section
3349
list.remove
3350
3351
     value: object
3352
     /
3353
3354
Remove first occurrence of value.
3355
3356
Raises ValueError if the value is not present.
3357
[clinic start generated code]*/
3358
3359
static PyObject *
3360
list_remove_impl(PyListObject *self, PyObject *value)
3361
/*[clinic end generated code: output=b9b76a6633b18778 input=26c813dbb95aa93b]*/
3362
2.61k
{
3363
2.61k
    Py_ssize_t i;
3364
3365
2.62k
    for (i = 0; i < Py_SIZE(self); i++) {
3366
2.62k
        PyObject *obj = self->ob_item[i];
3367
2.62k
        Py_INCREF(obj);
3368
2.62k
        int cmp = PyObject_RichCompareBool(obj, value, Py_EQ);
3369
2.62k
        Py_DECREF(obj);
3370
2.62k
        if (cmp > 0) {
3371
2.61k
            if (list_ass_slice_lock_held(self, i, i+1, NULL) == 0)
3372
2.61k
                Py_RETURN_NONE;
3373
0
            return NULL;
3374
2.61k
        }
3375
10
        else if (cmp < 0)
3376
0
            return NULL;
3377
2.62k
    }
3378
2
    PyErr_SetString(PyExc_ValueError, "list.remove(x): x not in list");
3379
2
    return NULL;
3380
2.61k
}
3381
3382
static int
3383
list_traverse(PyObject *self, visitproc visit, void *arg)
3384
56.0M
{
3385
56.0M
    PyListObject *o = (PyListObject *)self;
3386
56.0M
    Py_ssize_t i;
3387
3388
630M
    for (i = Py_SIZE(o); --i >= 0; )
3389
574M
        Py_VISIT(o->ob_item[i]);
3390
56.0M
    return 0;
3391
56.0M
}
3392
3393
static PyObject *
3394
list_richcompare_impl(PyObject *v, PyObject *w, int op)
3395
3.84k
{
3396
3.84k
    PyListObject *vl, *wl;
3397
3.84k
    Py_ssize_t i;
3398
3399
3.84k
    if (!PyList_Check(v) || !PyList_Check(w))
3400
731
        Py_RETURN_NOTIMPLEMENTED;
3401
3402
3.11k
    vl = (PyListObject *)v;
3403
3.11k
    wl = (PyListObject *)w;
3404
3405
3.11k
    if (Py_SIZE(vl) != Py_SIZE(wl) && (op == Py_EQ || op == Py_NE)) {
3406
        /* Shortcut: if the lengths differ, the lists differ */
3407
459
        if (op == Py_EQ)
3408
459
            Py_RETURN_FALSE;
3409
0
        else
3410
0
            Py_RETURN_TRUE;
3411
459
    }
3412
3413
    /* Search for the first index where items are different */
3414
2.85k
    for (i = 0; i < Py_SIZE(vl) && i < Py_SIZE(wl); i++) {
3415
375
        PyObject *vitem = vl->ob_item[i];
3416
375
        PyObject *witem = wl->ob_item[i];
3417
375
        if (vitem == witem) {
3418
148
            continue;
3419
148
        }
3420
3421
227
        Py_INCREF(vitem);
3422
227
        Py_INCREF(witem);
3423
227
        int k = PyObject_RichCompareBool(vitem, witem, Py_EQ);
3424
227
        Py_DECREF(vitem);
3425
227
        Py_DECREF(witem);
3426
227
        if (k < 0)
3427
0
            return NULL;
3428
227
        if (!k)
3429
179
            break;
3430
227
    }
3431
3432
2.65k
    if (i >= Py_SIZE(vl) || i >= Py_SIZE(wl)) {
3433
        /* No more items to compare -- compare sizes */
3434
2.48k
        Py_RETURN_RICHCOMPARE(Py_SIZE(vl), Py_SIZE(wl), op);
3435
2.48k
    }
3436
3437
    /* We have an item that differs -- shortcuts for EQ/NE */
3438
179
    if (op == Py_EQ) {
3439
168
        Py_RETURN_FALSE;
3440
168
    }
3441
11
    if (op == Py_NE) {
3442
11
        Py_RETURN_TRUE;
3443
11
    }
3444
3445
    /* Compare the final item again using the proper operator */
3446
0
    PyObject *vitem = vl->ob_item[i];
3447
0
    PyObject *witem = wl->ob_item[i];
3448
0
    Py_INCREF(vitem);
3449
0
    Py_INCREF(witem);
3450
0
    PyObject *result = PyObject_RichCompare(vl->ob_item[i], wl->ob_item[i], op);
3451
0
    Py_DECREF(vitem);
3452
0
    Py_DECREF(witem);
3453
0
    return result;
3454
11
}
3455
3456
static PyObject *
3457
list_richcompare(PyObject *v, PyObject *w, int op)
3458
3.84k
{
3459
3.84k
    PyObject *ret;
3460
3.84k
    Py_BEGIN_CRITICAL_SECTION2(v, w);
3461
3.84k
    ret = list_richcompare_impl(v, w, op);
3462
3.84k
    Py_END_CRITICAL_SECTION2()
3463
3.84k
    return ret;
3464
3.84k
}
3465
3466
/*[clinic input]
3467
list.__init__
3468
3469
    iterable: object(c_default="NULL") = ()
3470
    /
3471
3472
Built-in mutable sequence.
3473
3474
If no argument is given, the constructor creates a new empty list.
3475
The argument must be an iterable if specified.
3476
[clinic start generated code]*/
3477
3478
static int
3479
list___init___impl(PyListObject *self, PyObject *iterable)
3480
/*[clinic end generated code: output=0f3c21379d01de48 input=b3f3fe7206af8f6b]*/
3481
19.8M
{
3482
    /* Verify list invariants established by PyType_GenericAlloc() */
3483
19.8M
    assert(0 <= Py_SIZE(self));
3484
19.8M
    assert(Py_SIZE(self) <= self->allocated || self->allocated == -1);
3485
19.8M
    assert(self->ob_item != NULL ||
3486
19.8M
           self->allocated == 0 || self->allocated == -1);
3487
3488
    /* Empty previous contents */
3489
19.8M
    if (self->ob_item != NULL) {
3490
0
        Py_BEGIN_CRITICAL_SECTION(self);
3491
0
        list_clear(self);
3492
0
        Py_END_CRITICAL_SECTION();
3493
0
    }
3494
19.8M
    if (iterable != NULL) {
3495
8.98M
        if (_list_extend(self, iterable) < 0) {
3496
0
            return -1;
3497
0
        }
3498
8.98M
    }
3499
19.8M
    return 0;
3500
19.8M
}
3501
3502
static PyObject *
3503
list_vectorcall(PyObject *type, PyObject * const*args,
3504
                size_t nargsf, PyObject *kwnames)
3505
8.98M
{
3506
8.98M
    if (!_PyArg_NoKwnames("list", kwnames)) {
3507
0
        return NULL;
3508
0
    }
3509
8.98M
    Py_ssize_t nargs = PyVectorcall_NARGS(nargsf);
3510
8.98M
    if (!_PyArg_CheckPositional("list", nargs, 0, 1)) {
3511
0
        return NULL;
3512
0
    }
3513
3514
8.98M
    PyObject *list = PyType_GenericAlloc(_PyType_CAST(type), 0);
3515
8.98M
    if (list == NULL) {
3516
0
        return NULL;
3517
0
    }
3518
8.98M
    if (nargs) {
3519
8.98M
        if (list___init___impl((PyListObject *)list, args[0])) {
3520
0
            Py_DECREF(list);
3521
0
            return NULL;
3522
0
        }
3523
8.98M
    }
3524
8.98M
    return list;
3525
8.98M
}
3526
3527
3528
/*[clinic input]
3529
list.__sizeof__
3530
3531
Return the size of the list in memory, in bytes.
3532
[clinic start generated code]*/
3533
3534
static PyObject *
3535
list___sizeof___impl(PyListObject *self)
3536
/*[clinic end generated code: output=3417541f95f9a53e input=b8030a5d5ce8a187]*/
3537
0
{
3538
0
    size_t res = _PyObject_SIZE(Py_TYPE(self));
3539
0
    Py_ssize_t allocated = FT_ATOMIC_LOAD_SSIZE_RELAXED(self->allocated);
3540
0
    res += (size_t)allocated * sizeof(void*);
3541
0
    return PyLong_FromSize_t(res);
3542
0
}
3543
3544
static PyObject *list_iter(PyObject *seq);
3545
static PyObject *list_subscript(PyObject*, PyObject*);
3546
3547
static PyMethodDef list_methods[] = {
3548
    {"__getitem__", list_subscript, METH_O|METH_COEXIST,
3549
     PyDoc_STR("__getitem__($self, index, /)\n--\n\nReturn self[index].")},
3550
    LIST___REVERSED___METHODDEF
3551
    LIST___SIZEOF___METHODDEF
3552
    PY_LIST_CLEAR_METHODDEF
3553
    LIST_COPY_METHODDEF
3554
    LIST_APPEND_METHODDEF
3555
    LIST_INSERT_METHODDEF
3556
    LIST_EXTEND_METHODDEF
3557
    LIST_POP_METHODDEF
3558
    LIST_REMOVE_METHODDEF
3559
    LIST_INDEX_METHODDEF
3560
    LIST_COUNT_METHODDEF
3561
    LIST_REVERSE_METHODDEF
3562
    LIST_SORT_METHODDEF
3563
    {"__class_getitem__", Py_GenericAlias, METH_O|METH_CLASS, PyDoc_STR("See PEP 585")},
3564
    {NULL,              NULL}           /* sentinel */
3565
};
3566
3567
static PySequenceMethods list_as_sequence = {
3568
    list_length,                                /* sq_length */
3569
    list_concat,                                /* sq_concat */
3570
    list_repeat,                                /* sq_repeat */
3571
    list_item,                                  /* sq_item */
3572
    0,                                          /* sq_slice */
3573
    list_ass_item,                              /* sq_ass_item */
3574
    0,                                          /* sq_ass_slice */
3575
    list_contains,                              /* sq_contains */
3576
    list_inplace_concat,                        /* sq_inplace_concat */
3577
    list_inplace_repeat,                        /* sq_inplace_repeat */
3578
};
3579
3580
static inline PyObject *
3581
list_slice_step_lock_held(PyListObject *a, Py_ssize_t start, Py_ssize_t step, Py_ssize_t len)
3582
44
{
3583
44
    PyListObject *np = (PyListObject *)list_new_prealloc(len);
3584
44
    if (np == NULL) {
3585
0
        return NULL;
3586
0
    }
3587
44
    size_t cur;
3588
44
    Py_ssize_t i;
3589
44
    PyObject **src = a->ob_item;
3590
44
    PyObject **dest = np->ob_item;
3591
440
    for (cur = start, i = 0; i < len;
3592
396
            cur += (size_t)step, i++) {
3593
396
        PyObject *v = src[cur];
3594
396
        dest[i] = Py_NewRef(v);
3595
396
    }
3596
44
    Py_SET_SIZE(np, len);
3597
44
    return (PyObject *)np;
3598
44
}
3599
3600
static PyObject *
3601
list_slice_wrap(PyListObject *aa, Py_ssize_t start, Py_ssize_t stop, Py_ssize_t step)
3602
1.95M
{
3603
1.95M
    PyObject *res = NULL;
3604
1.95M
    Py_BEGIN_CRITICAL_SECTION(aa);
3605
1.95M
    Py_ssize_t len = PySlice_AdjustIndices(Py_SIZE(aa), &start, &stop, step);
3606
1.95M
    if (len <= 0) {
3607
359k
        res = PyList_New(0);
3608
359k
    }
3609
1.59M
    else if (step == 1) {
3610
1.59M
        res = list_slice_lock_held(aa, start, stop);
3611
1.59M
    }
3612
44
    else {
3613
44
        res = list_slice_step_lock_held(aa, start, step, len);
3614
44
    }
3615
1.95M
    Py_END_CRITICAL_SECTION();
3616
1.95M
    return res;
3617
1.95M
}
3618
3619
static inline PyObject*
3620
list_slice_subscript(PyObject* self, PyObject* item)
3621
1.95M
{
3622
1.95M
    assert(PyList_Check(self));
3623
1.95M
    assert(PySlice_Check(item));
3624
1.95M
    Py_ssize_t start, stop, step;
3625
1.95M
    if (PySlice_Unpack(item, &start, &stop, &step) < 0) {
3626
0
        return NULL;
3627
0
    }
3628
1.95M
    return list_slice_wrap((PyListObject *)self, start, stop, step);
3629
1.95M
}
3630
3631
PyObject *
3632
_PyList_SliceSubscript(PyObject* _self, PyObject* item)
3633
1.82M
{
3634
1.82M
    return list_slice_subscript(_self, item);
3635
1.82M
}
3636
3637
static PyObject *
3638
list_subscript(PyObject* _self, PyObject* item)
3639
14.8M
{
3640
14.8M
    PyListObject* self = (PyListObject*)_self;
3641
14.8M
    if (_PyIndex_Check(item)) {
3642
14.6M
        Py_ssize_t i;
3643
14.6M
        i = PyNumber_AsSsize_t(item, PyExc_IndexError);
3644
14.6M
        if (i == -1 && PyErr_Occurred())
3645
0
            return NULL;
3646
14.6M
        if (i < 0)
3647
11.1M
            i += PyList_GET_SIZE(self);
3648
14.6M
        return list_item((PyObject *)self, i);
3649
14.6M
    }
3650
129k
    else if (PySlice_Check(item)) {
3651
129k
        return list_slice_subscript(_self, item);
3652
129k
    }
3653
0
    else {
3654
0
        PyErr_Format(PyExc_TypeError,
3655
0
                     "list indices must be integers or slices, not %.200s",
3656
0
                     Py_TYPE(item)->tp_name);
3657
0
        return NULL;
3658
0
    }
3659
14.8M
}
3660
3661
static Py_ssize_t
3662
adjust_slice_indexes(PyListObject *lst,
3663
                     Py_ssize_t *start, Py_ssize_t *stop,
3664
                     Py_ssize_t step)
3665
183k
{
3666
183k
    Py_ssize_t slicelength = PySlice_AdjustIndices(Py_SIZE(lst), start, stop,
3667
183k
                                                   step);
3668
3669
    /* Make sure s[5:2] = [..] inserts at the right place:
3670
        before 5, not before 2. */
3671
183k
    if ((step < 0 && *start < *stop) ||
3672
183k
        (step > 0 && *start > *stop))
3673
0
        *stop = *start;
3674
3675
183k
    return slicelength;
3676
183k
}
3677
3678
static int
3679
list_ass_subscript_lock_held(PyObject *_self, PyObject *item, PyObject *value)
3680
186k
{
3681
186k
    _Py_CRITICAL_SECTION_ASSERT_OBJECT_LOCKED(_self);
3682
3683
186k
    PyListObject *self = (PyListObject *)_self;
3684
186k
    if (_PyIndex_Check(item)) {
3685
3.01k
        Py_ssize_t i = PyNumber_AsSsize_t(item, PyExc_IndexError);
3686
3.01k
        if (i == -1 && PyErr_Occurred())
3687
0
            return -1;
3688
3.01k
        if (i < 0)
3689
2.88k
            i += PyList_GET_SIZE(self);
3690
3.01k
        return list_ass_item_lock_held(self, i, value);
3691
3.01k
    }
3692
183k
    else if (PySlice_Check(item)) {
3693
183k
        Py_ssize_t start, stop, step;
3694
3695
183k
        if (PySlice_Unpack(item, &start, &stop, &step) < 0) {
3696
0
            return -1;
3697
0
        }
3698
3699
183k
        if (value == NULL) {
3700
            /* delete slice */
3701
11
            PyObject **garbage;
3702
11
            size_t cur;
3703
11
            Py_ssize_t i;
3704
11
            int res;
3705
3706
11
            Py_ssize_t slicelength = adjust_slice_indexes(self, &start, &stop,
3707
11
                                                          step);
3708
3709
11
            if (step == 1)
3710
11
                return list_ass_slice_lock_held(self, start, stop, value);
3711
3712
0
            if (slicelength <= 0)
3713
0
                return 0;
3714
3715
0
            if (step < 0) {
3716
0
                stop = start + 1;
3717
0
                start = stop + step*(slicelength - 1) - 1;
3718
0
                step = -step;
3719
0
            }
3720
3721
0
            garbage = (PyObject**)
3722
0
                PyMem_Malloc(slicelength*sizeof(PyObject*));
3723
0
            if (!garbage) {
3724
0
                PyErr_NoMemory();
3725
0
                return -1;
3726
0
            }
3727
3728
            /* drawing pictures might help understand these for
3729
               loops. Basically, we memmove the parts of the
3730
               list that are *not* part of the slice: step-1
3731
               items for each item that is part of the slice,
3732
               and then tail end of the list that was not
3733
               covered by the slice */
3734
0
            for (cur = start, i = 0;
3735
0
                 cur < (size_t)stop;
3736
0
                 cur += step, i++) {
3737
0
                Py_ssize_t lim = step - 1;
3738
3739
0
                garbage[i] = PyList_GET_ITEM(self, cur);
3740
3741
0
                if (cur + step >= (size_t)Py_SIZE(self)) {
3742
0
                    lim = Py_SIZE(self) - cur - 1;
3743
0
                }
3744
3745
0
                memmove(self->ob_item + cur - i,
3746
0
                    self->ob_item + cur + 1,
3747
0
                    lim * sizeof(PyObject *));
3748
0
            }
3749
0
            cur = start + (size_t)slicelength * step;
3750
0
            if (cur < (size_t)Py_SIZE(self)) {
3751
0
                memmove(self->ob_item + cur - slicelength,
3752
0
                    self->ob_item + cur,
3753
0
                    (Py_SIZE(self) - cur) *
3754
0
                     sizeof(PyObject *));
3755
0
            }
3756
3757
0
            Py_SET_SIZE(self, Py_SIZE(self) - slicelength);
3758
0
            res = list_resize(self, Py_SIZE(self));
3759
3760
0
            for (i = 0; i < slicelength; i++) {
3761
0
                Py_DECREF(garbage[i]);
3762
0
            }
3763
0
            PyMem_Free(garbage);
3764
3765
0
            return res;
3766
0
        }
3767
183k
        else {
3768
            /* assign slice */
3769
183k
            PyObject *ins, *seq;
3770
183k
            PyObject **garbage, **seqitems, **selfitems;
3771
183k
            Py_ssize_t i;
3772
183k
            size_t cur;
3773
3774
            /* protect against a[::-1] = a */
3775
183k
            if (self == (PyListObject*)value) {
3776
0
                seq = list_slice_lock_held((PyListObject *)value, 0,
3777
0
                                            Py_SIZE(value));
3778
0
            }
3779
183k
            else {
3780
183k
                seq = PySequence_Fast(value,
3781
183k
                                      "must assign iterable "
3782
183k
                                      "to extended slice");
3783
183k
            }
3784
183k
            if (!seq)
3785
0
                return -1;
3786
3787
183k
            Py_ssize_t slicelength = adjust_slice_indexes(self, &start, &stop,
3788
183k
                                                          step);
3789
3790
183k
            if (step == 1) {
3791
183k
                int res = list_ass_slice_lock_held(self, start, stop, seq);
3792
183k
                Py_DECREF(seq);
3793
183k
                return res;
3794
183k
            }
3795
3796
0
            if (PySequence_Fast_GET_SIZE(seq) != slicelength) {
3797
0
                PyErr_Format(PyExc_ValueError,
3798
0
                    "attempt to assign sequence of "
3799
0
                    "size %zd to extended slice of "
3800
0
                    "size %zd",
3801
0
                         PySequence_Fast_GET_SIZE(seq),
3802
0
                         slicelength);
3803
0
                Py_DECREF(seq);
3804
0
                return -1;
3805
0
            }
3806
3807
0
            if (!slicelength) {
3808
0
                Py_DECREF(seq);
3809
0
                return 0;
3810
0
            }
3811
3812
0
            garbage = (PyObject**)
3813
0
                PyMem_Malloc(slicelength*sizeof(PyObject*));
3814
0
            if (!garbage) {
3815
0
                Py_DECREF(seq);
3816
0
                PyErr_NoMemory();
3817
0
                return -1;
3818
0
            }
3819
3820
0
            selfitems = self->ob_item;
3821
0
            seqitems = PySequence_Fast_ITEMS(seq);
3822
0
            for (cur = start, i = 0; i < slicelength;
3823
0
                 cur += (size_t)step, i++) {
3824
0
                garbage[i] = selfitems[cur];
3825
0
                ins = Py_NewRef(seqitems[i]);
3826
0
                selfitems[cur] = ins;
3827
0
            }
3828
3829
0
            for (i = 0; i < slicelength; i++) {
3830
0
                Py_DECREF(garbage[i]);
3831
0
            }
3832
3833
0
            PyMem_Free(garbage);
3834
0
            Py_DECREF(seq);
3835
3836
0
            return 0;
3837
0
        }
3838
183k
    }
3839
0
    else {
3840
0
        PyErr_Format(PyExc_TypeError,
3841
0
                     "list indices must be integers or slices, not %.200s",
3842
0
                     Py_TYPE(item)->tp_name);
3843
0
        return -1;
3844
0
    }
3845
186k
}
3846
3847
static int
3848
list_ass_subscript(PyObject *self, PyObject *item, PyObject *value)
3849
186k
{
3850
186k
    int res;
3851
#ifdef Py_GIL_DISABLED
3852
    if (PySlice_Check(item) && value != NULL && PyList_CheckExact(value)) {
3853
        Py_BEGIN_CRITICAL_SECTION2(self, value);
3854
        res = list_ass_subscript_lock_held(self, item, value);
3855
        Py_END_CRITICAL_SECTION2();
3856
        return res;
3857
    }
3858
#endif
3859
186k
    Py_BEGIN_CRITICAL_SECTION(self);
3860
186k
    res = list_ass_subscript_lock_held(self, item, value);
3861
186k
    Py_END_CRITICAL_SECTION();
3862
186k
    return res;
3863
186k
}
3864
3865
static PyMappingMethods list_as_mapping = {
3866
    list_length,
3867
    list_subscript,
3868
    list_ass_subscript
3869
};
3870
3871
PyTypeObject PyList_Type = {
3872
    PyVarObject_HEAD_INIT(&PyType_Type, 0)
3873
    "list",
3874
    sizeof(PyListObject),
3875
    0,
3876
    list_dealloc,                               /* tp_dealloc */
3877
    0,                                          /* tp_vectorcall_offset */
3878
    0,                                          /* tp_getattr */
3879
    0,                                          /* tp_setattr */
3880
    0,                                          /* tp_as_async */
3881
    list_repr,                                  /* tp_repr */
3882
    0,                                          /* tp_as_number */
3883
    &list_as_sequence,                          /* tp_as_sequence */
3884
    &list_as_mapping,                           /* tp_as_mapping */
3885
    PyObject_HashNotImplemented,                /* tp_hash */
3886
    0,                                          /* tp_call */
3887
    0,                                          /* tp_str */
3888
    PyObject_GenericGetAttr,                    /* tp_getattro */
3889
    0,                                          /* tp_setattro */
3890
    0,                                          /* tp_as_buffer */
3891
    Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC |
3892
        Py_TPFLAGS_BASETYPE | Py_TPFLAGS_LIST_SUBCLASS |
3893
        _Py_TPFLAGS_MATCH_SELF | Py_TPFLAGS_SEQUENCE,  /* tp_flags */
3894
    list___init____doc__,                       /* tp_doc */
3895
    list_traverse,                              /* tp_traverse */
3896
    list_clear_slot,                            /* tp_clear */
3897
    list_richcompare,                           /* tp_richcompare */
3898
    0,                                          /* tp_weaklistoffset */
3899
    list_iter,                                  /* tp_iter */
3900
    0,                                          /* tp_iternext */
3901
    list_methods,                               /* tp_methods */
3902
    0,                                          /* tp_members */
3903
    0,                                          /* tp_getset */
3904
    0,                                          /* tp_base */
3905
    0,                                          /* tp_dict */
3906
    0,                                          /* tp_descr_get */
3907
    0,                                          /* tp_descr_set */
3908
    0,                                          /* tp_dictoffset */
3909
    list___init__,                              /* tp_init */
3910
    PyType_GenericAlloc,                        /* tp_alloc */
3911
    PyType_GenericNew,                          /* tp_new */
3912
    PyObject_GC_Del,                            /* tp_free */
3913
    .tp_vectorcall = list_vectorcall,
3914
    .tp_version_tag = _Py_TYPE_VERSION_LIST,
3915
};
3916
3917
/*********************** List Iterator **************************/
3918
3919
static void listiter_dealloc(PyObject *);
3920
static int listiter_traverse(PyObject *, visitproc, void *);
3921
static PyObject *listiter_next(PyObject *);
3922
static PyObject *listiter_len(PyObject *, PyObject *);
3923
static PyObject *listiter_reduce_general(void *_it, int forward);
3924
static PyObject *listiter_reduce(PyObject *, PyObject *);
3925
static PyObject *listiter_setstate(PyObject *, PyObject *state);
3926
3927
PyDoc_STRVAR(length_hint_doc, "Private method returning an estimate of len(list(it)).");
3928
PyDoc_STRVAR(reduce_doc, "Return state information for pickling.");
3929
PyDoc_STRVAR(setstate_doc, "Set state information for unpickling.");
3930
3931
static PyMethodDef listiter_methods[] = {
3932
    {"__length_hint__", listiter_len, METH_NOARGS, length_hint_doc},
3933
    {"__reduce__", listiter_reduce, METH_NOARGS, reduce_doc},
3934
    {"__setstate__", listiter_setstate, METH_O, setstate_doc},
3935
    {NULL,              NULL}           /* sentinel */
3936
};
3937
3938
PyTypeObject PyListIter_Type = {
3939
    PyVarObject_HEAD_INIT(&PyType_Type, 0)
3940
    "list_iterator",                            /* tp_name */
3941
    sizeof(_PyListIterObject),                  /* tp_basicsize */
3942
    0,                                          /* tp_itemsize */
3943
    /* methods */
3944
    listiter_dealloc,               /* tp_dealloc */
3945
    0,                                          /* tp_vectorcall_offset */
3946
    0,                                          /* tp_getattr */
3947
    0,                                          /* tp_setattr */
3948
    0,                                          /* tp_as_async */
3949
    0,                                          /* tp_repr */
3950
    0,                                          /* tp_as_number */
3951
    0,                                          /* tp_as_sequence */
3952
    0,                                          /* tp_as_mapping */
3953
    0,                                          /* tp_hash */
3954
    0,                                          /* tp_call */
3955
    0,                                          /* tp_str */
3956
    PyObject_GenericGetAttr,                    /* tp_getattro */
3957
    0,                                          /* tp_setattro */
3958
    0,                                          /* tp_as_buffer */
3959
    Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC,/* tp_flags */
3960
    0,                                          /* tp_doc */
3961
    listiter_traverse,                          /* tp_traverse */
3962
    0,                                          /* tp_clear */
3963
    0,                                          /* tp_richcompare */
3964
    0,                                          /* tp_weaklistoffset */
3965
    PyObject_SelfIter,                          /* tp_iter */
3966
    listiter_next,                              /* tp_iternext */
3967
    listiter_methods,                           /* tp_methods */
3968
    0,                                          /* tp_members */
3969
};
3970
3971
3972
static PyObject *
3973
list_iter(PyObject *seq)
3974
28.5M
{
3975
28.5M
    if (!PyList_Check(seq)) {
3976
0
        PyErr_BadInternalCall();
3977
0
        return NULL;
3978
0
    }
3979
28.5M
    _PyListIterObject *it = _Py_FREELIST_POP(_PyListIterObject, list_iters);
3980
28.5M
    if (it == NULL) {
3981
1.67M
        it = PyObject_GC_New(_PyListIterObject, &PyListIter_Type);
3982
1.67M
        if (it == NULL) {
3983
0
            return NULL;
3984
0
        }
3985
1.67M
    }
3986
28.5M
    it->it_index = 0;
3987
28.5M
    it->it_seq = (PyListObject *)Py_NewRef(seq);
3988
28.5M
    _PyObject_GC_TRACK(it);
3989
28.5M
    return (PyObject *)it;
3990
28.5M
}
3991
3992
static void
3993
listiter_dealloc(PyObject *self)
3994
28.5M
{
3995
28.5M
    _PyListIterObject *it = (_PyListIterObject *)self;
3996
28.5M
    _PyObject_GC_UNTRACK(it);
3997
28.5M
    Py_XDECREF(it->it_seq);
3998
28.5M
    assert(Py_IS_TYPE(self, &PyListIter_Type));
3999
28.5M
    _Py_FREELIST_FREE(list_iters, it, PyObject_GC_Del);
4000
28.5M
}
4001
4002
static int
4003
listiter_traverse(PyObject *it, visitproc visit, void *arg)
4004
389k
{
4005
389k
    Py_VISIT(((_PyListIterObject *)it)->it_seq);
4006
389k
    return 0;
4007
389k
}
4008
4009
static PyObject *
4010
listiter_next(PyObject *self)
4011
131M
{
4012
131M
    _PyListIterObject *it = (_PyListIterObject *)self;
4013
131M
    Py_ssize_t index = FT_ATOMIC_LOAD_SSIZE_RELAXED(it->it_index);
4014
131M
    if (index < 0) {
4015
156
        return NULL;
4016
156
    }
4017
4018
131M
    PyObject *item = list_get_item_ref(it->it_seq, index);
4019
131M
    if (item == NULL) {
4020
        // out-of-bounds
4021
27.9M
        FT_ATOMIC_STORE_SSIZE_RELAXED(it->it_index, -1);
4022
27.9M
#ifndef Py_GIL_DISABLED
4023
27.9M
        PyListObject *seq = it->it_seq;
4024
27.9M
        it->it_seq = NULL;
4025
27.9M
        Py_DECREF(seq);
4026
27.9M
#endif
4027
27.9M
        return NULL;
4028
27.9M
    }
4029
103M
    FT_ATOMIC_STORE_SSIZE_RELAXED(it->it_index, index + 1);
4030
103M
    return item;
4031
131M
}
4032
4033
static PyObject *
4034
listiter_len(PyObject *self, PyObject *Py_UNUSED(ignored))
4035
1.44M
{
4036
1.44M
    assert(self != NULL);
4037
1.44M
    _PyListIterObject *it = (_PyListIterObject *)self;
4038
1.44M
    Py_ssize_t index = FT_ATOMIC_LOAD_SSIZE_RELAXED(it->it_index);
4039
1.44M
    if (index >= 0) {
4040
1.44M
        Py_ssize_t len = PyList_GET_SIZE(it->it_seq) - index;
4041
1.44M
        if (len >= 0)
4042
1.44M
            return PyLong_FromSsize_t(len);
4043
1.44M
    }
4044
0
    return PyLong_FromLong(0);
4045
1.44M
}
4046
4047
static PyObject *
4048
listiter_reduce(PyObject *it, PyObject *Py_UNUSED(ignored))
4049
0
{
4050
0
    return listiter_reduce_general(it, 1);
4051
0
}
4052
4053
static PyObject *
4054
listiter_setstate(PyObject *self, PyObject *state)
4055
0
{
4056
0
    _PyListIterObject *it = (_PyListIterObject *)self;
4057
0
    Py_ssize_t index = PyLong_AsSsize_t(state);
4058
0
    if (index == -1 && PyErr_Occurred())
4059
0
        return NULL;
4060
0
    if (it->it_seq != NULL) {
4061
0
        if (index < -1)
4062
0
            index = -1;
4063
0
        else if (index > PyList_GET_SIZE(it->it_seq))
4064
0
            index = PyList_GET_SIZE(it->it_seq); /* iterator exhausted */
4065
0
        FT_ATOMIC_STORE_SSIZE_RELAXED(it->it_index, index);
4066
0
    }
4067
0
    Py_RETURN_NONE;
4068
0
}
4069
4070
/*********************** List Reverse Iterator **************************/
4071
4072
typedef struct {
4073
    PyObject_HEAD
4074
    Py_ssize_t it_index;
4075
    PyListObject *it_seq; /* Set to NULL when iterator is exhausted */
4076
} listreviterobject;
4077
4078
static void listreviter_dealloc(PyObject *);
4079
static int listreviter_traverse(PyObject *, visitproc, void *);
4080
static PyObject *listreviter_next(PyObject *);
4081
static PyObject *listreviter_len(PyObject *, PyObject *);
4082
static PyObject *listreviter_reduce(PyObject *, PyObject *);
4083
static PyObject *listreviter_setstate(PyObject *, PyObject *);
4084
4085
static PyMethodDef listreviter_methods[] = {
4086
    {"__length_hint__", listreviter_len, METH_NOARGS, length_hint_doc},
4087
    {"__reduce__", listreviter_reduce, METH_NOARGS, reduce_doc},
4088
    {"__setstate__", listreviter_setstate, METH_O, setstate_doc},
4089
    {NULL,              NULL}           /* sentinel */
4090
};
4091
4092
PyTypeObject PyListRevIter_Type = {
4093
    PyVarObject_HEAD_INIT(&PyType_Type, 0)
4094
    "list_reverseiterator",                     /* tp_name */
4095
    sizeof(listreviterobject),                  /* tp_basicsize */
4096
    0,                                          /* tp_itemsize */
4097
    /* methods */
4098
    listreviter_dealloc,                        /* tp_dealloc */
4099
    0,                                          /* tp_vectorcall_offset */
4100
    0,                                          /* tp_getattr */
4101
    0,                                          /* tp_setattr */
4102
    0,                                          /* tp_as_async */
4103
    0,                                          /* tp_repr */
4104
    0,                                          /* tp_as_number */
4105
    0,                                          /* tp_as_sequence */
4106
    0,                                          /* tp_as_mapping */
4107
    0,                                          /* tp_hash */
4108
    0,                                          /* tp_call */
4109
    0,                                          /* tp_str */
4110
    PyObject_GenericGetAttr,                    /* tp_getattro */
4111
    0,                                          /* tp_setattro */
4112
    0,                                          /* tp_as_buffer */
4113
    Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC,/* tp_flags */
4114
    0,                                          /* tp_doc */
4115
    listreviter_traverse,                       /* tp_traverse */
4116
    0,                                          /* tp_clear */
4117
    0,                                          /* tp_richcompare */
4118
    0,                                          /* tp_weaklistoffset */
4119
    PyObject_SelfIter,                          /* tp_iter */
4120
    listreviter_next,                           /* tp_iternext */
4121
    listreviter_methods,                /* tp_methods */
4122
    0,
4123
};
4124
4125
/*[clinic input]
4126
list.__reversed__
4127
4128
Return a reverse iterator over the list.
4129
[clinic start generated code]*/
4130
4131
static PyObject *
4132
list___reversed___impl(PyListObject *self)
4133
/*[clinic end generated code: output=b166f073208c888c input=eadb6e17f8a6a280]*/
4134
35.5M
{
4135
35.5M
    listreviterobject *it;
4136
4137
35.5M
    it = PyObject_GC_New(listreviterobject, &PyListRevIter_Type);
4138
35.5M
    if (it == NULL)
4139
0
        return NULL;
4140
35.5M
    assert(PyList_Check(self));
4141
35.5M
    it->it_index = PyList_GET_SIZE(self) - 1;
4142
35.5M
    it->it_seq = (PyListObject*)Py_NewRef(self);
4143
35.5M
    PyObject_GC_Track(it);
4144
35.5M
    return (PyObject *)it;
4145
35.5M
}
4146
4147
static void
4148
listreviter_dealloc(PyObject *self)
4149
35.5M
{
4150
35.5M
    listreviterobject *it = (listreviterobject *)self;
4151
35.5M
    PyObject_GC_UnTrack(it);
4152
35.5M
    Py_XDECREF(it->it_seq);
4153
35.5M
    PyObject_GC_Del(it);
4154
35.5M
}
4155
4156
static int
4157
listreviter_traverse(PyObject *it, visitproc visit, void *arg)
4158
491
{
4159
491
    Py_VISIT(((listreviterobject *)it)->it_seq);
4160
491
    return 0;
4161
491
}
4162
4163
static PyObject *
4164
listreviter_next(PyObject *self)
4165
44.4M
{
4166
44.4M
    listreviterobject *it = (listreviterobject *)self;
4167
44.4M
    assert(it != NULL);
4168
44.4M
    Py_ssize_t index = FT_ATOMIC_LOAD_SSIZE_RELAXED(it->it_index);
4169
44.4M
    if (index < 0) {
4170
25.4M
        return NULL;
4171
25.4M
    }
4172
4173
19.0M
    PyListObject *seq = it->it_seq;
4174
19.0M
    assert(PyList_Check(seq));
4175
19.0M
    PyObject *item = list_get_item_ref(seq, index);
4176
19.0M
    if (item != NULL) {
4177
19.0M
        FT_ATOMIC_STORE_SSIZE_RELAXED(it->it_index, index - 1);
4178
19.0M
        return item;
4179
19.0M
    }
4180
0
    FT_ATOMIC_STORE_SSIZE_RELAXED(it->it_index, -1);
4181
0
#ifndef Py_GIL_DISABLED
4182
0
    it->it_seq = NULL;
4183
0
    Py_DECREF(seq);
4184
0
#endif
4185
0
    return NULL;
4186
19.0M
}
4187
4188
static PyObject *
4189
listreviter_len(PyObject *self, PyObject *Py_UNUSED(ignored))
4190
0
{
4191
0
    listreviterobject *it = (listreviterobject *)self;
4192
0
    Py_ssize_t index = FT_ATOMIC_LOAD_SSIZE_RELAXED(it->it_index);
4193
0
    Py_ssize_t len = index + 1;
4194
0
    if (it->it_seq == NULL || PyList_GET_SIZE(it->it_seq) < len)
4195
0
        len = 0;
4196
0
    return PyLong_FromSsize_t(len);
4197
0
}
4198
4199
static PyObject *
4200
listreviter_reduce(PyObject *it, PyObject *Py_UNUSED(ignored))
4201
0
{
4202
0
    return listiter_reduce_general(it, 0);
4203
0
}
4204
4205
static PyObject *
4206
listreviter_setstate(PyObject *self, PyObject *state)
4207
0
{
4208
0
    listreviterobject *it = (listreviterobject *)self;
4209
0
    Py_ssize_t index = PyLong_AsSsize_t(state);
4210
0
    if (index == -1 && PyErr_Occurred())
4211
0
        return NULL;
4212
0
    if (it->it_seq != NULL) {
4213
0
        if (index < -1)
4214
0
            index = -1;
4215
0
        else if (index > PyList_GET_SIZE(it->it_seq) - 1)
4216
0
            index = PyList_GET_SIZE(it->it_seq) - 1;
4217
0
        FT_ATOMIC_STORE_SSIZE_RELAXED(it->it_index, index);
4218
0
    }
4219
0
    Py_RETURN_NONE;
4220
0
}
4221
4222
/* common pickling support */
4223
4224
static PyObject *
4225
listiter_reduce_general(void *_it, int forward)
4226
0
{
4227
0
    PyObject *list;
4228
0
    PyObject *iter;
4229
4230
    /* _PyEval_GetBuiltin can invoke arbitrary code,
4231
     * call must be before access of iterator pointers.
4232
     * see issue #101765 */
4233
4234
0
    if (forward) {
4235
0
        iter = _PyEval_GetBuiltin(&_Py_ID(iter));
4236
0
        _PyListIterObject *it = (_PyListIterObject *)_it;
4237
0
        Py_ssize_t idx = FT_ATOMIC_LOAD_SSIZE_RELAXED(it->it_index);
4238
0
        if (idx >= 0) {
4239
0
            return Py_BuildValue("N(O)n", iter, it->it_seq, idx);
4240
0
        }
4241
0
    } else {
4242
0
        iter = _PyEval_GetBuiltin(&_Py_ID(reversed));
4243
0
        listreviterobject *it = (listreviterobject *)_it;
4244
0
        Py_ssize_t idx = FT_ATOMIC_LOAD_SSIZE_RELAXED(it->it_index);
4245
0
        if (idx >= 0) {
4246
0
            return Py_BuildValue("N(O)n", iter, it->it_seq, idx);
4247
0
        }
4248
0
    }
4249
    /* empty iterator, create an empty list */
4250
0
    list = PyList_New(0);
4251
0
    if (list == NULL)
4252
0
        return NULL;
4253
0
    return Py_BuildValue("N(N)", iter, list);
4254
0
}