mailing list of musl libc
 help / color / mirror / code / Atom feed
* [musl] [PATCH] strcasestr: increase performance via inlining
@ 2022-07-02 23:09 NRK
  0 siblings, 0 replies; only message in thread
From: NRK @ 2022-07-02 23:09 UTC (permalink / raw)
  To: musl

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


^ permalink raw reply	[flat|nested] only message in thread

only message in thread, other threads:[~2022-07-02 23:09 UTC | newest]

Thread overview: (only message) (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2022-07-02 23:09 [musl] [PATCH] strcasestr: increase performance via inlining NRK

Code repositories for project(s) associated with this inbox:

	https://git.vuxu.org/mirror/musl/

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).