unofficial mirror of libc-alpha@sourceware.org
 help / color / mirror / Atom feed
From: Kuan-Wei Chiu <visitorckw@gmail.com>
To: libc-alpha@sourceware.org
Cc: adhemerval.zanella@linaro.org, goldstein.w.n@gmail.com,
	Kuan-Wei Chiu <visitorckw@gmail.com>
Subject: [PATCH] stdlib: Optimize number of calls to comparison function
Date: Sun,  3 Dec 2023 05:48:39 +0800	[thread overview]
Message-ID: <20231202214839.2634493-1-visitorckw@gmail.com> (raw)

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


             reply	other threads:[~2023-12-02 21:49 UTC|newest]

Thread overview: 19+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2023-12-02 21:48 Kuan-Wei Chiu [this message]
2023-12-04  8:20 ` [PATCH] stdlib: Optimize number of calls to comparison function 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

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

  List information: https://www.gnu.org/software/libc/involved.html

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20231202214839.2634493-1-visitorckw@gmail.com \
    --to=visitorckw@gmail.com \
    --cc=adhemerval.zanella@linaro.org \
    --cc=goldstein.w.n@gmail.com \
    --cc=libc-alpha@sourceware.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
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).