From 688a62c8263d00a2d650f5d2dfd124dd2e22be96 Mon Sep 17 00:00:00 2001 From: Kyryl Melekhin Date: Sat, 23 Apr 2022 17:03:30 +0000 Subject: [PATCH] speed it up, test greediness --- pike.c | 61 +++++++++++++++++++++++++++------------------------------ test.sh | 39 ++++++++++++++++++++++++++++++++++++ 2 files changed, 68 insertions(+), 32 deletions(-) diff --git a/pike.c b/pike.c index bf0e32e..17e32d9 100644 --- a/pike.c +++ b/pike.c @@ -446,8 +446,7 @@ if (si) { \ #define deccheck(nn) { decref(nsub) rec_check(nn) continue; } \ -#define onclist(nn) -#define onnlist(nn) \ +#define onlist(nn) \ if (sdense[spc] < sparsesz) \ if (sdense[sdense[spc] * 2] == (unsigned int)spc) \ deccheck(nn) \ @@ -467,27 +466,21 @@ subs[si++] = nsub; \ goto next##nn; \ #define saveclist() \ -newsub(memcpy(s1->sub, nsub->sub, osubp);, \ -memcpy(s1->sub, nsub->sub, osubp / 2);) \ - -#define savenlist() \ -newsub(/*nop*/, /*nop*/) \ -memcpy(s1->sub, nsub->sub, osubp); \ - -#define instclist(nn) \ -else if (spc == BOL) { \ - if (_sp != s) { \ - if (!si && !clistidx) \ - return 0; \ - deccheck(nn) \ - } \ - npc++; goto rec##nn; \ +if (npc[1] > nsubp / 2 && nsub->ref > 1) { \ + nsub->ref--; \ + newsub(memcpy(s1->sub, nsub->sub, osubp);, \ + memcpy(s1->sub, nsub->sub, osubp / 2);) \ + nsub = s1; \ + nsub->ref = 1; \ } \ -#define instnlist(nn) \ -else if (spc == JMP) { \ - npc += 2 + npc[1]; \ - goto rec##nn; \ +#define savenlist() \ +if (nsub->ref > 1) { \ + nsub->ref--; \ + newsub(/*nop*/, /*nop*/) \ + memcpy(s1->sub, nsub->sub, osubp); \ + nsub = s1; \ + nsub->ref = 1; \ } \ #define clistmatch() @@ -513,17 +506,12 @@ if ((unsigned int)spc < WBEG) { \ } \ next##nn: \ if (spc > JMP) { \ - on##list(nn) \ + onlist(nn) \ npc += 2; \ pcs[si] = npc + npc[-1]; \ fastrec(nn, list, listidx) \ } else if (spc == SAVE) { \ - if (nsub->ref > 1) { \ - nsub->ref--; \ - save##list() \ - nsub = s1; \ - nsub->ref = 1; \ - } \ + save##list() \ nsub->sub[npc[1]] = _sp; \ npc += 2; \ goto rec##nn; \ @@ -534,7 +522,7 @@ if (spc > JMP) { \ npc++; goto rec##nn; \ } else if (spc < 0) { \ spc = -spc; \ - on##list(nn) \ + onlist(nn) \ npc += 2; \ pcs[si] = npc; \ npc += npc[-1]; \ @@ -547,8 +535,17 @@ if (spc > JMP) { \ if (*_sp) \ deccheck(nn) \ npc++; goto rec##nn; \ -} inst##list(nn) \ -deccheck(nn) \ +} else if (spc == JMP) { \ + npc += 2 + npc[1]; \ + goto rec##nn; \ +} else { \ + if (_sp != s) { \ + if (!si && !clistidx) \ + return 0; \ + deccheck(nn) \ + } \ + npc++; goto rec##nn; \ +} \ #define swaplist() \ tmp = clist; \ @@ -567,7 +564,7 @@ int re_pikevm(rcode *prog, const char *s, const char **subp, int nsubp) int *insts = prog->insts; int *pcs[prog->splits]; rsub *subs[prog->splits]; - unsigned int sdense[prog->sparsesz], sparsesz; + unsigned int sdense[prog->sparsesz], sparsesz = 0; rsub *nsub, *s1, *matched = NULL, *freesub = NULL; rthread _clist[prog->len], _nlist[prog->len]; rthread *clist = _clist, *nlist = _nlist, *tmp; diff --git a/test.sh b/test.sh index d3246ed..b4ff3d0 100755 --- a/test.sh +++ b/test.sh @@ -172,6 +172,19 @@ aaaaa(aa)aa(aa(a)a)?aa ([0-9])+.(.*) (([0-9])+)(.)(.*) (abc|sjd|qwq(hs|qw|oo)|(ty|xx|pp)we) +(a?)? +(a?)? +(a?)?? +(a?)(a?)(a?a?)aaa(a?)|(b?) +(a?)(a?)(a?a?)aaa(a?)|(b?) +(a*)?? +(a*)|(b*)|(c*) +(a?|a?)|(a*)? +(a?|a?)a|(a*)? +(a?|a?)d|(a*)? +((a*b*c*)|(a*c*b*))+? +((a*b*c*)|(a*c*b*))*? +((a*b*c*)|(a*c*b*))+ " input="\ abcdef @@ -345,6 +358,19 @@ h:98: :3234utt;strokeliin:miter;stroke-mirlimit:10;stroke-dasharray:none;strok 650-253-000123434-45551221 650-253-000123434-455512213224hsaqer ppwe +m +a +aa +aa +aaaaaaaaaaaaaa +a +bbbbbbbbb +aaa +aaa +aaa +bbb +bbb +bbb " expect="\ (0,3) @@ -518,6 +544,19 @@ expect="\ (0,26)(2,3)(4,26) (0,36)(0,3)(2,3)(3,4)(4,36) (0,4)(0,4)(?,?)(0,2) +(0,0)(0,0) +(0,1)(0,1) +(0,0)(?,?) +(0,0)(?,?)(?,?)(?,?)(?,?)(0,0) +(0,8)(0,1)(1,2)(2,4)(7,8)(?,?) +(0,0)(?,?) +(0,0)(0,0)(?,?)(?,?) +(0,1)(0,1)(?,?) +(0,2)(0,1)(?,?) +(0,3)(?,?)(0,3) +(0,3)(0,3)(0,3)(?,?) +(0,0)(?,?)(?,?)(?,?) +(0,3)(0,3)(0,3)(?,?) (0,0) "