mirror of https://lore.kernel.org/lkml/
 help / color / mirror / Atom feed
From: John Sanpe <sanpeqf@gmail.com>
To: linux-kernel@vger.kernel.org
Cc: John Sanpe <sanpeqf@gmail.com>
Subject: [PATCH] lib/ts_kmp: Remove duplicate logic to improve performance
Date: Fri,  6 Jan 2023 04:06:03 +0800	[thread overview]
Message-ID: <20230105200603.51113-1-sanpeqf@gmail.com> (raw)

Replace the kmp_init processing sequence, first perform case
conversion and then perform compute_prefix_tbl, this can
reduce a meaningless conversions.

Signed-off-by: John Sanpe <sanpeqf@gmail.com>
---
 lib/ts_kmp.c | 11 ++++-------
 1 file changed, 4 insertions(+), 7 deletions(-)

diff --git a/lib/ts_kmp.c b/lib/ts_kmp.c
index c77a3d537f24..9f83f9ee352c 100644
--- a/lib/ts_kmp.c
+++ b/lib/ts_kmp.c
@@ -72,17 +72,14 @@ static unsigned int kmp_find(struct ts_config *conf, struct ts_state *state)
 }
 
 static inline void compute_prefix_tbl(const u8 *pattern, unsigned int len,
-				      unsigned int *prefix_tbl, int flags)
+				      unsigned int *prefix_tbl)
 {
 	unsigned int k, q;
-	const u8 icase = flags & TS_IGNORECASE;
 
 	for (k = 0, q = 1; q < len; q++) {
-		while (k > 0 && (icase ? toupper(pattern[k]) : pattern[k])
-		    != (icase ? toupper(pattern[q]) : pattern[q]))
+		while (k > 0 && pattern[k] != pattern[q])
 			k = prefix_tbl[k-1];
-		if ((icase ? toupper(pattern[k]) : pattern[k])
-		    == (icase ? toupper(pattern[q]) : pattern[q]))
+		if (pattern[k] == pattern[q])
 			k++;
 		prefix_tbl[q] = k;
 	}
@@ -104,13 +101,13 @@ static struct ts_config *kmp_init(const void *pattern, unsigned int len,
 	conf->flags = flags;
 	kmp = ts_config_priv(conf);
 	kmp->pattern_len = len;
-	compute_prefix_tbl(pattern, len, kmp->prefix_tbl, flags);
 	kmp->pattern = (u8 *) kmp->prefix_tbl + prefix_tbl_len;
 	if (flags & TS_IGNORECASE)
 		for (i = 0; i < len; i++)
 			kmp->pattern[i] = toupper(((u8 *)pattern)[i]);
 	else
 		memcpy(kmp->pattern, pattern, len);
+	compute_prefix_tbl(kmp->pattern, len, kmp->prefix_tbl);
 
 	return conf;
 }
-- 
2.38.1


                 reply	other threads:[~2023-01-05 20:06 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=20230105200603.51113-1-sanpeqf@gmail.com \
    --to=sanpeqf@gmail.com \
    --cc=linux-kernel@vger.kernel.org \
    /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.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox

all inboxes | Powered by JetHome®