lists.openwall.net   lists  /  announce  owl-users  owl-dev  john-users  john-dev  passwdqc-users  yescrypt  popa3d-users  /  oss-security  kernel-hardening  musl  sabotage  tlsify  passwords  /  crypt-dev  xvendor  /  Bugtraq  Full-Disclosure  linux-kernel  linux-netdev  linux-ext4  linux-hardening  linux-cve-announce  PHC 
Open Source and information security mailing list archives
 
Hash Suite: Windows password security audit tool. GUI, reports in PDF.
[<prev] [next>] [thread-next>] [day] [month] [year] [list]
Message-ID: <BYAPR06MB557347406F22FBA1E400A5BFD8319@BYAPR06MB5573.namprd06.prod.outlook.com>
Date:   Tue, 25 Oct 2022 01:46:32 +0000
From:   Nathan Moinvaziri <nathan@...hanm.com>
To:     "linux-kernel@...r.kernel.org" <linux-kernel@...r.kernel.org>,
        Andy Shevchenko <andy@...nel.org>
Subject: [PATCH] lib/string.c: Improve strcasecmp speed by not lowering if
 chars match

>From fcb0159ee74908f92adc34143657d8ca56e9a811 Mon Sep 17 00:00:00 2001
From: Nathan Moinvaziri <nathan@...hanm.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@...oo.de>
Reviewed-by: Steve Tucker <steven.r.tucker@...il.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

Powered by blists - more mailing lists

Powered by Openwall GNU/*/Linux Powered by OpenVZ