Coverage Report

Created: 2026-04-20 06:11

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