All of lore.kernel.org
 help / color / mirror / Atom feed
* [PATCH] lib/string.c: Improve strcasecmp speed by not lowering if chars match
@ 2022-10-25  1:46 Nathan Moinvaziri
  2022-10-25  8:00 ` Andy Shevchenko
  0 siblings, 1 reply; 11+ messages in thread
From: Nathan Moinvaziri @ 2022-10-25  1:46 UTC (permalink / raw)
  To: linux-kernel, Andy Shevchenko

From fcb0159ee74908f92adc34143657d8ca56e9a811 Mon Sep 17 00:00:00 2001
From: Nathan Moinvaziri <nathan@nathanm.com>
Date: Mon, 24 Oct 2022 16:37:59 -0700
Subject: [PATCH] lib/string.c: Improve strcasecmp speed by not lowering if
 chars match.

With strings where many characters match exactly each character is needlessly
converted to lowercase before comparing. This patch improves the comparison
by only converting to lowercase after checking that the characters don't match.

The more characters that match exactly the better performance we expect versus
the old function.

When running tests using Quick Benchmark with two matching 256 character
strings these changes result in anywhere between ~6-9x speed improvement.

* We use unsigned char instead of int similar to strncasecmp.
* We only subtract c1 - c2 when they are not equal.

Reviewed-by: Sergey Markelov <sergio_nsk@yahoo.de>
Reviewed-by: Steve Tucker <steven.r.tucker@gmail.com>
---
 lib/string.c | 17 ++++++++++++-----
 1 file changed, 12 insertions(+), 5 deletions(-)

diff --git a/lib/string.c b/lib/string.c
index 3371d26a0e39..51ad56db1b5d 100644
--- a/lib/string.c
+++ b/lib/string.c
@@ -64,13 +64,20 @@ EXPORT_SYMBOL(strncasecmp);
 #ifndef __HAVE_ARCH_STRCASECMP
 int strcasecmp(const char *s1, const char *s2)
 {
-	int c1, c2;
+	/* Yes, Virginia, it had better be unsigned */
+	unsigned char c1, c2;
 
 	do {
-		c1 = tolower(*s1++);
-		c2 = tolower(*s2++);
-	} while (c1 == c2 && c1 != 0);
-	return c1 - c2;
+		c1 = *s1++;
+		c2 = *s2++;
+		if (c1 != c2) {
+			c1 = tolower(c1);
+			c2 = tolower(c2);
+			if (c1 != c2)
+				return (int)c1 - (int)c2;
+		}
+	} while (c1 != 0);
+	return 0;
 }
 EXPORT_SYMBOL(strcasecmp);
 #endif
-- 
2.37.2.windows.2


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

end of thread, other threads:[~2022-10-27  6:32 UTC | newest]

Thread overview: 11+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2022-10-25  1:46 [PATCH] lib/string.c: Improve strcasecmp speed by not lowering if chars match Nathan Moinvaziri
2022-10-25  8:00 ` Andy Shevchenko
2022-10-25  9:03   ` Andy Shevchenko
2022-10-25 17:53     ` Nathan Moinvaziri
2022-10-25 19:19       ` Andy Shevchenko
2022-10-27  3:29         ` Nathan Moinvaziri
2022-10-27  6:31           ` Andy Shevchenko
2022-10-25 19:32       ` Christophe JAILLET
2022-10-25 23:27         ` Nathan Moinvaziri
2022-10-25 19:55   ` Rasmus Villemoes
2022-10-25 22:37     ` Nathan Moinvaziri

This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.