From: Kuan-Wei Chiu <visitorckw@gmail.com>
To: akpm@linux-foundation.org
Cc: linux-kernel@vger.kernel.org, lkml@sdf.org,
Kuan-Wei Chiu <visitorckw@gmail.com>
Subject: [PATCH] lib/sort: Optimize number of calls to comparison function
Date: Sun, 3 Dec 2023 00:37:17 +0800 [thread overview]
Message-ID: <20231202163717.687578-1-visitorckw@gmail.com> (raw)
The current implementation continues the loop when the comparison
function returns a non-negative value, which includes the case when it
returns 0. However, in scenarios where the comparison function returns
0, further comparisons are unnecessary. By making this adjustment, we
can potentially reduce the number of comparisons by approximately 50%
in extreme cases where all elements in the array are equal, and the
array size is sufficiently large.
Signed-off-by: Kuan-Wei Chiu <visitorckw@gmail.com>
---
lib/sort.c | 2 +-
1 file changed, 1 insertion(+), 1 deletion(-)
diff --git a/lib/sort.c b/lib/sort.c
index b399bf10d675..1e98a62bb2f3 100644
--- a/lib/sort.c
+++ b/lib/sort.c
@@ -267,7 +267,7 @@ void sort_r(void *base, size_t num, size_t size,
b = c;
/* Now backtrack from "b" to the correct location for "a" */
- while (b != a && do_cmp(base + a, base + b, cmp_func, priv) >= 0)
+ while (b != a && do_cmp(base + a, base + b, cmp_func, priv) > 0)
b = parent(b, lsbit, size);
c = b; /* Where "a" belongs */
while (b != a) { /* Shift it into place */
--
2.25.1
next reply other threads:[~2023-12-02 16:37 UTC|newest]
Thread overview: 2+ messages / expand[flat|nested] mbox.gz Atom feed top
2023-12-02 16:37 Kuan-Wei Chiu [this message]
2023-12-02 21:12 ` Kuan-Wei Chiu
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=20231202163717.687578-1-visitorckw@gmail.com \
--to=visitorckw@gmail.com \
--cc=akpm@linux-foundation.org \
--cc=linux-kernel@vger.kernel.org \
--cc=lkml@sdf.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
all inboxes | Powered by JetHome®