From: Kent Overstreet <kent.overstreet@linux.dev>
To: Kuan-Wei Chiu <visitorckw@gmail.com>
Cc: bfoster@redhat.com, jserv@ccns.ncku.edu.tw,
linux-bcachefs@vger.kernel.org, linux-kernel@vger.kernel.org
Subject: Re: [PATCH v2] bcachefs: Optimize eytzinger0_sort() with bottom-up heapsort
Date: Sun, 7 Apr 2024 00:12:22 -0400 [thread overview]
Message-ID: <jx6yufij6gygb6ypsoq2yhw3eb3nobr4ytnb7phgmbpn5gmtws@23hu2rwhm4mt> (raw)
In-Reply-To: <20240407033904.719773-1-visitorckw@gmail.com>
On Sun, Apr 07, 2024 at 11:39:04AM +0800, Kuan-Wei Chiu wrote:
> This optimization reduces the average number of comparisons required
> from 2*n*log2(n) - 3*n + o(n) to n*log2(n) + 0.37*n + o(n). When n is
> sufficiently large, it results in approximately 50% fewer comparisons.
>
> Currently, eytzinger0_sort employs the textbook version of heapsort,
> where during the heapify process, each level requires two comparisons
> to determine the maximum among three elements. In contrast, the
> bottom-up heapsort, during heapify, only compares two children at each
> level until reaching a leaf node. Then, it backtracks from the leaf
> node to find the correct position. Since heapify typically continues
> until very close to the leaf node, the standard heapify requires about
> 2*log2(n) comparisons, while the bottom-up variant only needs log2(n)
> comparisons.
>
> The experimental data presented below is based on an array generated
> by get_random_u32().
>
> | N | comparisons(old) | comparisons(new) | time(old) | time(new) |
> |-------|------------------|------------------|-----------|-----------|
> | 10000 | 235381 | 136615 | 25545 us | 20366 us |
> | 20000 | 510694 | 293425 | 31336 us | 18312 us |
> | 30000 | 800384 | 457412 | 35042 us | 27386 us |
> | 40000 | 1101617 | 626831 | 48779 us | 38253 us |
> | 50000 | 1409762 | 799637 | 62238 us | 46950 us |
> | 60000 | 1721191 | 974521 | 75588 us | 58367 us |
> | 70000 | 2038536 | 1152171 | 90823 us | 68778 us |
> | 80000 | 2362958 | 1333472 | 104165 us | 78625 us |
> | 90000 | 2690900 | 1516065 | 116111 us | 89573 us |
> | 100000| 3019413 | 1699879 | 133638 us | 100998 us |
>
> 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>
Thanks - applied
prev parent reply other threads:[~2024-04-07 4:12 UTC|newest]
Thread overview: 6+ messages / expand[flat|nested] mbox.gz Atom feed top
2024-04-07 1:33 [PATCH] bcachefs: Optimize eytzinger0_sort() with bottom-up heapsort Kuan-Wei Chiu
2024-04-07 2:05 ` Kent Overstreet
2024-04-07 2:54 ` Kuan-Wei Chiu
2024-04-07 3:12 ` Kent Overstreet
2024-04-07 3:39 ` [PATCH v2] " Kuan-Wei Chiu
2024-04-07 4:12 ` Kent Overstreet [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=jx6yufij6gygb6ypsoq2yhw3eb3nobr4ytnb7phgmbpn5gmtws@23hu2rwhm4mt \
--to=kent.overstreet@linux.dev \
--cc=bfoster@redhat.com \
--cc=jserv@ccns.ncku.edu.tw \
--cc=linux-bcachefs@vger.kernel.org \
--cc=linux-kernel@vger.kernel.org \
--cc=visitorckw@gmail.com \
/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).