small optimizations

This commit is contained in:
Roberto Ierusalimschy 2005-04-05 10:41:29 -03:00
parent 0316308c0d
commit 7b6c3b463e
5 changed files with 33 additions and 34 deletions

View File

@ -1,5 +1,5 @@
/*
** $Id: ldebug.c,v 2.12 2004/12/20 15:50:00 roberto Exp roberto $
** $Id: ldebug.c,v 2.13 2005/04/04 18:12:51 roberto Exp roberto $
** Debug Interface
** See Copyright Notice in lua.h
*/
@ -34,8 +34,9 @@ static const char *getfuncname (lua_State *L, CallInfo *ci, const char **name);
static int currentpc (lua_State *L, CallInfo *ci) {
UNUSED(L);
if (!isLua(ci)) return -1; /* function is not a Lua function? */
if (ci == L->ci)
ci->savedpc = L->savedpc;
return pcRel(ci->savedpc, ci_func(ci)->l.p);
}

7
ldo.c
View File

@ -1,5 +1,5 @@
/*
** $Id: ldo.c,v 2.20 2005/03/28 17:17:53 roberto Exp roberto $
** $Id: ldo.c,v 2.21 2005/03/29 16:20:48 roberto Exp roberto $
** Stack and Call structure of Lua
** See Copyright Notice in lua.h
*/
@ -253,6 +253,7 @@ int luaD_precall (lua_State *L, StkId func, int nresults) {
func = tryfuncTM(L, func); /* check the `function' tag method */
funcr = savestack(L, func);
cl = &clvalue(func)->l;
L->ci->savedpc = L->savedpc;
if (!cl->isC) { /* Lua function? prepare its call */
CallInfo *ci;
StkId st, base;
@ -273,7 +274,7 @@ int luaD_precall (lua_State *L, StkId func, int nresults) {
L->base = ci->base = base;
ci->top = L->base + p->maxstacksize;
lua_assert(ci->top <= L->stack_last);
ci->savedpc = p->code; /* starting point */
L->savedpc = p->code; /* starting point */
ci->tailcalls = 0;
ci->nresults = nresults;
for (st = L->top; st < ci->top; st++)
@ -325,6 +326,7 @@ void luaD_poscall (lua_State *L, int wanted, StkId firstResult) {
res = L->ci->func; /* res == final position of 1st result */
L->ci--;
L->base = L->ci->base; /* restore base */
L->savedpc = L->ci->savedpc; /* restore savedpc */
/* move results to correct place */
while (wanted != 0 && firstResult < L->top) {
setobjs2s(L, res++, firstResult++);
@ -451,6 +453,7 @@ int luaD_pcall (lua_State *L, Pfunc func, void *u,
L->nCcalls = oldnCcalls;
L->ci = restoreci(L, old_ci);
L->base = L->ci->base;
L->savedpc = L->ci->savedpc;
L->allowhook = old_allowhooks;
restore_stack_limit(L);
}

View File

@ -1,5 +1,5 @@
/*
** $Id: lstate.c,v 2.27 2005/03/18 18:55:45 roberto Exp roberto $
** $Id: lstate.c,v 2.28 2005/03/22 16:04:29 roberto Exp roberto $
** Global State
** See Copyright Notice in lua.h
*/
@ -94,6 +94,7 @@ static void preinit_state (lua_State *L, global_State *g) {
L->nCcalls = 0;
L->status = 0;
L->base_ci = L->ci = NULL;
L->savedpc = NULL;
L->errfunc = 0;
setnilvalue(gt(L));
}

View File

@ -1,5 +1,5 @@
/*
** $Id: lstate.h,v 2.17 2005/03/18 18:55:09 roberto Exp roberto $
** $Id: lstate.h,v 2.18 2005/03/22 16:04:29 roberto Exp roberto $
** Global State
** See Copyright Notice in lua.h
*/
@ -102,6 +102,7 @@ struct lua_State {
StkId base; /* base of current function */
global_State *l_G;
CallInfo *ci; /* call info for current function */
const Instruction *savedpc; /* `savedpc' of current function */
StkId stack_last; /* last free slot in the stack */
StkId stack; /* stack base */
int stacksize;

49
lvm.c
View File

@ -1,5 +1,5 @@
/*
** $Id: lvm.c,v 2.35 2005/03/28 17:17:53 roberto Exp roberto $
** $Id: lvm.c,v 2.36 2005/04/04 18:12:51 roberto Exp roberto $
** Lua virtual machine
** See Copyright Notice in lua.h
*/
@ -59,9 +59,8 @@ int luaV_tostring (lua_State *L, StkId obj) {
static void traceexec (lua_State *L, const Instruction *pc) {
lu_byte mask = L->hookmask;
CallInfo *ci = L->ci;
const Instruction *oldpc = ci->savedpc;
ci->savedpc = pc;
const Instruction *oldpc = L->savedpc;
L->savedpc = pc;
if (mask > LUA_MASKLINE) { /* instruction-hook set? */
if (L->hookcount == 0) {
resethookcount(L);
@ -70,7 +69,7 @@ static void traceexec (lua_State *L, const Instruction *pc) {
}
}
if (mask & LUA_MASKLINE) {
Proto *p = ci_func(ci)->l.p;
Proto *p = ci_func(L->ci)->l.p;
int npc = pcRel(pc, p);
int newline = getline(p, npc);
/* call linehook when enter a new function, when jump back (loop),
@ -81,17 +80,13 @@ static void traceexec (lua_State *L, const Instruction *pc) {
}
static void prepTMcall (lua_State *L, const TValue *f,
static void callTMres (lua_State *L, StkId res, const TValue *f,
const TValue *p1, const TValue *p2) {
ptrdiff_t result = savestack(L, res);
luaD_checkstack(L, 3);
setobj2s(L, L->top, f); /* push function */
setobj2s(L, L->top+1, p1); /* 1st argument */
setobj2s(L, L->top+2, p2); /* 2nd argument */
}
static void callTMres (lua_State *L, StkId res) {
ptrdiff_t result = savestack(L, res);
luaD_checkstack(L, 3);
L->top += 3;
luaD_call(L, L->top - 3, 1);
res = restorestack(L, result);
@ -101,8 +96,12 @@ static void callTMres (lua_State *L, StkId res) {
static void callTM (lua_State *L) {
static void callTM (lua_State *L, const TValue *f, const TValue *p1, const TValue *p2, const TValue *p3) {
luaD_checkstack(L, 4);
setobj2s(L, L->top, f); /* push function */
setobj2s(L, L->top+1, p1); /* 1st argument */
setobj2s(L, L->top+2, p2); /* 2nd argument */
setobj2s(L, L->top+3, p3); /* 3th argument */
L->top += 4;
luaD_call(L, L->top - 4, 0);
}
@ -125,8 +124,7 @@ void luaV_gettable (lua_State *L, const TValue *t, TValue *key, StkId val) {
else if (ttisnil(tm = luaT_gettmbyobj(L, t, TM_INDEX)))
luaG_typeerror(L, t, "index");
if (ttisfunction(tm)) {
prepTMcall(L, tm, t, key);
callTMres(L, val);
callTMres(L, val, tm, t, key);
return;
}
t = tm; /* else repeat with `tm' */
@ -153,9 +151,7 @@ void luaV_settable (lua_State *L, const TValue *t, TValue *key, StkId val) {
else if (ttisnil(tm = luaT_gettmbyobj(L, t, TM_NEWINDEX)))
luaG_typeerror(L, t, "index");
if (ttisfunction(tm)) {
prepTMcall(L, tm, t, key);
setobj2s(L, L->top+3, val); /* 3th argument */
callTM(L);
callTM(L, tm, t, key, val);
return;
}
t = tm; /* else repeat with `tm' */
@ -170,8 +166,7 @@ static int call_binTM (lua_State *L, const TValue *p1, const TValue *p2,
if (ttisnil(tm))
tm = luaT_gettmbyobj(L, p2, event); /* try second operand */
if (!ttisfunction(tm)) return 0;
prepTMcall(L, tm, p1, p2);
callTMres(L, res);
callTMres(L, res, tm, p1, p2);
return 1;
}
@ -198,8 +193,7 @@ static int call_orderTM (lua_State *L, const TValue *p1, const TValue *p2,
tm2 = luaT_gettmbyobj(L, p2, event);
if (!luaO_rawequalObj(tm1, tm2)) /* different metamethods? */
return -1;
prepTMcall(L, tm1, p1, p2);
callTMres(L, L->top);
callTMres(L, L->top, tm1, p1, p2);
return !l_isfalse(L->top);
}
@ -278,8 +272,7 @@ int luaV_equalval (lua_State *L, const TValue *t1, const TValue *t2) {
default: return gcvalue(t1) == gcvalue(t2);
}
if (tm == NULL) return 0; /* no TM? */
prepTMcall(L, tm, t1, t2);
callTMres(L, L->top); /* call TM */
callTMres(L, L->top, tm, t1, t2); /* call TM */
return !l_isfalse(L->top);
}
@ -369,7 +362,7 @@ StkId luaV_execute (lua_State *L, int nexeccalls) {
if (L->hookmask & LUA_MASKCALL)
luaD_callhook(L, LUA_HOOKCALL, -1);
retentry: /* entry point when returning to old functions */
pc = L->ci->savedpc;
pc = L->savedpc;
cl = &clvalue(L->ci->func)->l;
k = cl->p->k;
/* main loop of interpreter */
@ -381,14 +374,14 @@ StkId luaV_execute (lua_State *L, int nexeccalls) {
(--L->hookcount == 0 || L->hookmask & LUA_MASKLINE)) {
traceexec(L, pc); /***/
if (L->status == LUA_YIELD) { /* did hook yield? */
L->ci->savedpc = pc - 1;
L->savedpc = pc - 1;
return NULL;
}
}
/* warning!! several calls may realloc the stack and invalidate `ra' */
base = L->base;
ra = RA(i);
L->ci->savedpc = pc;
L->savedpc = pc;
lua_assert(base == L->ci->base);
lua_assert(base <= L->top && L->top <= L->stack + L->stacksize);
lua_assert(L->top == L->ci->top || luaG_checkopenop(i));
@ -634,7 +627,7 @@ StkId luaV_execute (lua_State *L, int nexeccalls) {
setobjs2s(L, func+aux, pfunc+aux);
ci->top = L->top = func+aux; /* correct top */
lua_assert(L->top == L->base + clvalue(func)->l.p->maxstacksize);
ci->savedpc = (ci+1)->savedpc;
ci->savedpc = L->savedpc;
ci->tailcalls++; /* one more call lost */
L->ci--; /* remove new frame */
goto callentry;