mirror of https://lore.kernel.org/lkml/
 help / color / mirror / Atom feed
* [PATCH] lib/sort: Optimize number of calls to comparison function
@ 2023-12-02 16:37 Kuan-Wei Chiu
  2023-12-02 21:12 ` Kuan-Wei Chiu
  0 siblings, 1 reply; 2+ messages in thread
From: Kuan-Wei Chiu @ 2023-12-02 16:37 UTC (permalink / raw)
  To: akpm; +Cc: linux-kernel, lkml, Kuan-Wei Chiu

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


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

* Re: [PATCH] lib/sort: Optimize number of calls to comparison function
  2023-12-02 16:37 [PATCH] lib/sort: Optimize number of calls to comparison function Kuan-Wei Chiu
@ 2023-12-02 21:12 ` Kuan-Wei Chiu
  0 siblings, 0 replies; 2+ messages in thread
From: Kuan-Wei Chiu @ 2023-12-02 21:12 UTC (permalink / raw)
  To: akpm; +Cc: linux-kernel

On Sun, Dec 03, 2023 at 12:37:17AM +0800, Kuan-Wei Chiu wrote:
> 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
>

While the patch decreases the number of comparisons, it simultaneously
leads to an increase in the number of swaps. As a result, the overall
performance improvement may not be guaranteed.

Therefore, I believe it would be more prudent to drop this patch.
I apologize for any disruption caused on the mailing list.

Best regards,
Kuan-Wei Chiu

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

end of thread, other threads:[~2023-12-02 21:22 UTC | newest]

Thread overview: 2+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2023-12-02 16:37 [PATCH] lib/sort: Optimize number of calls to comparison function Kuan-Wei Chiu
2023-12-02 21:12 ` Kuan-Wei Chiu

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®