@@ -74,8 +74,9 @@ list_resize(PyListObject *self, Py_ssize_t newsize)
74
74
if (newsize - Py_SIZE (self ) > (Py_ssize_t )(new_allocated - newsize ))
75
75
new_allocated = ((size_t )newsize + 3 ) & ~(size_t )3 ;
76
76
77
- if (newsize == 0 )
78
- new_allocated = 0 ;
77
+ /* Don't overallocate for lists that start empty or are set to empty. */
78
+ if (newsize == 0 || Py_SIZE (self ) == 0 )
79
+ new_allocated = newsize ;
79
80
num_allocated_bytes = new_allocated * sizeof (PyObject * );
80
81
items = (PyObject * * )PyMem_Realloc (self -> ob_item , num_allocated_bytes );
81
82
if (items == NULL ) {
@@ -218,7 +219,6 @@ valid_index(Py_ssize_t i, Py_ssize_t limit)
218
219
{
219
220
/* The cast to size_t lets us use just a single comparison
220
221
to check whether i is in the range: 0 <= i < limit.
221
-
222
222
See: Section 14.2 "Bounds Checking" in the Agner Fog
223
223
optimization manual found at:
224
224
https://www.agner.org/optimize/optimizing_cpp.pdf
@@ -787,11 +787,9 @@ list_ass_item(PyListObject *a, Py_ssize_t i, PyObject *v)
787
787
788
788
/*[clinic input]
789
789
list.insert
790
-
791
790
index: Py_ssize_t
792
791
object: object
793
792
/
794
-
795
793
Insert object before index.
796
794
[clinic start generated code]*/
797
795
@@ -806,7 +804,6 @@ list_insert_impl(PyListObject *self, Py_ssize_t index, PyObject *object)
806
804
807
805
/*[clinic input]
808
806
list.clear
809
-
810
807
Remove all items from list.
811
808
[clinic start generated code]*/
812
809
@@ -820,7 +817,6 @@ list_clear_impl(PyListObject *self)
820
817
821
818
/*[clinic input]
822
819
list.copy
823
-
824
820
Return a shallow copy of the list.
825
821
[clinic start generated code]*/
826
822
@@ -833,10 +829,8 @@ list_copy_impl(PyListObject *self)
833
829
834
830
/*[clinic input]
835
831
list.append
836
-
837
832
object: object
838
833
/
839
-
840
834
Append object to the end of the list.
841
835
[clinic start generated code]*/
842
836
@@ -851,10 +845,8 @@ list_append(PyListObject *self, PyObject *object)
851
845
852
846
/*[clinic input]
853
847
list.extend
854
-
855
848
iterable: object
856
849
/
857
-
858
850
Extend list by appending elements from the iterable.
859
851
[clinic start generated code]*/
860
852
@@ -865,7 +857,6 @@ list_extend(PyListObject *self, PyObject *iterable)
865
857
PyObject * it ; /* iter(v) */
866
858
Py_ssize_t m ; /* size of self */
867
859
Py_ssize_t n ; /* guess for size of iterable */
868
- Py_ssize_t mn ; /* m + n */
869
860
Py_ssize_t i ;
870
861
PyObject * (* iternext )(PyObject * );
871
862
@@ -889,13 +880,7 @@ list_extend(PyListObject *self, PyObject *iterable)
889
880
/* It should not be possible to allocate a list large enough to cause
890
881
an overflow on any relevant platform */
891
882
assert (m < PY_SSIZE_T_MAX - n );
892
- if (n && self -> ob_item == NULL ) {
893
- if (list_preallocate_exact (self , n ) < 0 ) {
894
- Py_DECREF (iterable );
895
- return NULL ;
896
- }
897
- }
898
- else if (list_resize (self , m + n ) < 0 ) {
883
+ if (list_resize (self , m + n ) < 0 ) {
899
884
Py_DECREF (iterable );
900
885
return NULL ;
901
886
}
@@ -935,16 +920,13 @@ list_extend(PyListObject *self, PyObject *iterable)
935
920
*/
936
921
}
937
922
else {
938
- if (n && self -> ob_item == NULL ) {
923
+ /* Make room. */
924
+ if (self -> ob_item == NULL ) {
939
925
if (list_preallocate_exact (self , n ) < 0 )
940
926
goto error ;
941
927
}
942
- else {
943
- mn = m + n ;
944
- /* Make room. */
945
- if (list_resize (self , mn ) < 0 )
946
- goto error ;
947
- }
928
+ else if (list_resize (self , m + n ) < 0 )
929
+ goto error ;
948
930
/* Make the list sane again. */
949
931
Py_SET_SIZE (self , m );
950
932
}
@@ -1009,12 +991,9 @@ list_inplace_concat(PyListObject *self, PyObject *other)
1009
991
1010
992
/*[clinic input]
1011
993
list.pop
1012
-
1013
994
index: Py_ssize_t = -1
1014
995
/
1015
-
1016
996
Remove and return item at index (default last).
1017
-
1018
997
Raises IndexError if list is empty or index is out of range.
1019
998
[clinic start generated code]*/
1020
999
@@ -1301,19 +1280,14 @@ binarysort(MergeState *ms, sortslice lo, PyObject **hi, PyObject **start)
1301
1280
/*
1302
1281
Return the length of the run beginning at lo, in the slice [lo, hi). lo < hi
1303
1282
is required on entry. "A run" is the longest ascending sequence, with
1304
-
1305
1283
lo[0] <= lo[1] <= lo[2] <= ...
1306
-
1307
1284
or the longest descending sequence, with
1308
-
1309
1285
lo[0] > lo[1] > lo[2] > ...
1310
-
1311
1286
Boolean *descending is set to 0 in the former case, or to 1 in the latter.
1312
1287
For its intended use in a stable mergesort, the strictness of the defn of
1313
1288
"descending" is needed so that the caller can safely reverse a descending
1314
1289
sequence without violating stability (strict > ensures there are no equal
1315
1290
elements to get out of order).
1316
-
1317
1291
Returns -1 in case of error.
1318
1292
*/
1319
1293
static Py_ssize_t
@@ -1355,20 +1329,14 @@ Locate the proper position of key in a sorted vector; if the vector contains
1355
1329
an element equal to key, return the position immediately to the left of
1356
1330
the leftmost equal element. [gallop_right() does the same except returns
1357
1331
the position to the right of the rightmost equal element (if any).]
1358
-
1359
1332
"a" is a sorted vector with n elements, starting at a[0]. n must be > 0.
1360
-
1361
1333
"hint" is an index at which to begin the search, 0 <= hint < n. The closer
1362
1334
hint is to the final result, the faster this runs.
1363
-
1364
1335
The return value is the int k in 0..n such that
1365
-
1366
1336
a[k-1] < key <= a[k]
1367
-
1368
1337
pretending that *(a-1) is minus infinity and a[n] is plus infinity. IOW,
1369
1338
key belongs at index k; or, IOW, the first k elements of a should precede
1370
1339
key, and the last n-k should follow key.
1371
-
1372
1340
Returns -1 on error. See listsort.txt for info on the method.
1373
1341
*/
1374
1342
static Py_ssize_t
@@ -1449,13 +1417,9 @@ gallop_left(MergeState *ms, PyObject *key, PyObject **a, Py_ssize_t n, Py_ssize_
1449
1417
/*
1450
1418
Exactly like gallop_left(), except that if key already exists in a[0:n],
1451
1419
finds the position immediately to the right of the rightmost equal value.
1452
-
1453
1420
The return value is the int k in 0..n such that
1454
-
1455
1421
a[k-1] <= key < a[k]
1456
-
1457
1422
or -1 if error.
1458
-
1459
1423
The code duplication is massive, but this is enough different given that
1460
1424
we're sticking to "<" comparisons that it's much harder to follow if
1461
1425
written as one routine with yet another "left or right?" flag.
@@ -2286,19 +2250,14 @@ unsafe_tuple_compare(PyObject *v, PyObject *w, MergeState *ms)
2286
2250
*/
2287
2251
/*[clinic input]
2288
2252
list.sort
2289
-
2290
2253
*
2291
2254
key as keyfunc: object = None
2292
2255
reverse: bool(accept={int}) = False
2293
-
2294
2256
Sort the list in ascending order and return None.
2295
-
2296
2257
The sort is in-place (i.e. the list itself is modified) and stable (i.e. the
2297
2258
order of two equal elements is maintained).
2298
-
2299
2259
If a key function is given, apply it once to each list item and sort them,
2300
2260
ascending or descending, according to their function values.
2301
-
2302
2261
The reverse flag can be set to sort in descending order.
2303
2262
[clinic start generated code]*/
2304
2263
@@ -2584,7 +2543,6 @@ PyList_Sort(PyObject *v)
2584
2543
2585
2544
/*[clinic input]
2586
2545
list.reverse
2587
-
2588
2546
Reverse *IN PLACE*.
2589
2547
[clinic start generated code]*/
2590
2548
@@ -2623,14 +2581,11 @@ PyList_AsTuple(PyObject *v)
2623
2581
2624
2582
/*[clinic input]
2625
2583
list.index
2626
-
2627
2584
value: object
2628
2585
start: slice_index(accept={int}) = 0
2629
2586
stop: slice_index(accept={int}, c_default="PY_SSIZE_T_MAX") = sys.maxsize
2630
2587
/
2631
-
2632
2588
Return first index of value.
2633
-
2634
2589
Raises ValueError if the value is not present.
2635
2590
[clinic start generated code]*/
2636
2591
@@ -2667,10 +2622,8 @@ list_index_impl(PyListObject *self, PyObject *value, Py_ssize_t start,
2667
2622
2668
2623
/*[clinic input]
2669
2624
list.count
2670
-
2671
2625
value: object
2672
2626
/
2673
-
2674
2627
Return number of occurrences of value.
2675
2628
[clinic start generated code]*/
2676
2629
@@ -2700,12 +2653,9 @@ list_count(PyListObject *self, PyObject *value)
2700
2653
2701
2654
/*[clinic input]
2702
2655
list.remove
2703
-
2704
2656
value: object
2705
2657
/
2706
-
2707
2658
Remove first occurrence of value.
2708
-
2709
2659
Raises ValueError if the value is not present.
2710
2660
[clinic start generated code]*/
2711
2661
@@ -2801,12 +2751,9 @@ list_richcompare(PyObject *v, PyObject *w, int op)
2801
2751
2802
2752
/*[clinic input]
2803
2753
list.__init__
2804
-
2805
2754
iterable: object(c_default="NULL") = ()
2806
2755
/
2807
-
2808
2756
Built-in mutable sequence.
2809
-
2810
2757
If no argument is given, the constructor creates a new empty list.
2811
2758
The argument must be an iterable if specified.
2812
2759
[clinic start generated code]*/
@@ -2826,6 +2773,19 @@ list___init___impl(PyListObject *self, PyObject *iterable)
2826
2773
(void )_list_clear (self );
2827
2774
}
2828
2775
if (iterable != NULL ) {
2776
+ if (_PyObject_HasLen (iterable )) {
2777
+ Py_ssize_t iter_len = PyObject_Size (iterable );
2778
+ if (iter_len == -1 ) {
2779
+ if (!PyErr_ExceptionMatches (PyExc_TypeError )) {
2780
+ return -1 ;
2781
+ }
2782
+ PyErr_Clear ();
2783
+ }
2784
+ if (iter_len > 0 && self -> ob_item == NULL
2785
+ && list_preallocate_exact (self , iter_len )) {
2786
+ return -1 ;
2787
+ }
2788
+ }
2829
2789
PyObject * rv = list_extend (self , iterable );
2830
2790
if (rv == NULL )
2831
2791
return -1 ;
@@ -2862,7 +2822,6 @@ list_vectorcall(PyObject *type, PyObject * const*args,
2862
2822
2863
2823
/*[clinic input]
2864
2824
list.__sizeof__
2865
-
2866
2825
Return the size of the list in memory, in bytes.
2867
2826
[clinic start generated code]*/
2868
2827
@@ -3390,7 +3349,6 @@ PyTypeObject PyListRevIter_Type = {
3390
3349
3391
3350
/*[clinic input]
3392
3351
list.__reversed__
3393
-
3394
3352
Return a reverse iterator over the list.
3395
3353
[clinic start generated code]*/
3396
3354
0 commit comments