lower nsubs memory complexity
Now it shouldn't have the whole regex worst case, optimizes basic search cases with 1 alt, now giving a sane nsubs size default that should be very hard to ever surpass.
This commit is contained in:
16
pike.c
16
pike.c
@@ -537,18 +537,12 @@ int re_pikevm(rcode *prog, const char *s, const char **subp, int nsubp)
|
|||||||
{
|
{
|
||||||
int i, j, c, gen, subidx = 1, *npc;
|
int i, j, c, gen, subidx = 1, *npc;
|
||||||
int rsubsize = sizeof(rsub)+(sizeof(char*)*nsubp);
|
int rsubsize = sizeof(rsub)+(sizeof(char*)*nsubp);
|
||||||
int nsubssize = rsubsize * (prog->len+3 - prog->splits);
|
int clistidx = 0, nlistidx = 0, pclistidx;
|
||||||
int clistidx = 0, nlistidx = 0;
|
|
||||||
const char *sp = s, *_sp = s;
|
const char *sp = s, *_sp = s;
|
||||||
int *insts = prog->insts;
|
int *insts = prog->insts;
|
||||||
int *pcs[prog->splits];
|
int *pcs[prog->splits];
|
||||||
rsub *subs[prog->splits];
|
rsub *subs[prog->splits];
|
||||||
/* Although worst case scenario nsubs size is prog->len,
|
char nsubs[rsubsize * 512];
|
||||||
with moderate sized regexes it is easy to stack overflow
|
|
||||||
here. Most of the time only very small portion of memory
|
|
||||||
is actually used, but it is necessary to cover all cases
|
|
||||||
and posible paths, as it is nondeterministic. */
|
|
||||||
char nsubs[nsubssize > 500000 ? 500000 : nsubssize];
|
|
||||||
rsub *nsub, *s1, *matched = NULL, *freesub = NULL;
|
rsub *nsub, *s1, *matched = NULL, *freesub = NULL;
|
||||||
rthread _clist[prog->len], _nlist[prog->len];
|
rthread _clist[prog->len], _nlist[prog->len];
|
||||||
rthread *clist = _clist, *nlist = _nlist, *tmp;
|
rthread *clist = _clist, *nlist = _nlist, *tmp;
|
||||||
@@ -590,8 +584,11 @@ int re_pikevm(rcode *prog, const char *s, const char **subp, int nsubp)
|
|||||||
nlist = tmp;
|
nlist = tmp;
|
||||||
clistidx = nlistidx;
|
clistidx = nlistidx;
|
||||||
nlistidx = 0;
|
nlistidx = 0;
|
||||||
if (!matched) {
|
if (clistidx != 1 && !matched) {
|
||||||
|
if (!clistidx && pclistidx)
|
||||||
|
_sp = sp;
|
||||||
jmp_start:
|
jmp_start:
|
||||||
|
pclistidx = nlistidx;
|
||||||
newsub(for (i = 1; i < nsubp; i++) s1->sub[i] = NULL;, /*nop*/)
|
newsub(for (i = 1; i < nsubp; i++) s1->sub[i] = NULL;, /*nop*/)
|
||||||
s1->ref = 1;
|
s1->ref = 1;
|
||||||
s1->sub[0] = _sp;
|
s1->sub[0] = _sp;
|
||||||
@@ -599,6 +596,7 @@ int re_pikevm(rcode *prog, const char *s, const char **subp, int nsubp)
|
|||||||
addthread(1, clist, clistidx)
|
addthread(1, clist, clistidx)
|
||||||
} else if (!clistidx)
|
} else if (!clistidx)
|
||||||
break;
|
break;
|
||||||
|
pclistidx = clistidx;
|
||||||
}
|
}
|
||||||
if (matched) {
|
if (matched) {
|
||||||
for (i = 0, j = i; i < nsubp; i+=2, j++) {
|
for (i = 0, j = i; i < nsubp; i+=2, j++) {
|
||||||
|
|||||||
Reference in New Issue
Block a user