Skip to content

Commit 080dd3f

Browse files
committed
Make binarysort adaptative.
1 parent 1a2e00c commit 080dd3f

1 file changed

Lines changed: 4 additions & 3 deletions

File tree

Objects/listobject.c

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1829,6 +1829,7 @@ binarysort(MergeState *ms, const sortslice *ss, Py_ssize_t n, Py_ssize_t ok)
18291829
}
18301830
#else // binary insertion sort
18311831
Py_ssize_t L, R;
1832+
M = ok - 1; // Start with the element close to the pivot
18321833
for (; ok < n; ++ok) {
18331834
/* set L to where a[ok] belongs */
18341835
L = 0;
@@ -1841,14 +1842,14 @@ binarysort(MergeState *ms, const sortslice *ss, Py_ssize_t n, Py_ssize_t ok)
18411842
*/
18421843
assert(L < R);
18431844
do {
1844-
/* don't do silly ;-) things to prevent overflow when finding
1845-
the midpoint; L and R are very far from filling a Py_ssize_t */
1846-
M = (L + R) >> 1;
18471845
#if 1 // straightforward, but highly unpredictable branch on random data
18481846
IFLT(pivot, a[M])
18491847
R = M;
18501848
else
18511849
L = M + 1;
1850+
/* don't do silly ;-) things to prevent overflow when finding
1851+
the midpoint; L and R are very far from filling a Py_ssize_t */
1852+
M = (L + R) >> 1;
18521853
#else
18531854
/* Try to get compiler to generate conditional move instructions
18541855
instead. Works fine, but leaving it disabled for now because

0 commit comments

Comments
 (0)