small optimizations

This commit is contained in:
Roberto Ierusalimschy 2001-06-15 16:17:33 -03:00
parent ae19b2f51e
commit eadf2aaaff
2 changed files with 97 additions and 89 deletions

114
lgc.c
View File

@ -1,5 +1,5 @@
/* /*
** $Id: lgc.c,v 1.103 2001/06/12 18:43:13 roberto Exp roberto $ ** $Id: lgc.c,v 1.104 2001/06/13 18:51:20 roberto Exp roberto $
** Garbage Collector ** Garbage Collector
** See Copyright Notice in lua.h ** See Copyright Notice in lua.h
*/ */
@ -62,7 +62,7 @@ static void markclosure (GCState *st, Closure *cl) {
static void marktable (GCState *st, Hash *h) { static void marktable (GCState *st, Hash *h) {
if (!ismarked(h)) { if (!ismarked(h)) {
h->mark = st->tmark; /* chain it in list of marked */ h->mark = st->tmark; /* chain it for later traversal */
st->tmark = h; st->tmark = h;
} }
} }
@ -127,8 +127,9 @@ static void traverseclosure (GCState *st, Closure *f) {
static void removekey (Node *n) { static void removekey (Node *n) {
lua_assert(ttype(val(n)) == LUA_TNIL);
if (ttype_key(n) != LUA_TNIL && ttype_key(n) != LUA_TNUMBER) if (ttype_key(n) != LUA_TNIL && ttype_key(n) != LUA_TNUMBER)
n->key_value.ts = NULL; /* dead key; remove it */ ttype_key(n) = LUA_TNONE; /* dead key; remove it */
} }
@ -180,30 +181,29 @@ static void markall (lua_State *L) {
} }
static int hasmark (const TObject *o) { static int hasmark (int tt, Value *v) {
switch (ttype(o)) { switch (tt) {
case LUA_TSTRING: case LUA_TSTRING:
return tsvalue(o)->marked; return v->ts->marked;
case LUA_TUSERDATA: case LUA_TUSERDATA:
return ismarkedudata(uvalue(o)); return ismarkedudata(v->u);
case LUA_TTABLE: case LUA_TTABLE:
return ismarked(hvalue(o)); return ismarked(v->h);
case LUA_TFUNCTION: case LUA_TFUNCTION:
return ismarked(clvalue(o)); return ismarked(v->cl);
default: /* number, nil */ default: /* number, nil */
return 1; return 1;
} }
} }
static void invalidatetable (Hash *h) { static void cleardeadnodes (Hash *h) {
int i; int i;
for (i=0; i<h->size; i++) { for (i=0; i<h->size; i++) {
Node *n = node(h, i); Node *n = node(h, i);
TObject k;
if (ttype(val(n)) == LUA_TNIL) continue; /* empty node */ if (ttype(val(n)) == LUA_TNIL) continue; /* empty node */
setkey2obj(&k, n); if (!hasmark(ttype(val(n)), &(val(n)->value)) ||
if (!hasmark(val(n)) || !hasmark(&k)) { !hasmark(ttype_key(n), &n->key_value)) {
setnilvalue(val(n)); /* remove value */ setnilvalue(val(n)); /* remove value */
removekey(n); removekey(n);
} }
@ -211,27 +211,26 @@ static void invalidatetable (Hash *h) {
} }
static void invalidatetables (global_State *G) { static void cleartables (global_State *G) {
Hash *h; Hash *h;
for (h = G->roottable; h; h = h->next) { for (h = G->roottable; h; h = h->next) {
if (ismarked(h) && h->weakmode) if (h->weakmode && ismarked(h))
invalidatetable(h); cleardeadnodes(h);
} }
} }
static void collectproto (lua_State *L) { static void collectproto (lua_State *L) {
Proto **p = &G(L)->rootproto; Proto **p = &G(L)->rootproto;
Proto *next; Proto *curr;
while ((next = *p) != NULL) { while ((curr = *p) != NULL) {
if (next->marked) { if (curr->marked) {
next->marked = 0; curr->marked = 0;
p = &next->next; p = &curr->next;
} }
else { else {
*p = next->next; *p = curr->next;
luaF_freeproto(L, next); luaF_freeproto(L, curr);
} }
} }
} }
@ -239,15 +238,15 @@ static void collectproto (lua_State *L) {
static void collectclosure (lua_State *L) { static void collectclosure (lua_State *L) {
Closure **p = &G(L)->rootcl; Closure **p = &G(L)->rootcl;
Closure *next; Closure *curr;
while ((next = *p) != NULL) { while ((curr = *p) != NULL) {
if (ismarked(next)) { if (ismarked(curr)) {
next->mark = next; /* unmark */ curr->mark = curr; /* unmark */
p = &next->next; p = &curr->next;
} }
else { else {
*p = next->next; *p = curr->next;
luaF_freeclosure(L, next); luaF_freeclosure(L, curr);
} }
} }
} }
@ -255,15 +254,15 @@ static void collectclosure (lua_State *L) {
static void collecttable (lua_State *L) { static void collecttable (lua_State *L) {
Hash **p = &G(L)->roottable; Hash **p = &G(L)->roottable;
Hash *next; Hash *curr;
while ((next = *p) != NULL) { while ((curr = *p) != NULL) {
if (ismarked(next)) { if (ismarked(curr)) {
next->mark = next; /* unmark */ curr->mark = curr; /* unmark */
p = &next->next; p = &curr->next;
} }
else { else {
*p = next->next; *p = curr->next;
luaH_free(L, next); luaH_free(L, curr);
} }
} }
} }
@ -271,17 +270,17 @@ static void collecttable (lua_State *L) {
void luaC_collectudata (lua_State *L) { void luaC_collectudata (lua_State *L) {
Udata **p = &G(L)->rootudata; Udata **p = &G(L)->rootudata;
Udata *next; Udata *curr;
while ((next = *p) != NULL) { while ((curr = *p) != NULL) {
if (ismarkedudata(next)) { if (ismarkedudata(curr)) {
switchudatamark(next); /* unmark */ switchudatamark(curr); /* unmark */
p = &next->next; p = &curr->next;
} }
else { /* collect */ else { /* collect */
int tag = next->tag; int tag = curr->tag;
*p = next->next; *p = curr->next;
next->next = G(L)->TMtable[tag].collected; /* chain udata */ curr->next = G(L)->TMtable[tag].collected; /* chain udata */
G(L)->TMtable[tag].collected = next; G(L)->TMtable[tag].collected = curr;
} }
} }
} }
@ -291,17 +290,17 @@ static void collectstrings (lua_State *L, int all) {
int i; int i;
for (i=0; i<G(L)->strt.size; i++) { /* for each list */ for (i=0; i<G(L)->strt.size; i++) { /* for each list */
TString **p = &G(L)->strt.hash[i]; TString **p = &G(L)->strt.hash[i];
TString *next; TString *curr;
while ((next = *p) != NULL) { while ((curr = *p) != NULL) {
if (next->marked && !all) { /* preserve? */ if (curr->marked && !all) { /* preserve? */
if (next->marked < FIXMARK) /* does not change FIXMARKs */ if (curr->marked < FIXMARK) /* does not change FIXMARKs */
next->marked = 0; curr->marked = 0;
p = &next->nexthash; p = &curr->nexthash;
} }
else { /* collect */ else { /* collect */
*p = next->nexthash; *p = curr->nexthash;
G(L)->strt.nuse--; G(L)->strt.nuse--;
luaM_free(L, next, sizestring(next->len)); luaM_free(L, curr, sizestring(curr->len));
} }
} }
} }
@ -365,11 +364,12 @@ void luaC_collect (lua_State *L, int all) {
void luaC_collectgarbage (lua_State *L) { void luaC_collectgarbage (lua_State *L) {
markall(L); markall(L);
invalidatetables(G(L)); cleartables(G(L));
luaC_collect(L, 0); luaC_collect(L, 0);
checkMbuffer(L); checkMbuffer(L);
G(L)->GCthreshold = 2*G(L)->nblocks; /* set new threshold */ G(L)->GCthreshold = 2*G(L)->nblocks; /* temporary threshold (for TM) */
luaC_callgcTMudata(L); luaC_callgcTMudata(L);
G(L)->GCthreshold = 2*G(L)->nblocks; /* new threshold */
callgcTM(L, &luaO_nilobject); callgcTM(L, &luaO_nilobject);
} }

72
lvm.c
View File

@ -1,5 +1,5 @@
/* /*
** $Id: lvm.c,v 1.183 2001/06/08 19:20:02 roberto Exp roberto $ ** $Id: lvm.c,v 1.184 2001/06/11 14:56:42 roberto Exp roberto $
** Lua virtual machine ** Lua virtual machine
** See Copyright Notice in lua.h ** See Copyright Notice in lua.h
*/ */
@ -106,17 +106,17 @@ void luaV_Lclosure (lua_State *L, Proto *l, int nelems) {
} }
static void callTM (lua_State *L, Closure *f, const l_char *fmt, ...) { /* maximum stack used by a call to a tag method (func + args) */
#define MAXSTACK_TM 4
static StkId callTM (lua_State *L, Closure *f, const l_char *fmt, ...) {
va_list argp; va_list argp;
StkId base = L->top; StkId base = L->top;
StkId result = NULL; /* result position */ lua_assert(strlen(fmt)+1 <= MAXSTACK_TM);
luaD_checkstack(L, MAXSTACK_TM);
va_start(argp, fmt); va_start(argp, fmt);
setclvalue(L->top, f); /* push function */ setclvalue(L->top, f); /* push function */
incr_top; L->top++;
if (*fmt == l_c('r')) {
fmt++;
result = va_arg(argp, TObject *); /* result position */
}
while (*fmt) { while (*fmt) {
if (*fmt++ == l_c('o')) { if (*fmt++ == l_c('o')) {
setobj(L->top, va_arg(argp, TObject *)); setobj(L->top, va_arg(argp, TObject *));
@ -125,17 +125,24 @@ static void callTM (lua_State *L, Closure *f, const l_char *fmt, ...) {
lua_assert(*(fmt-1) == l_c('s')); lua_assert(*(fmt-1) == l_c('s'));
setsvalue(L->top, va_arg(argp, TString *)); setsvalue(L->top, va_arg(argp, TString *));
} }
incr_top; L->top++;
} }
luaD_call(L, base); luaD_call(L, base);
if (result) { /* need result? */ va_end(argp);
if (L->top == base) /* are there valid results? */ return base;
setnilvalue(result); /* function had no results */ }
else
setobj(result, base); /* get first result */
#define setTM(L, base) (L->top = (base))
static void setTMresult (lua_State *L, TObject *result, StkId base) {
if (L->top == base) { /* are there valid results? */
setnilvalue(result); /* function had no results */
}
else {
setobj(result, base); /* get first result */
} }
L->top = base; /* restore top */ L->top = base; /* restore top */
va_end(argp);
} }
@ -164,7 +171,7 @@ void luaV_gettable (lua_State *L, StkId t, TObject *key, StkId res) {
if (tm == NULL) /* no tag method? */ if (tm == NULL) /* no tag method? */
luaG_typeerror(L, t, l_s("index")); luaG_typeerror(L, t, l_s("index"));
} }
callTM(L, tm, l_s("roo"), res, t, key); setTMresult(L, res, callTM(L, tm, l_s("oo"), t, key));
} }
@ -187,7 +194,7 @@ void luaV_settable (lua_State *L, StkId t, TObject *key, StkId val) {
if (tm == NULL) /* no tag method? */ if (tm == NULL) /* no tag method? */
luaG_typeerror(L, t, l_s("index")); luaG_typeerror(L, t, l_s("index"));
} }
callTM(L, tm, l_s("ooo"), t, key, val); setTM(L, callTM(L, tm, l_s("ooo"), t, key, val));
} }
@ -197,8 +204,9 @@ void luaV_getglobal (lua_State *L, TString *name, StkId res) {
if (!HAS_TM_GETGLOBAL(L, ttype(value)) || /* is there a tag method? */ if (!HAS_TM_GETGLOBAL(L, ttype(value)) || /* is there a tag method? */
(tm = luaT_gettmbyObj(G(L), value, TM_GETGLOBAL)) == NULL) { (tm = luaT_gettmbyObj(G(L), value, TM_GETGLOBAL)) == NULL) {
setobj(res, value); /* default behavior */ setobj(res, value); /* default behavior */
} else }
callTM(L, tm, l_s("rso"), res, name, value); else
setTMresult(L, res, callTM(L, tm, l_s("so"), name, value));
} }
@ -208,8 +216,9 @@ void luaV_setglobal (lua_State *L, TString *name, StkId val) {
if (!HAS_TM_SETGLOBAL(L, ttype(oldvalue)) || /* no tag methods? */ if (!HAS_TM_SETGLOBAL(L, ttype(oldvalue)) || /* no tag methods? */
(tm = luaT_gettmbyObj(G(L), oldvalue, TM_SETGLOBAL)) == NULL) { (tm = luaT_gettmbyObj(G(L), oldvalue, TM_SETGLOBAL)) == NULL) {
setobj(oldvalue, val); /* raw set */ setobj(oldvalue, val); /* raw set */
} else }
callTM(L, tm, l_s("soo"), name, oldvalue, val); else
setTM(L, callTM(L, tm, l_s("soo"), name, oldvalue, val));
} }
@ -226,7 +235,7 @@ static int call_binTM (lua_State *L, const TObject *p1, const TObject *p2,
} }
} }
opname = luaS_new(L, luaT_eventname[event]); opname = luaS_new(L, luaT_eventname[event]);
callTM(L, tm, l_s("roos"), res, p1, p2, opname); setTMresult(L, res, callTM(L, tm, l_s("oos"), p1, p2, opname));
return 1; return 1;
} }
@ -566,15 +575,16 @@ StkId luaV_execute (lua_State *L, const Closure *cl, StkId base) {
return ra; return ra;
} }
case OP_FORPREP: { case OP_FORPREP: {
int jmp = GETARG_sBc(i);
if (luaV_tonumber(ra, ra) == NULL) if (luaV_tonumber(ra, ra) == NULL)
luaD_error(L, l_s("`for' initial value must be a number")); luaD_error(L, l_s("`for' initial value must be a number"));
if (luaV_tonumber(ra+1, ra+1) == NULL) if (luaV_tonumber(ra+1, ra+1) == NULL)
luaD_error(L, l_s("`for' limit must be a number")); luaD_error(L, l_s("`for' limit must be a number"));
if (luaV_tonumber(ra+2, ra+2) == NULL) if (luaV_tonumber(ra+2, ra+2) == NULL)
luaD_error(L, l_s("`for' step must be a number")); luaD_error(L, l_s("`for' step must be a number"));
pc += -jmp; /* `jump' to loop end (delta is negated here) */
nvalue(ra) -= nvalue(ra+2);/* decrement index (to be incremented) */ nvalue(ra) -= nvalue(ra+2);/* decrement index (to be incremented) */
pc += -GETARG_sBc(i); /* `jump' to loop end (delta is negated here) */
/* store in `ra+1' total number of repetitions */
nvalue(ra+1) = ((nvalue(ra+1)-nvalue(ra))/nvalue(ra+2));
/* go through */ /* go through */
} }
case OP_FORLOOP: { case OP_FORLOOP: {
@ -582,28 +592,26 @@ StkId luaV_execute (lua_State *L, const Closure *cl, StkId base) {
ttype(ra+2) == LUA_TNUMBER); ttype(ra+2) == LUA_TNUMBER);
if (ttype(ra) != LUA_TNUMBER) if (ttype(ra) != LUA_TNUMBER)
luaD_error(L, l_s("`for' index must be a number")); luaD_error(L, l_s("`for' index must be a number"));
nvalue(ra) += nvalue(ra+2); /* increment index */ if (--nvalue(ra+1) >= 0) {
if (nvalue(ra+2) > 0 ? nvalue(ra) += nvalue(ra+2); /* increment index */
nvalue(ra) <= nvalue(ra+1) :
nvalue(ra) >= nvalue(ra+1))
dojump(pc, i); /* repeat loop */ dojump(pc, i); /* repeat loop */
}
break; break;
} }
case OP_TFORPREP: { case OP_TFORPREP: {
int jmp = GETARG_sBc(i);
if (ttype(ra) != LUA_TTABLE) if (ttype(ra) != LUA_TTABLE)
luaD_error(L, l_s("`for' table must be a table")); luaD_error(L, l_s("`for' table must be a table"));
setnvalue(ra+1, -1); /* initial index */ setnvalue(ra+1, -1); /* initial index */
setnilvalue(ra+2); setnilvalue(ra+2);
setnilvalue(ra+3); setnilvalue(ra+3);
pc += -jmp; /* `jump' to loop end (delta is negated here) */ pc += -GETARG_sBc(i); /* `jump' to loop end (delta is negated here) */
/* go through */ /* go through */
} }
case OP_TFORLOOP: { case OP_TFORLOOP: {
Hash *t; Hash *t;
int n; int n;
runtime_check(L, ttype(ra) == LUA_TTABLE); runtime_check(L, ttype(ra) == LUA_TTABLE &&
runtime_check(L, ttype(ra+1) == LUA_TNUMBER); ttype(ra+1) == LUA_TNUMBER);
t = hvalue(ra); t = hvalue(ra);
n = (int)nvalue(ra+1); n = (int)nvalue(ra+1);
n = luaH_nexti(t, n); n = luaH_nexti(t, n);