Browse Source

small optimizations

Roberto Ierusalimschy 23 năm trước cách đây
mục cha
commit
42754c0f15
4 tập tin đã thay đổi với 18 bổ sung29 xóa
  1. 3 1
      lapi.c
  2. 0 10
      ldo.c
  3. 0 1
      ldo.h
  4. 15 17
      lvm.c

+ 3 - 1
lapi.c

@@ -104,7 +104,9 @@ LUA_API void lua_settop (lua_State *L, int index) {
   lua_lock(L);
   lua_lock(L);
   if (index >= 0) {
   if (index >= 0) {
     api_check(L, index <= L->stack_last - L->ci->base);
     api_check(L, index <= L->stack_last - L->ci->base);
-    luaD_adjusttop(L, L->ci->base+index);
+    while (L->top < L->ci->base + index)
+      setnilvalue(L->top++);
+    L->top = L->ci->base + index;
   }
   }
   else {
   else {
     api_check(L, -(index+1) <= (L->top - L->ci->base));
     api_check(L, -(index+1) <= (L->top - L->ci->base));

+ 0 - 10
ldo.c

@@ -68,16 +68,6 @@ void luaD_stackerror (lua_State *L) {
 }
 }
 
 
 
 
-/*
-** adjust top to new value; assume that new top is valid
-*/
-void luaD_adjusttop (lua_State *L, StkId newtop) {
-  while (L->top < newtop)
-    setnilvalue(L->top++);
-  L->top = newtop;  /* `newtop' could be lower than `top' */
-}
-
-
 /*
 /*
 ** Open a hole inside the stack at `pos'
 ** Open a hole inside the stack at `pos'
 */
 */

+ 0 - 1
ldo.h

@@ -23,7 +23,6 @@
 
 
 
 
 void luaD_init (lua_State *L, int stacksize);
 void luaD_init (lua_State *L, int stacksize);
-void luaD_adjusttop (lua_State *L, StkId newtop);
 void luaD_lineHook (lua_State *L, int line, lua_Hook linehook);
 void luaD_lineHook (lua_State *L, int line, lua_Hook linehook);
 void luaD_callHook (lua_State *L, lua_Hook callhook, const char *event);
 void luaD_callHook (lua_State *L, lua_Hook callhook, const char *event);
 StkId luaD_precall (lua_State *L, StkId func);
 StkId luaD_precall (lua_State *L, StkId func);

+ 15 - 17
lvm.c

@@ -256,33 +256,29 @@ void luaV_strconc (lua_State *L, int total, StkId top) {
 }
 }
 
 
 
 
-static void luaV_pack (lua_State *L, StkId firstelem) {
+static void adjust_varargs (lua_State *L, StkId base, int nfixargs) {
   int i;
   int i;
-  Table *htab = luaH_new(L, 0, 0);
+  Table *htab;
   TObject n, nname;
   TObject n, nname;
-  for (i=0; firstelem+i<L->top; i++)
-    luaH_setnum(L, htab, i+1, firstelem+i);
+  StkId firstvar = base + nfixargs;  /* position of first vararg */
+  if (L->top < firstvar) {
+    luaD_checkstack(L, firstvar - L->top);
+    while (L->top < firstvar)
+      setnilvalue(L->top++);
+  }
+  htab = luaH_new(L, 0, 0);
+  for (i=0; firstvar+i<L->top; i++)
+    luaH_setnum(L, htab, i+1, firstvar+i);
   /* store counter in field `n' */
   /* store counter in field `n' */
   setnvalue(&n, i);
   setnvalue(&n, i);
   setsvalue(&nname, luaS_newliteral(L, "n"));
   setsvalue(&nname, luaS_newliteral(L, "n"));
   luaH_set(L, htab, &nname, &n);
   luaH_set(L, htab, &nname, &n);
-  L->top = firstelem;  /* remove elements from the stack */
+  L->top = firstvar;  /* remove elements from the stack */
   sethvalue(L->top, htab);
   sethvalue(L->top, htab);
   incr_top;
   incr_top;
 }
 }
 
 
 
 
-static void adjust_varargs (lua_State *L, StkId base, int nfixargs) {
-  int nvararg = (L->top-base) - nfixargs;
-  StkId firstvar = base + nfixargs;  /* position of first vararg */
-  if (nvararg < 0) {
-    luaD_checkstack(L, -nvararg);
-    luaD_adjusttop(L, firstvar);
-  }
-  luaV_pack(L, firstvar);
-}
-
-
 static void powOp (lua_State *L, StkId ra, StkId rb, StkId rc) {
 static void powOp (lua_State *L, StkId ra, StkId rb, StkId rc) {
   const TObject *b = rb;
   const TObject *b = rb;
   const TObject *c = rc;
   const TObject *c = rc;
@@ -352,7 +348,9 @@ StkId luaV_execute (lua_State *L, const LClosure *cl, StkId base) {
     adjust_varargs(L, base, cl->p->numparams);
     adjust_varargs(L, base, cl->p->numparams);
   if (base > L->stack_last - cl->p->maxstacksize)
   if (base > L->stack_last - cl->p->maxstacksize)
     luaD_stackerror(L);
     luaD_stackerror(L);
-  luaD_adjusttop(L, base + cl->p->maxstacksize);
+  while (L->top < base + cl->p->maxstacksize)
+    setnilvalue(L->top++);
+  L->top = base + cl->p->maxstacksize;
   L->ci->pc = &pc;
   L->ci->pc = &pc;
   linehook = L->ci->linehook = L->linehook;
   linehook = L->ci->linehook = L->linehook;
   pc = cl->p->code;
   pc = cl->p->code;