Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Make binarysort adaptative. This improve sorting() performance by reducing the amount of comparisons of already sorted intervals.
7 changes: 4 additions & 3 deletions Objects/listobject.c
Original file line number Diff line number Diff line change
Expand Up @@ -1829,6 +1829,7 @@ binarysort(MergeState *ms, const sortslice *ss, Py_ssize_t n, Py_ssize_t ok)
}
#else // binary insertion sort
Py_ssize_t L, R;
M = ok - 1; // Start with the element close to the pivot
for (; ok < n; ++ok) {
/* set L to where a[ok] belongs */
L = 0;
Expand All @@ -1841,14 +1842,14 @@ binarysort(MergeState *ms, const sortslice *ss, Py_ssize_t n, Py_ssize_t ok)
*/
assert(L < R);
do {
/* don't do silly ;-) things to prevent overflow when finding
the midpoint; L and R are very far from filling a Py_ssize_t */
M = (L + R) >> 1;
#if 1 // straightforward, but highly unpredictable branch on random data
IFLT(pivot, a[M])
R = M;
else
L = M + 1;
/* don't do silly ;-) things to prevent overflow when finding
the midpoint; L and R are very far from filling a Py_ssize_t */
M = (L + R) >> 1;
#else
/* Try to get compiler to generate conditional move instructions
instead. Works fine, but leaving it disabled for now because
Expand Down
Loading