aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorB. Watson <urchlay@slackware.uk>2025-11-27 07:43:36 -0500
committerB. Watson <urchlay@slackware.uk>2025-11-27 07:43:36 -0500
commit460ce868edacbce45e757e6aa01a89cddbbba87c (patch)
tree4bd6ac0472da56a326cfb6d8a7cb9d1fae2989cc
parent8556c491c9580ea7a2c558687d5f216574432a02 (diff)
downloadunalf-460ce868edacbce45e757e6aa01a89cddbbba87c.tar.gz
Speed up match_token() by 20%. Still too slow.
-rw-r--r--src/alf.c30
1 files changed, 25 insertions, 5 deletions
diff --git a/src/alf.c b/src/alf.c
index 61e3499..3492720 100644
--- a/src/alf.c
+++ b/src/alf.c
@@ -199,21 +199,41 @@ void store_token(int tok) {
search backwards, the tokens are stored with longer ones later
in the list. */
int match_token(int pos) {
- int i, maxlen;
+ int i, len, maxlen;
+ token_t *t;
+ u8 *p, *q;
maxlen = input_len - pos;
for(i = curr_token - 1; i >= INIT_TOKEN; i--) {
+ t = &tokentab[i];
+
/* don't search past the end of the input */
- if(tokentab[i].length > maxlen) continue;
+ if(t->length > maxlen) continue;
/* if the first char doesn't match, don't bother with memcmp.
this is a 5x speedup (!) */
- if(input_buf[pos] != *(tokentab[i].start)) continue;
+ if(input_buf[pos] != *(t->start)) continue;
- /* memcmp is where alf spends most of its time. */
- if(memcmp(&input_buf[pos], tokentab[i].start, tokentab[i].length) == 0)
+ /* this is where alf spends most of its time.
+ using memcmp is noticeably slower than the code below. */
+ /*
+ if(memcmp(&input_buf[pos], t->start, t->length) == 0)
return i;
+ */
+
+ /* inline memcmp replacement of sorts. I don't think it's really
+ faster than memcmp(), it only seems that way because there's
+ no function call overhead. ~20% speedup. */
+ len = t->length;
+ p = &input_buf[pos];
+ q = t->start;
+ while(len) {
+ if(*p != *q) break;
+ p++; q++;
+ len--;
+ }
+ if(!len) return i;
}
/* hard-coded single character tokens map to their values, no need