From 080dd3f94c93d0ad00608214f76b28d794046259 Mon Sep 17 00:00:00 2001 From: Alan Cristhian Date: Thu, 25 Sep 2025 19:52:07 -0300 Subject: [PATCH 1/4] Make binarysort adaptative. --- Objects/listobject.c | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) diff --git a/Objects/listobject.c b/Objects/listobject.c index 5905a6d335b311..39fb3b208c72e5 100644 --- a/Objects/listobject.c +++ b/Objects/listobject.c @@ -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; @@ -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 From 617bfb1774820d2b89597631df1a53ebd66c38f8 Mon Sep 17 00:00:00 2001 From: "blurb-it[bot]" <43283697+blurb-it[bot]@users.noreply.github.com> Date: Thu, 25 Sep 2025 23:26:16 +0000 Subject: [PATCH 2/4] =?UTF-8?q?=F0=9F=93=9C=F0=9F=A4=96=20Added=20by=20blu?= =?UTF-8?q?rb=5Fit.?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../2025-09-25-23-26-15.gh-issue-138946.uN03eR.rst | 1 + 1 file changed, 1 insertion(+) create mode 100644 Misc/NEWS.d/next/Core_and_Builtins/2025-09-25-23-26-15.gh-issue-138946.uN03eR.rst diff --git a/Misc/NEWS.d/next/Core_and_Builtins/2025-09-25-23-26-15.gh-issue-138946.uN03eR.rst b/Misc/NEWS.d/next/Core_and_Builtins/2025-09-25-23-26-15.gh-issue-138946.uN03eR.rst new file mode 100644 index 00000000000000..3fbeabe0786895 --- /dev/null +++ b/Misc/NEWS.d/next/Core_and_Builtins/2025-09-25-23-26-15.gh-issue-138946.uN03eR.rst @@ -0,0 +1 @@ +Make binarysort adaptative. Give an initial value to M before entering the outer loop, and move the M calculation to the end of the inner loop. From 974030b73a8d583524b84ac506aba87f470e75cb Mon Sep 17 00:00:00 2001 From: Alan Cristhian Date: Thu, 25 Sep 2025 21:01:37 -0300 Subject: [PATCH 3/4] Update 2025-09-25-23-26-15.gh-issue-138946.uN03eR.rst Explain the end goal of the change. --- .../2025-09-25-23-26-15.gh-issue-138946.uN03eR.rst | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Misc/NEWS.d/next/Core_and_Builtins/2025-09-25-23-26-15.gh-issue-138946.uN03eR.rst b/Misc/NEWS.d/next/Core_and_Builtins/2025-09-25-23-26-15.gh-issue-138946.uN03eR.rst index 3fbeabe0786895..7917c2a3aad85c 100644 --- a/Misc/NEWS.d/next/Core_and_Builtins/2025-09-25-23-26-15.gh-issue-138946.uN03eR.rst +++ b/Misc/NEWS.d/next/Core_and_Builtins/2025-09-25-23-26-15.gh-issue-138946.uN03eR.rst @@ -1 +1 @@ -Make binarysort adaptative. Give an initial value to M before entering the outer loop, and move the M calculation to the end of the inner loop. +Make binarysort adaptative. This improve `sorting()` performance by reducing the amount of comparisons of already sorted intervals. From 20288ad4e1ae4d5c4f4daad32c0ff08d2cbb325e Mon Sep 17 00:00:00 2001 From: Alan Cristhian Date: Thu, 25 Sep 2025 22:04:40 -0300 Subject: [PATCH 4/4] Fix Sphinx Lint error. --- .../2025-09-25-23-26-15.gh-issue-138946.uN03eR.rst | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Misc/NEWS.d/next/Core_and_Builtins/2025-09-25-23-26-15.gh-issue-138946.uN03eR.rst b/Misc/NEWS.d/next/Core_and_Builtins/2025-09-25-23-26-15.gh-issue-138946.uN03eR.rst index 7917c2a3aad85c..721a5f5f9d5b35 100644 --- a/Misc/NEWS.d/next/Core_and_Builtins/2025-09-25-23-26-15.gh-issue-138946.uN03eR.rst +++ b/Misc/NEWS.d/next/Core_and_Builtins/2025-09-25-23-26-15.gh-issue-138946.uN03eR.rst @@ -1 +1 @@ -Make binarysort adaptative. This improve `sorting()` performance by reducing the amount of comparisons of already sorted intervals. +Make binarysort adaptative. This improve sorting() performance by reducing the amount of comparisons of already sorted intervals.