From 4c5df2212baf06ff5fe2a37c11964ea3aa92e47c Mon Sep 17 00:00:00 2001 From: Kyryl Melekhin Date: Thu, 25 Nov 2021 22:52:25 +0000 Subject: [PATCH] apply micro-optimizations --- pike.c | 58 ++++++++++++++++++++++++++++------------------------------ 1 file changed, 28 insertions(+), 30 deletions(-) diff --git a/pike.c b/pike.c index 7d5567d..50a0ad4 100644 --- a/pike.c +++ b/pike.c @@ -65,9 +65,9 @@ enum { // Instructions which consume input bytes (and thus fail if none left) CHAR = 1, - ANY, CLASS, MATCH, + ANY, // Assert position WBEG, WEND, @@ -79,7 +79,6 @@ enum RSPLIT, // Other (special) instructions SAVE, - MATCHCONT, }; // Return codes for re_sizecode() and re_comp() @@ -419,7 +418,7 @@ syntax_error: int re_sizecode(const char *re, int *nsub) { rcode dummyprog; - dummyprog.unilen = 4; + dummyprog.unilen = 3; dummyprog.sub = 0; int res = _compilecode(&re, &dummyprog, /*sizecode*/1); @@ -446,7 +445,6 @@ int re_comp(rcode *prog, const char *re, int nsubs) prog->insts[prog->unilen++] = SAVE; prog->insts[prog->unilen++] = prog->sub + 1; prog->insts[prog->unilen++] = MATCH; - prog->insts[prog->unilen] = MATCHCONT; prog->len += 2; return RE_SUCCESS; @@ -467,14 +465,13 @@ if (--csub->ref == 0) { \ #define deccheck(nn) \ { decref(nsub) goto rec_check##nn; } \ +#define onclist(nn) #define onnlist(nn) \ for (j = 0; j < plistidx; j++) \ if (npc == plist[j]) \ deccheck(nn) \ plist[plistidx++] = npc; \ -#define onclist(nn) \ - #define fastrec(nn, list, listidx) \ nsub->ref++; \ spc = *npc; \ @@ -570,11 +567,13 @@ clist = nlist; \ nlist = tmp; \ clistidx = nlistidx; \ +#define deccont() { decref(nsub) continue; } + int re_pikevm(rcode *prog, const char *s, const char **subp, int nsubp) { int rsubsize = sizeof(rsub)+(sizeof(char*)*nsubp); int si, i, j, c, suboff = rsubsize, *npc, osubp = nsubp * sizeof(char*); - int clistidx = 0, nlistidx, plistidx, spc; + int clistidx = 0, nlistidx, plistidx, spc, mcont = MATCH; const char *sp = s, *_sp = s; int *insts = prog->insts; int *pcs[prog->splits], *plist[prog->splits]; @@ -592,27 +591,24 @@ int re_pikevm(rcode *prog, const char *s, const char **subp, int nsubp) for (i = 0; i < clistidx; i++) { npc = clist[i].pc; nsub = clist[i].sub; - switch(*npc++) { - case CHAR: - if (c != *npc++) - break; - case ANY: - addthread: - if (*nlist[nlistidx-1].pc == MATCH) - break; - addthread(2, nlist, nlistidx) - case CLASS: - if (!re_classmatch(npc, c)) - break; - npc += *(npc+1) * 2 + 2; - goto addthread; - case MATCH: - if (matched) { - decref(matched) - suboff = 0; + spc = *npc; + if (spc == CHAR) { + if (c != *(npc+1)) + deccont() + npc += 2; + } else if (spc == CLASS) { + if (!re_classmatch(npc+1, c)) + deccont() + npc += *(npc+2) * 2 + 3; + } else if (spc == MATCH) { + nlist[nlistidx++].pc = &mcont; + if (npc != &mcont) { + if (matched) { + decref(matched) + suboff = 0; + } + matched = nsub; } - matched = nsub; - case MATCHCONT: if (sp == _sp || !nlistidx) { for (i = 0, j = i; i < nsubp; i+=2, j++) { subp[i] = matched->sub[j]; @@ -620,11 +616,13 @@ int re_pikevm(rcode *prog, const char *s, const char **subp, int nsubp) } return 1; } - nlist[nlistidx++].pc = &insts[prog->unilen]; swaplist() goto _continue; - } - decref(nsub) + } else + npc++; + if (*nlist[nlistidx-1].pc == MATCH) + deccont() + addthread(2, nlist, nlistidx) } if (sp == _sp) break;