From 460ce868edacbce45e757e6aa01a89cddbbba87c Mon Sep 17 00:00:00 2001 From: "B. Watson" Date: Thu, 27 Nov 2025 07:43:36 -0500 Subject: Speed up match_token() by 20%. Still too slow. --- src/alf.c | 30 +++++++++++++++++++++++++----- 1 file 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 -- cgit v1.2.3