Follow @Openwall on Twitter for new release announcements and other news
[<prev] [next>] [day] [month] [year] [list]
Message-ID: <20220702230931.rdlokxbui5tfliog@gen2.localdomain>
Date: Sun, 3 Jul 2022 05:09:31 +0600
From: NRK <nrk@...root.org>
To: musl@...ts.openwall.com
Subject: [PATCH] strcasestr: increase performance via inlining

this is a follow up to: https://www.openwall.com/lists/musl/2022/06/16/4

simply inlining the comparison has measureable performance improvement,
~10x on my system.

the algorithmic complexity is still the same as before as it's the same
exact brute force algorithm. so hopefully this is not seen as too much
code/maintainance burden while still improving the performance a tad
bit.
---
 src/string/strcasestr.c | 9 ++++++---
 1 file changed, 6 insertions(+), 3 deletions(-)

diff --git a/src/string/strcasestr.c b/src/string/strcasestr.c
index af109f36..b5e21f70 100644
--- a/src/string/strcasestr.c
+++ b/src/string/strcasestr.c
@@ -1,9 +1,12 @@
-#define _GNU_SOURCE
+#include <ctype.h>
 #include <string.h>
 
 char *strcasestr(const char *h, const char *n)
 {
-	size_t l = strlen(n);
-	for (; *h; h++) if (!strncasecmp(h, n, l)) return (char *)h;
+	size_t i, l = strlen(n);
+	for (; *h; h++) {
+		for (i = 0; i < l && tolower((unsigned char)n[i]) == tolower((unsigned char)h[i]); ++i);
+		if (i == l) return (char *)h;
+	}
 	return 0;
 }
-- 
2.35.1

Powered by blists - more mailing lists

Confused about mailing lists and their use? Read about mailing lists on Wikipedia and check out these guidelines on proper formatting of your messages.