Skip site navigation (1)Skip section navigation (2)
Date:      Wed, 19 Apr 2023 15:18:14 GMT
From:      Hans Petter Selasky <hselasky@FreeBSD.org>
To:        src-committers@FreeBSD.org, dev-commits-src-all@FreeBSD.org, dev-commits-src-main@FreeBSD.org
Subject:   git: ecb2ce3a51e9 - main - libc: Sorting is not needed when there are less than two elements
Message-ID:  <202304191518.33JFIEA7075821@gitrepo.freebsd.org>

next in thread | raw e-mail | index | archive | help
The branch main has been updated by hselasky:

URL: https://cgit.FreeBSD.org/src/commit/?id=ecb2ce3a51e9b09a57cd42262fc798ae089c0758

commit ecb2ce3a51e9b09a57cd42262fc798ae089c0758
Author:     Hans Petter Selasky <hselasky@FreeBSD.org>
AuthorDate: 2023-04-19 10:18:56 +0000
Commit:     Hans Petter Selasky <hselasky@FreeBSD.org>
CommitDate: 2023-04-19 15:17:33 +0000

    libc: Sorting is not needed when there are less than two elements
    
    If there are less than two elements avoid executing the first
    sorting loop. No functional change intended.
    
    Reviewed by:    kib@
    MFC after:      1 week
    Sponsored by:   NVIDIA Networking
    Differential Revision:  https://reviews.freebsd.org/D39691
---
 lib/libc/stdlib/qsort.c | 3 ++-
 1 file changed, 2 insertions(+), 1 deletion(-)

diff --git a/lib/libc/stdlib/qsort.c b/lib/libc/stdlib/qsort.c
index 425edd562420..0d65cd119ea6 100644
--- a/lib/libc/stdlib/qsort.c
+++ b/lib/libc/stdlib/qsort.c
@@ -114,7 +114,8 @@ local_qsort(void *a, size_t n, size_t es, cmp_t *cmp, void *thunk)
 	int cmp_result;
 	int swap_cnt;
 
-	if (__predict_false(n == 0))
+	/* if there are less than 2 elements, then sorting is not needed */
+	if (__predict_false(n < 2))
 		return;
 loop:
 	swap_cnt = 0;



Want to link to this message? Use this URL: <https://mail-archive.FreeBSD.org/cgi/mid.cgi?202304191518.33JFIEA7075821>