浏览代码

Removed GC checks from function calls

Function calls do not create new objects. (It may use memory with
stack reallocation, but now that is irrelevant to the GC.)
Roberto Ierusalimschy 2 年之前
父节点
当前提交
152b51955a
共有 5 个文件被更改,包括 10 次插入20 次删除
  1. 3 2
      lapi.c
  2. 4 4
      ldo.c
  3. 0 12
      ldo.h
  4. 2 1
      lgc.c
  5. 1 1
      ltm.c

+ 3 - 2
lapi.c

@@ -1286,13 +1286,14 @@ LUA_API void lua_toclose (lua_State *L, int idx) {
 LUA_API void lua_concat (lua_State *L, int n) {
   lua_lock(L);
   api_checknelems(L, n);
-  if (n > 0)
+  if (n > 0) {
     luaV_concat(L, n);
+    luaC_checkGC(L);
+  }
   else {  /* nothing to concatenate */
     setsvalue2s(L, L->top.p, luaS_newlstr(L, "", 0));  /* push empty string */
     api_incr_top(L);
   }
-  luaC_checkGC(L);
   lua_unlock(L);
 }
 

+ 4 - 4
ldo.c

@@ -416,7 +416,7 @@ static void rethook (lua_State *L, CallInfo *ci, int nres) {
 StkId luaD_tryfuncTM (lua_State *L, StkId func) {
   const TValue *tm;
   StkId p;
-  checkstackGCp(L, 1, func);  /* space for metamethod */
+  checkstackp(L, 1, func);  /* space for metamethod */
   tm = luaT_gettmbyobj(L, s2v(func), TM_CALL);  /* (after previous GC) */
   if (l_unlikely(ttisnil(tm)))
     luaG_callerror(L, s2v(func));  /* nothing to call */
@@ -521,7 +521,7 @@ l_sinline int precallC (lua_State *L, StkId func, int nresults,
                                             lua_CFunction f) {
   int n;  /* number of returns */
   CallInfo *ci;
-  checkstackGCp(L, LUA_MINSTACK, func);  /* ensure minimum stack size */
+  checkstackp(L, LUA_MINSTACK, func);  /* ensure minimum stack size */
   L->ci = ci = prepCallInfo(L, func, nresults, CIST_C,
                                L->top.p + LUA_MINSTACK);
   lua_assert(ci->top.p <= L->stack_last.p);
@@ -557,7 +557,7 @@ int luaD_pretailcall (lua_State *L, CallInfo *ci, StkId func,
       int fsize = p->maxstacksize;  /* frame size */
       int nfixparams = p->numparams;
       int i;
-      checkstackGCp(L, fsize - delta, func);
+      checkstackp(L, fsize - delta, func);
       ci->func.p -= delta;  /* restore 'func' (if vararg) */
       for (i = 0; i < narg1; i++)  /* move down function and arguments */
         setobjs2s(L, ci->func.p + i, func + i);
@@ -604,7 +604,7 @@ CallInfo *luaD_precall (lua_State *L, StkId func, int nresults) {
       int narg = cast_int(L->top.p - func) - 1;  /* number of real arguments */
       int nfixparams = p->numparams;
       int fsize = p->maxstacksize;  /* frame size */
-      checkstackGCp(L, fsize, func);
+      checkstackp(L, fsize, func);
       L->ci = ci = prepCallInfo(L, func, nresults, 0, func + 1 + fsize);
       ci->u.l.savedpc = p->code;  /* starting point */
       for (; narg < nfixparams; narg++)

+ 0 - 12
ldo.h

@@ -44,18 +44,6 @@
     p = restorestack(L, t__))  /* 'pos' part: restore 'p' */
 
 
-/* macro to check stack size and GC, preserving 'p' */
-#define checkstackGCp(L,n,p)  \
-  luaD_checkstackaux(L, n, \
-    ptrdiff_t t__ = savestack(L, p);  /* save 'p' */ \
-    luaC_checkGC(L),  /* stack grow uses memory */ \
-    p = restorestack(L, t__))  /* 'pos' part: restore 'p' */
-
-
-/* macro to check stack size and GC */
-#define checkstackGC(L,fsize)  \
-	luaD_checkstackaux(L, (fsize), luaC_checkGC(L), (void)0)
-
 
 /* type of protected functions, to be ran by 'runprotected' */
 typedef void (*Pfunc) (lua_State *L, void *ud);

+ 2 - 1
lgc.c

@@ -1700,8 +1700,9 @@ static void fullinc (lua_State *L, global_State *g) {
   /* finish any pending sweep phase to start a new cycle */
   luaC_runtilstate(L, bitmask(GCSpause));
   luaC_runtilstate(L, bitmask(GCScallfin));  /* run up to finalizers */
-  luaC_runtilstate(L, bitmask(GCSpause));  /* finish collection */
   /* estimate must be correct after a full GC cycle */
+  lua_assert(g->marked == gettotalobjs(g));
+  luaC_runtilstate(L, bitmask(GCSpause));  /* finish collection */
   setpause(g);
 }
 

+ 1 - 1
ltm.c

@@ -260,7 +260,7 @@ void luaT_getvarargs (lua_State *L, CallInfo *ci, StkId where, int wanted) {
   int nextra = ci->u.l.nextraargs;
   if (wanted < 0) {
     wanted = nextra;  /* get all extra arguments available */
-    checkstackGCp(L, nextra, where);  /* ensure stack space */
+    checkstackp(L, nextra, where);  /* ensure stack space */
     L->top.p = where + nextra;  /* next instruction will need top */
   }
   for (i = 0; i < wanted && i < nextra; i++)