From: Adhemerval Zanella Netto <adhemerval.zanella@linaro.org>
To: Kuan-Wei Chiu <visitorckw@gmail.com>
Cc: libc-alpha@sourceware.org, goldstein.w.n@gmail.com,
fweimer@redhat.com, zack@owlfolio.org, jserv@ccns.ncku.edu.tw
Subject: Re: [PATCH] stdlib: Optimize number of calls to comparison function
Date: Wed, 27 Mar 2024 17:42:21 -0300 [thread overview]
Message-ID: <4962acdc-18ab-4045-97da-67f6b2ad19b0@linaro.org> (raw)
In-Reply-To: <ZgR6tpGIpPlI/cNM@visitorckw-System-Product-Name>
On 27/03/24 16:59, Kuan-Wei Chiu wrote:
> On Wed, Mar 27, 2024 at 04:45:35PM -0300, Adhemerval Zanella Netto wrote:
>>
>>
>> On 16/02/24 04:08, Kuan-Wei Chiu wrote:
>>> On Sun, Dec 03, 2023 at 05:48:39AM +0800, Kuan-Wei Chiu wrote:
>>>> 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
>>>>
>>>
>>> Since we still retain heapsort as a fallback for mergesort, should we
>>> reconsider applying this patch?
>>>
>>> Thanks,
>>> Kuan-Wei
>>
>> Yes, I think it is work and it looks great to me.
>
> Should I resend a new patch, or can you directly apply this one?
It applies as-is, so it should be ok. I will run some tests and apply, thanks.
prev parent reply other threads:[~2024-03-27 20:42 UTC|newest]
Thread overview: 19+ messages / expand[flat|nested] mbox.gz Atom feed top
2023-12-02 21:48 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 message]
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
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=4962acdc-18ab-4045-97da-67f6b2ad19b0@linaro.org \
--to=adhemerval.zanella@linaro.org \
--cc=fweimer@redhat.com \
--cc=goldstein.w.n@gmail.com \
--cc=jserv@ccns.ncku.edu.tw \
--cc=libc-alpha@sourceware.org \
--cc=visitorckw@gmail.com \
--cc=zack@owlfolio.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).