* [PATCH] lib/ts_kmp: Remove duplicate logic to improve performance
@ 2023-01-05 20:06 John Sanpe
0 siblings, 0 replies; only message in thread
From: John Sanpe @ 2023-01-05 20:06 UTC (permalink / raw)
To: linux-kernel; +Cc: John Sanpe
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
^ permalink raw reply [flat|nested] only message in thread
only message in thread, other threads:[~2023-01-05 20:06 UTC | newest]
Thread overview: (only message) (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2023-01-05 20:06 [PATCH] lib/ts_kmp: Remove duplicate logic to improve performance John Sanpe
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®