apply micro-optimizations

This commit is contained in:
Kyryl Melekhin
2021-11-25 22:52:25 +00:00
parent e715d8cceb
commit 4c5df2212b

50
pike.c
View File

@@ -65,9 +65,9 @@ enum
{ {
// Instructions which consume input bytes (and thus fail if none left) // Instructions which consume input bytes (and thus fail if none left)
CHAR = 1, CHAR = 1,
ANY,
CLASS, CLASS,
MATCH, MATCH,
ANY,
// Assert position // Assert position
WBEG, WBEG,
WEND, WEND,
@@ -79,7 +79,6 @@ enum
RSPLIT, RSPLIT,
// Other (special) instructions // Other (special) instructions
SAVE, SAVE,
MATCHCONT,
}; };
// Return codes for re_sizecode() and re_comp() // Return codes for re_sizecode() and re_comp()
@@ -419,7 +418,7 @@ syntax_error:
int re_sizecode(const char *re, int *nsub) int re_sizecode(const char *re, int *nsub)
{ {
rcode dummyprog; rcode dummyprog;
dummyprog.unilen = 4; dummyprog.unilen = 3;
dummyprog.sub = 0; dummyprog.sub = 0;
int res = _compilecode(&re, &dummyprog, /*sizecode*/1); 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++] = SAVE;
prog->insts[prog->unilen++] = prog->sub + 1; prog->insts[prog->unilen++] = prog->sub + 1;
prog->insts[prog->unilen++] = MATCH; prog->insts[prog->unilen++] = MATCH;
prog->insts[prog->unilen] = MATCHCONT;
prog->len += 2; prog->len += 2;
return RE_SUCCESS; return RE_SUCCESS;
@@ -467,14 +465,13 @@ if (--csub->ref == 0) { \
#define deccheck(nn) \ #define deccheck(nn) \
{ decref(nsub) goto rec_check##nn; } \ { decref(nsub) goto rec_check##nn; } \
#define onclist(nn)
#define onnlist(nn) \ #define onnlist(nn) \
for (j = 0; j < plistidx; j++) \ for (j = 0; j < plistidx; j++) \
if (npc == plist[j]) \ if (npc == plist[j]) \
deccheck(nn) \ deccheck(nn) \
plist[plistidx++] = npc; \ plist[plistidx++] = npc; \
#define onclist(nn) \
#define fastrec(nn, list, listidx) \ #define fastrec(nn, list, listidx) \
nsub->ref++; \ nsub->ref++; \
spc = *npc; \ spc = *npc; \
@@ -570,11 +567,13 @@ clist = nlist; \
nlist = tmp; \ nlist = tmp; \
clistidx = nlistidx; \ clistidx = nlistidx; \
#define deccont() { decref(nsub) continue; }
int re_pikevm(rcode *prog, const char *s, const char **subp, int nsubp) int re_pikevm(rcode *prog, const char *s, const char **subp, int nsubp)
{ {
int rsubsize = sizeof(rsub)+(sizeof(char*)*nsubp); int rsubsize = sizeof(rsub)+(sizeof(char*)*nsubp);
int si, i, j, c, suboff = rsubsize, *npc, osubp = nsubp * sizeof(char*); 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; const char *sp = s, *_sp = s;
int *insts = prog->insts; int *insts = prog->insts;
int *pcs[prog->splits], *plist[prog->splits]; 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++) { for (i = 0; i < clistidx; i++) {
npc = clist[i].pc; npc = clist[i].pc;
nsub = clist[i].sub; nsub = clist[i].sub;
switch(*npc++) { spc = *npc;
case CHAR: if (spc == CHAR) {
if (c != *npc++) if (c != *(npc+1))
break; deccont()
case ANY: npc += 2;
addthread: } else if (spc == CLASS) {
if (*nlist[nlistidx-1].pc == MATCH) if (!re_classmatch(npc+1, c))
break; deccont()
addthread(2, nlist, nlistidx) npc += *(npc+2) * 2 + 3;
case CLASS: } else if (spc == MATCH) {
if (!re_classmatch(npc, c)) nlist[nlistidx++].pc = &mcont;
break; if (npc != &mcont) {
npc += *(npc+1) * 2 + 2;
goto addthread;
case MATCH:
if (matched) { if (matched) {
decref(matched) decref(matched)
suboff = 0; suboff = 0;
} }
matched = nsub; matched = nsub;
case MATCHCONT: }
if (sp == _sp || !nlistidx) { if (sp == _sp || !nlistidx) {
for (i = 0, j = i; i < nsubp; i+=2, j++) { for (i = 0, j = i; i < nsubp; i+=2, j++) {
subp[i] = matched->sub[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; return 1;
} }
nlist[nlistidx++].pc = &insts[prog->unilen];
swaplist() swaplist()
goto _continue; goto _continue;
} } else
decref(nsub) npc++;
if (*nlist[nlistidx-1].pc == MATCH)
deccont()
addthread(2, nlist, nlistidx)
} }
if (sp == _sp) if (sp == _sp)
break; break;