better memory management of threads (thanks knuth)

This commit is contained in:
ben 2016-04-27 17:36:43 -05:00
parent 86e0099835
commit 3bf6ef0196
5 changed files with 64 additions and 51 deletions

View file

@ -39,7 +39,6 @@ struct Reprog
{ {
Reinst *startinst; Reinst *startinst;
Rethread *threads; Rethread *threads;
Rethread **thrpool;
char *regstr; char *regstr;
int len; int len;
int nthr; int nthr;

View file

@ -20,9 +20,3 @@ UPDATE=\
${LIB:/$objtype/%=/386/%}\ ${LIB:/$objtype/%=/386/%}\
</sys/src/cmd/mksyslib </sys/src/cmd/mksyslib
$O.regextest: tests/regextest.$O $LIB
$LD -o $target regextest.$O
$O.sysregextest: tests/sysregextest.$O
$LD -o $target sysregextest.$O

View file

@ -190,13 +190,11 @@ regcomp1(char *regstr, int nl, int lit)
// prtree(parsetr, 0, 1); // prtree(parsetr, 0, 1);
reprog = malloc(sizeof(Reprog) + reprog = malloc(sizeof(Reprog) +
sizeof(Reinst) * plex.instrs + sizeof(Reinst) * plex.instrs +
sizeof(Rethread) * maxthr + sizeof(Rethread) * maxthr);
sizeof(Rethread*) * maxthr);
reprog->len = plex.instrs; reprog->len = plex.instrs;
reprog->nthr = maxthr; reprog->nthr = maxthr;
reprog->startinst = compile(parsetr, reprog, nl); reprog->startinst = compile(parsetr, reprog, nl);
reprog->threads = (Rethread*)(reprog->startinst + reprog->len); reprog->threads = (Rethread*)(reprog->startinst + reprog->len);
reprog->thrpool = (Rethread**)(reprog->threads + reprog->nthr);
reprog->regstr = regstr; reprog->regstr = regstr;
free(plex.nodes); free(plex.nodes);

View file

@ -14,7 +14,7 @@ int
regexec(Reprog *prog, char *str, Resub *sem, int msize) regexec(Reprog *prog, char *str, Resub *sem, int msize)
{ {
RethreadQ lists[2], *clist, *nlist, *tmp; RethreadQ lists[2], *clist, *nlist, *tmp;
Rethread *t, *nextthr, **availthr; Rethread *t, *next, *pooltop, *avail;
Reinst *curinst; Reinst *curinst;
Rune r; Rune r;
char *sp, *ep, endc; char *sp, *ep, endc;
@ -35,9 +35,8 @@ regexec(Reprog *prog, char *str, Resub *sem, int msize)
nlist->head = nil; nlist->head = nil;
nlist->tail = &nlist->head; nlist->tail = &nlist->head;
for(i = 0; i < prog->nthr; i++) pooltop = prog->threads + prog->nthr;
prog->thrpool[i] = prog->threads + i; avail = nil;
availthr = prog->thrpool + prog->nthr;
pri = matchpri = gen = match = 0; pri = matchpri = gen = match = 0;
sp = str; sp = str;
@ -71,14 +70,14 @@ Again:
goto Done; goto Done;
case OANY: /* fallthrough */ case OANY: /* fallthrough */
Any: Any:
nextthr = t->next; next = t->next;
t->pc = curinst + 1; t->pc = curinst + 1;
t->next = nil; t->next = nil;
*nlist->tail = t; *nlist->tail = t;
nlist->tail = &t->next; nlist->tail = &t->next;
if(nextthr == nil) if(next == nil)
break; break;
t = nextthr; t = next;
curinst = t->pc; curinst = t->pc;
goto Again; goto Again;
case OCLASS: case OCLASS:
@ -89,14 +88,14 @@ Again:
curinst++; curinst++;
goto Class; goto Class;
} }
nextthr = t->next; next = t->next;
t->pc = curinst->a; t->pc = curinst->a;
t->next = nil; t->next = nil;
*nlist->tail = t; *nlist->tail = t;
nlist->tail = &t->next; nlist->tail = &t->next;
if(nextthr == nil) if(next == nil)
break; break;
t = nextthr; t = next;
curinst = t->pc; curinst = t->pc;
goto Again; goto Again;
case ONOTNL: case ONOTNL:
@ -123,13 +122,18 @@ Again:
curinst = curinst->a; curinst = curinst->a;
goto Again; goto Again;
case OSPLIT: case OSPLIT:
nextthr = *--availthr; if(avail == nil)
nextthr->pc = curinst->b; next = --pooltop;
else {
next = avail;
avail = avail->next;
}
next->pc = curinst->b;
if(msize > 0) if(msize > 0)
memcpy(nextthr->sem, t->sem, sizeof(Resub)*msize); memcpy(next->sem, t->sem, sizeof(Resub)*msize);
nextthr->pri = t->pri; next->pri = t->pri;
nextthr->next = t->next; next->next = t->next;
t->next = nextthr; t->next = next;
curinst = curinst->a; curinst = curinst->a;
goto Again; goto Again;
case OSAVE: case OSAVE:
@ -155,10 +159,12 @@ Again:
curinst++; curinst++;
goto Again; goto Again;
Done: Done:
*availthr++ = t; next = t->next;
t = t->next; t->next = avail;
if(t == nil) avail = t;
if(next == nil)
break; break;
t = next;
curinst = t->pc; curinst = t->pc;
goto Again; goto Again;
} }
@ -166,7 +172,12 @@ Start:
/* Start again once if we haven't found anything. */ /* Start again once if we haven't found anything. */
if(first == 1 && match == 0) { if(first == 1 && match == 0) {
first = 0; first = 0;
t = *--availthr; if(avail == nil)
t = --pooltop;
else {
t = avail;
avail = avail->next;
}
if(msize > 0) if(msize > 0)
memset(t->sem, 0, sizeof(Resub)*msize); memset(t->sem, 0, sizeof(Resub)*msize);
/* "Lower" priority thread */ /* "Lower" priority thread */

View file

@ -14,10 +14,10 @@ int
rregexec(Reprog *prog, Rune *str, Resub *sem, int msize) rregexec(Reprog *prog, Rune *str, Resub *sem, int msize)
{ {
RethreadQ lists[2], *clist, *nlist, *tmp; RethreadQ lists[2], *clist, *nlist, *tmp;
Rethread *t, *nextthr, **availthr; Rethread *t, *next, *pooltop, *avail;
Reinst *curinst; Reinst *curinst;
Rune *rsp, *rep, endr, last; Rune *rsp, *rep, endr, last;
int i, match, first, gen, pri, matchpri; int match, first, gen, pri, matchpri;
if(msize > NSUBEXPM) if(msize > NSUBEXPM)
msize = NSUBEXPM; msize = NSUBEXPM;
@ -34,9 +34,8 @@ rregexec(Reprog *prog, Rune *str, Resub *sem, int msize)
nlist->head = nil; nlist->head = nil;
nlist->tail = &nlist->head; nlist->tail = &nlist->head;
for(i = 0; i < prog->nthr; i++) pooltop = prog->threads + prog->nthr;
prog->thrpool[i] = prog->threads + i; avail = nil;
availthr = prog->thrpool + prog->nthr;
pri = matchpri = gen = match = 0; pri = matchpri = gen = match = 0;
rsp = str; rsp = str;
@ -70,14 +69,14 @@ Again:
goto Done; goto Done;
case OANY: /* fallthrough */ case OANY: /* fallthrough */
Any: Any:
nextthr = t->next; next = t->next;
t->pc = curinst + 1; t->pc = curinst + 1;
t->next = nil; t->next = nil;
*nlist->tail = t; *nlist->tail = t;
nlist->tail = &t->next; nlist->tail = &t->next;
if(nextthr == nil) if(next == nil)
break; break;
t = nextthr; t = next;
curinst = t->pc; curinst = t->pc;
goto Again; goto Again;
case OCLASS: case OCLASS:
@ -88,14 +87,14 @@ Again:
curinst++; curinst++;
goto Class; goto Class;
} }
nextthr = t->next; next = t->next;
t->pc = curinst->a; t->pc = curinst->a;
t->next = nil; t->next = nil;
*nlist->tail = t; *nlist->tail = t;
nlist->tail = &t->next; nlist->tail = &t->next;
if(nextthr == nil) if(next == nil)
break; break;
t = nextthr; t = next;
curinst = t->pc; curinst = t->pc;
goto Again; goto Again;
case ONOTNL: case ONOTNL:
@ -122,13 +121,18 @@ Again:
curinst = curinst->a; curinst = curinst->a;
goto Again; goto Again;
case OSPLIT: case OSPLIT:
nextthr = *--availthr; if(avail == nil)
nextthr->pc = curinst->b; next = --pooltop;
else {
next = avail;
avail = avail->next;
}
next->pc = curinst->b;
if(msize > 0) if(msize > 0)
memcpy(nextthr->sem, t->sem, sizeof(Resub)*msize); memcpy(next->sem, t->sem, sizeof(Resub)*msize);
nextthr->pri = t->pri; next->pri = t->pri;
nextthr->next = t->next; next->next = t->next;
t->next = nextthr; t->next = next;
curinst = curinst->a; curinst = curinst->a;
goto Again; goto Again;
case OSAVE: case OSAVE:
@ -154,10 +158,12 @@ Again:
curinst++; curinst++;
goto Again; goto Again;
Done: Done:
*availthr++ = t; next = t->next;
t = t->next; t->next = avail;
if(t == nil) avail = t;
if(next == nil)
break; break;
t = next;
curinst = t->pc; curinst = t->pc;
goto Again; goto Again;
} }
@ -165,7 +171,12 @@ Start:
/* Start again once if we haven't found anything. */ /* Start again once if we haven't found anything. */
if(first == 1 && match == 0) { if(first == 1 && match == 0) {
first = 0; first = 0;
t = *--availthr; if(avail == nil)
t = --pooltop;
else {
t = avail;
avail = avail->next;
}
if(msize > 0) if(msize > 0)
memset(t->sem, 0, sizeof(Resub)*msize); memset(t->sem, 0, sizeof(Resub)*msize);
/* "Lower" priority thread */ /* "Lower" priority thread */