Explorar o código

keep L->ci->base in L->base for faster access

Roberto Ierusalimschy %!s(int64=23) %!d(string=hai) anos
pai
achega
ddc144e4d2
Modificáronse 7 ficheiros con 48 adicións e 41 borrados
  1. 17 17
      lapi.c
  2. 2 2
      ldebug.c
  3. 16 12
      ldo.c
  4. 2 2
      lobject.c
  5. 2 2
      lstate.c
  6. 2 1
      lstate.h
  7. 7 5
      lvm.c

+ 17 - 17
lapi.c

@@ -1,5 +1,5 @@
 /*
-** $Id: lapi.c,v 1.220 2002/11/14 16:15:53 roberto Exp roberto $
+** $Id: lapi.c,v 1.221 2002/11/21 14:16:52 roberto Exp roberto $
 ** Lua API
 ** See Copyright Notice in lua.h
 */
@@ -36,7 +36,7 @@ const char lua_ident[] =
 #define api_check(L, o)		/*{ assert(o); }*/
 #endif
 
-#define api_checknelems(L, n)	api_check(L, (n) <= (L->top - L->ci->base))
+#define api_checknelems(L, n)	api_check(L, (n) <= (L->top - L->base))
 
 #define api_incr_top(L)   {api_check(L, L->top < L->ci->top); L->top++;}
 
@@ -45,14 +45,14 @@ const char lua_ident[] =
 
 static TObject *negindex (lua_State *L, int index) {
   if (index > LUA_REGISTRYINDEX) {
-    api_check(L, index != 0 && -index <= L->top - L->ci->base);
+    api_check(L, index != 0 && -index <= L->top - L->base);
     return L->top+index;
   }
   else switch (index) {  /* pseudo-indices */
     case LUA_REGISTRYINDEX: return registry(L);
     case LUA_GLOBALSINDEX: return gt(L);
     default: {
-      TObject *func = (L->ci->base - 1);
+      TObject *func = (L->base - 1);
       index = LUA_GLOBALSINDEX - index;
       api_check(L, iscfunction(func) && index <= clvalue(func)->c.nupvalues);
       return &clvalue(func)->c.upvalue[index-1];
@@ -63,8 +63,8 @@ static TObject *negindex (lua_State *L, int index) {
 
 static TObject *luaA_index (lua_State *L, int index) {
   if (index > 0) {
-    api_check(L, index <= L->top - L->ci->base);
-    return L->ci->base + index - 1;
+    api_check(L, index <= L->top - L->base);
+    return L->base + index - 1;
   }
   else
     return negindex(L, index);
@@ -73,8 +73,8 @@ static TObject *luaA_index (lua_State *L, int index) {
 
 static TObject *luaA_indexAcceptable (lua_State *L, int index) {
   if (index > 0) {
-    TObject *o = L->ci->base+(index-1);
-    api_check(L, index <= L->stack_last - L->ci->base);
+    TObject *o = L->base+(index-1);
+    api_check(L, index <= L->stack_last - L->base);
     if (o >= L->top) return NULL;
     else return o;
   }
@@ -92,7 +92,7 @@ void luaA_pushobject (lua_State *L, const TObject *o) {
 LUA_API int lua_checkstack (lua_State *L, int size) {
   int res;
   lua_lock(L);
-  if ((L->top - L->ci->base + size) > LUA_MAXCSTACK)
+  if ((L->top - L->base + size) > LUA_MAXCSTACK)
     res = 0;  /* stack overflow */
   else {
     luaD_checkstack(L, size);
@@ -148,20 +148,20 @@ LUA_API lua_State *lua_newthread (lua_State *L) {
 
 
 LUA_API int lua_gettop (lua_State *L) {
-  return (L->top - L->ci->base);
+  return (L->top - L->base);
 }
 
 
 LUA_API void lua_settop (lua_State *L, int index) {
   lua_lock(L);
   if (index >= 0) {
-    api_check(L, index <= L->stack_last - L->ci->base);
-    while (L->top < L->ci->base + index)
+    api_check(L, index <= L->stack_last - L->base);
+    while (L->top < L->base + index)
       setnilvalue(L->top++);
-    L->top = L->ci->base + index;
+    L->top = L->base + index;
   }
   else {
-    api_check(L, -(index+1) <= (L->top - L->ci->base));
+    api_check(L, -(index+1) <= (L->top - L->base));
     L->top += index+1;  /* `subtract' index (index is negative) */
   }
   lua_unlock(L);
@@ -763,7 +763,7 @@ LUA_API void lua_concat (lua_State *L, int n) {
   luaC_checkGC(L);
   api_checknelems(L, n);
   if (n >= 2) {
-    luaV_concat(L, n, L->top - L->ci->base - 1);
+    luaV_concat(L, n, L->top - L->base - 1);
     L->top -= (n-1);
   }
   else if (n == 0) {  /* push empty string */
@@ -791,8 +791,8 @@ LUA_API int lua_pushupvalues (lua_State *L) {
   Closure *func;
   int n, i;
   lua_lock(L);
-  api_check(L, iscfunction(L->ci->base - 1));
-  func = clvalue(L->ci->base - 1);
+  api_check(L, iscfunction(L->base - 1));
+  func = clvalue(L->base - 1);
   n = func->c.nupvalues;
   luaD_checkstack(L, n + LUA_MINSTACK);
   for (i=0; i<n; i++) {

+ 2 - 2
ldebug.c

@@ -1,5 +1,5 @@
 /*
-** $Id: ldebug.c,v 1.136 2002/11/07 15:37:10 roberto Exp roberto $
+** $Id: ldebug.c,v 1.137 2002/11/18 11:01:55 roberto Exp roberto $
 ** Debug Interface
 ** See Copyright Notice in lua.h
 */
@@ -497,7 +497,7 @@ void luaG_typeerror (lua_State *L, const TObject *o, const char *op) {
   const char *name = NULL;
   const char *t = luaT_typenames[ttype(o)];
   const char *kind = (isinstack(L->ci, o)) ?
-                         getobjname(L->ci, o - L->ci->base, &name) : NULL;
+                         getobjname(L->ci, o - L->base, &name) : NULL;
   if (kind)
     luaG_runerror(L, "attempt to %s %s `%s' (a %s value)",
                 op, kind, name, t);

+ 16 - 12
ldo.c

@@ -1,5 +1,5 @@
 /*
-** $Id: ldo.c,v 1.203 2002/11/18 15:24:11 roberto Exp roberto $
+** $Id: ldo.c,v 1.204 2002/11/18 18:45:38 roberto Exp roberto $
 ** Stack and Call structure of Lua
 ** See Copyright Notice in lua.h
 */
@@ -115,6 +115,7 @@ static void correctstack (lua_State *L, TObject *oldstack) {
     }
     ci->base = newbase;
   }
+  L->base = L->ci->base;
 }
 
 
@@ -230,8 +231,8 @@ StkId luaD_precall (lua_State *L, StkId func) {
       adjust_varargs(L, p->numparams, func+1);
     luaD_checkstack(L, p->maxstacksize);
     ci = ++L->ci;  /* now `enter' new function */
-    ci->base = restorestack(L, funcr) + 1;
-    ci->top = ci->base + p->maxstacksize;
+    L->base = L->ci->base = restorestack(L, funcr) + 1;
+    ci->top = L->base + p->maxstacksize;
     ci->u.l.savedpc = p->code;  /* starting point */
     ci->state = CI_SAVEDPC;
     while (L->top < ci->top)
@@ -244,7 +245,7 @@ StkId luaD_precall (lua_State *L, StkId func) {
     int n;
     luaD_checkstack(L, LUA_MINSTACK);  /* ensure minimum stack size */
     ci = ++L->ci;  /* now `enter' new function */
-    ci->base = restorestack(L, funcr) + 1;
+    L->base = L->ci->base = restorestack(L, funcr) + 1;
     ci->top = L->top + LUA_MINSTACK;
     ci->state = CI_C;  /* a C function */
     if (L->hookmask & LUA_MASKCALL) {
@@ -255,7 +256,7 @@ StkId luaD_precall (lua_State *L, StkId func) {
 #ifdef LUA_COMPATUPVALUES
     lua_pushupvalues(L);
 #endif
-    n = (*clvalue(ci->base-1)->c.f)(L);  /* do the actual call */
+    n = (*clvalue(L->base - 1)->c.f)(L);  /* do the actual call */
     lua_lock(L);
     return L->top - n;
   }
@@ -269,8 +270,9 @@ void luaD_poscall (lua_State *L, int wanted, StkId firstResult) {
     luaD_callhook(L, LUA_HOOKRET, -1);
     firstResult = restorestack(L, fr);
   }
-  res = L->ci->base - 1;  /* res == final position of 1st result */
+  res = L->base - 1;  /* res == final position of 1st result */
   L->ci--;
+  L->base = L->ci->base;  /* restore base */
   /* move results to correct place */
   while (wanted != 0 && firstResult < L->top) {
     setobjs2s(res++, firstResult++);
@@ -307,7 +309,7 @@ static void resume (lua_State *L, void *ud) {
   int nargs = *cast(int *, ud);
   CallInfo *ci = L->ci;
   if (ci == L->base_ci) {  /* no activation record? */
-    if (nargs >= L->top - L->ci->base)
+    if (nargs >= L->top - L->base)
       luaG_runerror(L, "cannot resume dead coroutine");
     luaD_precall(L, L->top - (nargs + 1));  /* start coroutine */
   }
@@ -343,8 +345,9 @@ LUA_API int lua_resume (lua_State *L, int nargs) {
   status = luaD_rawrunprotected(L, resume, &nargs);
   if (status != 0) {  /* error? */
     L->ci = L->base_ci;  /* go back to initial level */
-    luaF_close(L, L->ci->base);  /* close eventual pending closures */
-    seterrorobj(L, status, L->ci->base);
+    L->base = L->ci->base;
+    luaF_close(L, L->base);  /* close eventual pending closures */
+    seterrorobj(L, status, L->base);
     L->allowhook = old_allowhooks;
     restore_stack_limit(L);
   }
@@ -360,11 +363,11 @@ LUA_API int lua_yield (lua_State *L, int nresults) {
   if (ci->state & CI_C) {  /* usual yield */
     if ((ci-1)->state & CI_C)
       luaG_runerror(L, "cannot yield a C function");
-    if (L->top - nresults > ci->base) {  /* is there garbage in the stack? */
+    if (L->top - nresults > L->base) {  /* is there garbage in the stack? */
       int i;
       for (i=0; i<nresults; i++)  /* move down results */
-        setobjs2s(ci->base + i, L->top - nresults + i);
-      L->top = ci->base + nresults;
+        setobjs2s(L->base + i, L->top - nresults + i);
+      L->top = L->base + nresults;
     }
   }
   /* else it's an yield inside a hook: nothing to do */
@@ -405,6 +408,7 @@ int luaD_pcall (lua_State *L, int nargs, int nresults, ptrdiff_t errfunc) {
     luaF_close(L, oldtop);  /* close eventual pending closures */
     seterrorobj(L, status, oldtop);
     L->ci = restoreci(L, old_ci);
+    L->base = L->ci->base;
     L->allowhook = old_allowhooks;
     restore_stack_limit(L);
   }

+ 2 - 2
lobject.c

@@ -1,5 +1,5 @@
 /*
-** $Id: lobject.c,v 1.91 2002/10/22 17:18:28 roberto Exp roberto $
+** $Id: lobject.c,v 1.92 2002/11/07 15:37:10 roberto Exp roberto $
 ** Some generic functions over Lua objects
 ** See Copyright Notice in lua.h
 */
@@ -128,7 +128,7 @@ const char *luaO_pushvfstring (lua_State *L, const char *fmt, va_list argp) {
     fmt = e+2;
   }
   pushstr(L, fmt);
-  luaV_concat(L, n+1, L->top - L->ci->base - 1);
+  luaV_concat(L, n+1, L->top - L->base - 1);
   L->top -= n;
   return svalue(L->top - 1);
 }

+ 2 - 2
lstate.c

@@ -1,5 +1,5 @@
 /*
-** $Id: lstate.c,v 1.113 2002/11/19 14:12:13 roberto Exp roberto $
+** $Id: lstate.c,v 1.114 2002/11/21 14:14:42 roberto Exp roberto $
 ** Global State
 ** See Copyright Notice in lua.h
 */
@@ -66,7 +66,7 @@ static void stack_init (lua_State *L1, lua_State *L) {
   L1->ci = L1->base_ci;
   L1->ci->state = CI_C;  /*  not a Lua function */
   setnilvalue(L1->top++);  /* `function' entry for this `ci' */
-  L1->ci->base = L1->top;
+  L1->base = L1->ci->base = L1->top;
   L1->ci->top = L1->top + LUA_MINSTACK;
   L1->size_ci = BASIC_CI_SIZE;
   L1->end_ci = L1->base_ci + L1->size_ci;

+ 2 - 1
lstate.h

@@ -1,5 +1,5 @@
 /*
-** $Id: lstate.h,v 1.102 2002/11/18 11:01:55 roberto Exp roberto $
+** $Id: lstate.h,v 1.103 2002/11/18 15:23:43 roberto Exp roberto $
 ** Global State
 ** See Copyright Notice in lua.h
 */
@@ -131,6 +131,7 @@ typedef struct global_State {
 struct lua_State {
   CommonHeader;
   StkId top;  /* first free slot in the stack */
+  StkId base;  /* base of current function */
   global_State *l_G;
   CallInfo *ci;  /* call info for current function */
   StkId stack_last;  /* last free slot in the stack */

+ 7 - 5
lvm.c

@@ -1,5 +1,5 @@
 /*
-** $Id: lvm.c,v 1.264 2002/11/19 08:50:56 roberto Exp roberto $
+** $Id: lvm.c,v 1.265 2002/11/21 14:18:01 roberto Exp roberto $
 ** Lua virtual machine
 ** See Copyright Notice in lua.h
 */
@@ -283,7 +283,7 @@ int luaV_equalval (lua_State *L, const TObject *t1, const TObject *t2) {
 
 void luaV_concat (lua_State *L, int total, int last) {
   do {
-    StkId top = L->ci->base + last + 1;
+    StkId top = L->base + last + 1;
     int n = 2;  /* number of elements handled in this pass (at least 2) */
     if (!tostring(L, top-2) || !tostring(L, top-1)) {
       if (!call_binTM(L, top-2, top-1, top-2, TM_CONCAT))
@@ -377,7 +377,7 @@ StkId luaV_execute (lua_State *L) {
              L->ci->state == (CI_SAVEDPC | CI_CALLING));
   L->ci->state = CI_HASFRAME;  /* activate frame */
   pc = L->ci->u.l.savedpc;
-  base = L->ci->base;
+  base = L->base;
   cl = &clvalue(base - 1)->l;
   k = cl->p->k;
   /* main loop of interpreter */
@@ -394,9 +394,10 @@ StkId luaV_execute (lua_State *L) {
       }
     }
     /* warning!! several calls may realloc the stack and invalidate `ra' */
-    lua_assert((L->ci->state & CI_HASFRAME) && base == L->ci->base);
     ra = RA(i);
-    lua_assert(L->top <= L->stack + L->stacksize && L->top >= L->ci->base);
+    lua_assert(L->ci->state & CI_HASFRAME);
+    lua_assert(base == L->base && base == L->ci->base);
+    lua_assert(L->top <= L->stack + L->stacksize && L->top >= base);
     lua_assert(L->top == L->ci->top ||
          GET_OPCODE(i) == OP_CALL ||   GET_OPCODE(i) == OP_TAILCALL ||
          GET_OPCODE(i) == OP_RETURN || GET_OPCODE(i) == OP_SETLISTO);
@@ -618,6 +619,7 @@ StkId luaV_execute (lua_State *L) {
             (L->ci - 1)->u.l.savedpc = L->ci->u.l.savedpc;
             (L->ci - 1)->state = CI_SAVEDPC;
             L->ci--;  /* remove new frame */
+            L->base = L->ci->base;
           }
           goto callentry;
         }