mailing list of musl libc
 help / color / mirror / code / Atom feed
From: NRK <nrk@disroot.org>
To: musl@lists.openwall.com
Subject: [musl] [PATCH] strcasestr: increase performance via inlining
Date: Sun, 3 Jul 2022 05:09:31 +0600	[thread overview]
Message-ID: <20220702230931.rdlokxbui5tfliog@gen2.localdomain> (raw)

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


                 reply	other threads:[~2022-07-02 23:09 UTC|newest]

Thread overview: [no followups] expand[flat|nested]  mbox.gz  Atom feed

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20220702230931.rdlokxbui5tfliog@gen2.localdomain \
    --to=nrk@disroot.org \
    --cc=musl@lists.openwall.com \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
Code repositories for project(s) associated with this public 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).