make it even faster

This commit is contained in:
Kyryl Melekhin
2021-08-04 09:38:56 +00:00
parent 47ef1fd6d2
commit 68cddcbc6f
2 changed files with 27 additions and 24 deletions

46
pike.c
View File

@@ -447,16 +447,12 @@ int re_comp(rcode *prog, const char *re)
return RE_SUCCESS; return RE_SUCCESS;
} }
#define newsub(n, csub) \ #define newsub() \
s1 = freesub; \ s1 = freesub; \
if (s1) \ if (s1) \
freesub = (rsub*)s1->sub[0]; \ freesub = (rsub*)s1->sub[0]; \
else \ else \
s1 = (rsub*)&nsubs[rsubsize * subidx++]; \ s1 = (rsub*)&nsubs[rsubsize * subidx++]; \
for (j = n; j < nsubp; j++) \
s1->sub[j] = csub->sub[j]; \
csub = s1; \
csub->ref = 1; \
#define decref(csub) \ #define decref(csub) \
if (--csub->ref == 0) { \ if (--csub->ref == 0) { \
@@ -471,6 +467,11 @@ if (--csub->ref == 0) { \
const char *_sp = sp+l; \ const char *_sp = sp+l; \
rsub *sub = _sub; \ rsub *sub = _sub; \
rec##nn: \ rec##nn: \
if (*pc < ASSERT) { \
list[listidx].sub = sub; \
list[listidx++].pc = pc; \
goto rec_check##nn; \
} \
if(plist[pc - insts] == gen) { \ if(plist[pc - insts] == gen) { \
dec_check##nn: \ dec_check##nn: \
decref(sub) \ decref(sub) \
@@ -483,11 +484,6 @@ if (--csub->ref == 0) { \
continue; \ continue; \
} \ } \
plist[pc - insts] = gen; \ plist[pc - insts] = gen; \
if (*pc < ASSERT) { \
list[listidx].sub = sub; \
list[listidx++].pc = pc; \
goto rec_check##nn; \
} \
switch(*pc) { \ switch(*pc) { \
case JMP: \ case JMP: \
pc += 2 + pc[1]; \ pc += 2 + pc[1]; \
@@ -508,7 +504,11 @@ if (--csub->ref == 0) { \
case SAVE: \ case SAVE: \
if (sub->ref > 1) { \ if (sub->ref > 1) { \
sub->ref--; \ sub->ref--; \
newsub(0, sub) \ newsub() \
for (j = 0; j < nsubp; j++) \
s1->sub[j] = sub->sub[j]; \
sub = s1; \
sub->ref = 1; \
} \ } \
sub->sub[pc[1]] = _sp; \ sub->sub[pc[1]] = _sp; \
pc += 2; \ pc += 2; \
@@ -542,18 +542,14 @@ int re_pikevm(rcode *prog, const char *s, const char **subp, int nsubp)
const int *pcs[prog->splits], *npc; const int *pcs[prog->splits], *npc;
rsub *subs[prog->splits]; rsub *subs[prog->splits];
char nsubs[rsubsize*256]; char nsubs[rsubsize*256];
rsub *nsub = (rsub*)nsubs, *lsub = nsub, *matched = NULL, *s1; rsub *nsub = (rsub*)nsubs, *matched = NULL, *s1;
rsub *freesub = NULL; rsub *freesub = NULL;
rthread _clist[prog->len]; rthread _clist[prog->len];
rthread _nlist[prog->len]; rthread _nlist[prog->len];
rthread *clist = _clist, *nlist = _nlist, *tmp; rthread *clist = _clist, *nlist = _nlist, *tmp;
memset(plist, 0, prog->unilen*sizeof(plist[0])); memset(plist, 0, prog->unilen*sizeof(plist[0]));
for(i = 0; i < nsubp; i++)
for(i = 0; i < nsubp; i++) {
subp[i] = NULL; subp[i] = NULL;
nsub->sub[i] = NULL;
}
gen = 1; gen = 1;
goto jmp_start; goto jmp_start;
for(;; sp += l) { for(;; sp += l) {
@@ -589,17 +585,21 @@ int re_pikevm(rcode *prog, const char *s, const char **subp, int nsubp)
clistidx = nlistidx; clistidx = nlistidx;
nlistidx = 0; nlistidx = 0;
if (!matched) { if (!matched) {
nsub = lsub;
jmp_start: jmp_start:
newsub(1, nsub) newsub()
nsub->sub[0] = sp + l; s1->ref = 1;
addthread(1, clist, clistidx, insts, nsub) s1->sub[0] = sp + l;
addthread(1, clist, clistidx, insts, s1)
} else if (!clistidx) } else if (!clistidx)
break; break;
} }
if(matched) { if(matched) {
for(i = 0; i < nsubp; i++) for(i = 0; i < nsubp; i+=2) {
subp[i] = matched->sub[i]; if (matched->sub[i] && matched->sub[i+1]) {
subp[i] = matched->sub[i];
subp[i+1] = matched->sub[i+1];
}
}
return 1; return 1;
} }
return 0; return 0;

View File

@@ -90,6 +90,7 @@ abc\\\\>
[-+]?\\\\<(0[xX][0-9a-fA-FUL]+|[0-9.]{1,}[0-9eEfFuULl]+|[0-9]+)\\\\> [-+]?\\\\<(0[xX][0-9a-fA-FUL]+|[0-9.]{1,}[0-9eEfFuULl]+|[0-9]+)\\\\>
[-+]?\\\\<(0[xX][0-9a-fA-FUL]+|[0-9.]{1,}[0-9eEfFuULl]+|[0-9]+)\\\\> [-+]?\\\\<(0[xX][0-9a-fA-FUL]+|[0-9.]{1,}[0-9eEfFuULl]+|[0-9]+)\\\\>
[-+]?\\\\<(0[xX][0-9a-fA-FUL]+|[0-9.]{1,}[0-9eEfFuULl]+|[0-9]+)\\\\> [-+]?\\\\<(0[xX][0-9a-fA-FUL]+|[0-9.]{1,}[0-9eEfFuULl]+|[0-9]+)\\\\>
qwerty.*$
" "
input="\ input="\
abcdef abcdef
@@ -181,6 +182,7 @@ world
3245 jjfjjj 3245 jjfjjj
0x663q 0x663q
x37247 x37247
jjdfjk sjdjjsqwerty jdfjdfhhdhfdjjjfj jjjdf
" "
expect="\ expect="\
(0,3) (0,3)
@@ -272,6 +274,7 @@ expect="\
(3,7)(3,7) (3,7)(3,7)
-nomatch- -nomatch-
-nomatch- -nomatch-
(14,44)
(0,0) (0,0)
" "
@@ -293,4 +296,4 @@ echo "$regex" | tr '\n' | while read re; do
c=$((c+1)) c=$((c+1))
done done
gcc pike.c -pedantic -Wall -Wfatal-errors -std=c99 gcc pike.c -pedantic -Wall -Wfatal-errors -std=c99 $CFLAGS