public inbox for libc-alpha@sourceware.org
 help / color / mirror / Atom feed
* [PATCH] stdlib: Optimize number of calls to comparison function
@ 2023-12-02 21:48 Kuan-Wei Chiu
  2023-12-04  8:20 ` Florian Weimer
  2024-02-16  7:08 ` [PATCH] stdlib: Optimize number of calls to comparison function Kuan-Wei Chiu
  0 siblings, 2 replies; 19+ messages in thread
From: Kuan-Wei Chiu @ 2023-12-02 21:48 UTC (permalink / raw)
  To: libc-alpha; +Cc: adhemerval.zanella, goldstein.w.n, Kuan-Wei Chiu

The current heapsort implementation in the siftdown function follows
the standard textbook version, necessitating two comparisons at each
level. Transitioning to the Bottom-up heapsort version allows us to
decrease the required comparisons to just one per level. On average,
this modification significantly reduces the comparison count from
2nlog2(n) - 3n + o(n) to nlog2(n) + 0.37*n + o(n).

Refs:
  BOTTOM-UP-HEAPSORT, a new variant of HEAPSORT beating, on an average,
  QUICKSORT (if n is not very small)
  Ingo Wegener
  Theoretical Computer Science, 118(1); Pages 81-98, 13 September 1993
  https://doi.org/10.1016/0304-3975(93)90364-Y

Signed-off-by: Kuan-Wei Chiu <visitorckw@gmail.com>
---
 stdlib/qsort.c | 36 ++++++++++++++++++------------------
 1 file changed, 18 insertions(+), 18 deletions(-)

diff --git a/stdlib/qsort.c b/stdlib/qsort.c
index be01fb5598..f5babef150 100644
--- a/stdlib/qsort.c
+++ b/stdlib/qsort.c
@@ -132,26 +132,26 @@ static inline void
 siftdown (void *base, size_t size, size_t k, size_t n,
 	  enum swap_type_t swap_type, __compar_d_fn_t cmp, void *arg)
 {
-  /* There can only be a heap condition violation if there are
-     children.  */
-  while (2 * k + 1 <= n)
-    {
-      /* Left child.  */
-      size_t j = 2 * k + 1;
-      /* If the right child is larger, use it.  */
-      if (j < n && cmp (base + (j * size), base + ((j + 1) * size), arg) < 0)
-	j++;
-
-      /* If k is already >= to its children, we are done.  */
-      if (j == k || cmp (base + (k * size), base + (j * size), arg) >= 0)
-	break;
+  size_t i, j;
+
+  /* Find the sift-down path all the way to the leaves. */
+  for (i = k; j = 2 * i + 1, j + 1 <= n;)
+    i = cmp (base + j * size, base + (j + 1) * size, arg) >= 0 ? j : (j + 1);
 
-      /* Heal the violation.  */
-      do_swap (base + (size * j), base + (k * size), size, swap_type);
+  /* Special case for the last leaf with no sibling. */
+  if (j == n)
+    i = j;
 
-      /* Swapping with j may have introduced a violation at j.  Fix
-	 it in the next loop iteration.  */
-      k = j;
+  /* Backtrack to the correct location. */
+  while (i != k && cmp (base + k * size, base + i * size, arg) > 0)
+    i = (i - 1) / 2;
+
+  /* Shift the element into its correct place. */
+  j = i;
+  while (i != k)
+    {
+      i = (i - 1) / 2;
+      do_swap (base + i * size, base + j * size, size, swap_type);
     }
 }
 
-- 
2.25.1


^ permalink raw reply	[flat|nested] 19+ messages in thread

end of thread, other threads:[~2024-03-27 20:42 UTC | newest]

Thread overview: 19+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2023-12-02 21:48 [PATCH] stdlib: Optimize number of calls to comparison function Kuan-Wei Chiu
2023-12-04  8:20 ` Florian Weimer
2023-12-04 18:31   ` Kuan-Wei Chiu
2023-12-05 10:44     ` Florian Weimer
2023-12-05 20:00       ` Kuan-Wei Chiu
2023-12-05 20:35     ` Zack Weinberg
2023-12-06 10:21       ` Florian Weimer
2023-12-06 12:51         ` Adhemerval Zanella Netto
2023-12-17 15:42           ` Zack Weinberg
2023-12-17 15:55             ` Florian Weimer
2023-12-17 16:47             ` Kuan-Wei Chiu
2023-12-17 18:02               ` Florian Weimer
2023-12-05  3:22   ` [PATCH v2 0/2] stdlib: Optimize number of calls to comparison function in qsort Kuan-Wei Chiu
2023-12-05  3:22     ` [PATCH v2 1/2] " Kuan-Wei Chiu
2023-12-05  3:22     ` [PATCH v2 2/2] stdlib: Adjust the factor in tst-qsort5 Kuan-Wei Chiu
2024-02-16  7:08 ` [PATCH] stdlib: Optimize number of calls to comparison function Kuan-Wei Chiu
2024-03-27 19:45   ` Adhemerval Zanella Netto
2024-03-27 19:59     ` Kuan-Wei Chiu
2024-03-27 20:42       ` Adhemerval Zanella Netto

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).