浏览代码

some cleaning in GC parameters

Roberto Ierusalimschy 7 年之前
父节点
当前提交
1d8920dd7f
共有 4 个文件被更改,包括 46 次插入31 次删除
  1. 15 10
      lapi.c
  2. 12 14
      lgc.c
  3. 14 4
      lgc.h
  4. 5 3
      lstate.c

+ 15 - 10
lapi.c

@@ -1,5 +1,5 @@
 /*
 /*
-** $Id: lapi.c,v 2.269 2017/06/01 20:22:33 roberto Exp roberto $
+** $Id: lapi.c,v 2.270 2017/06/29 15:06:44 roberto Exp roberto $
 ** Lua API
 ** Lua API
 ** See Copyright Notice in lua.h
 ** See Copyright Notice in lua.h
 */
 */
@@ -1114,14 +1114,14 @@ LUA_API int lua_gc (lua_State *L, int what, ...) {
     }
     }
     case LUA_GCSETPAUSE: {
     case LUA_GCSETPAUSE: {
       int data = va_arg(argp, int);
       int data = va_arg(argp, int);
-      res = g->gcpause + 100;
-      g->gcpause = (data >= 100) ? data - 100 : 0;
+      res = getgcparam(g->gcpause);
+      setgcparam(g->gcpause, data);
       break;
       break;
     }
     }
     case LUA_GCSETSTEPMUL: {
     case LUA_GCSETSTEPMUL: {
       int data = va_arg(argp, int);
       int data = va_arg(argp, int);
-      res = g->gcstepmul * 10;
-      g->gcstepmul = data / 10;
+      res = getgcparam(g->gcstepmul);
+      setgcparam(g->gcstepmul, data);
       break;
       break;
     }
     }
     case LUA_GCISRUNNING: {
     case LUA_GCISRUNNING: {
@@ -1131,8 +1131,10 @@ LUA_API int lua_gc (lua_State *L, int what, ...) {
     case LUA_GCGEN: {
     case LUA_GCGEN: {
       int minormul = va_arg(argp, int);
       int minormul = va_arg(argp, int);
       int majormul = va_arg(argp, int);
       int majormul = va_arg(argp, int);
-      g->genminormul = (minormul == 0) ? LUAI_GENMINORMUL : minormul;
-      g->genmajormul = (majormul == 0) ? LUAI_GENMAJORMUL : majormul;
+      if (minormul != 0)
+        g->genminormul = minormul;
+      if (majormul != 0)
+        setgcparam(g->genmajormul, majormul);
       luaC_changemode(L, KGC_GEN);
       luaC_changemode(L, KGC_GEN);
       break;
       break;
     }
     }
@@ -1140,9 +1142,12 @@ LUA_API int lua_gc (lua_State *L, int what, ...) {
       int pause = va_arg(argp, int);
       int pause = va_arg(argp, int);
       int stepmul = va_arg(argp, int);
       int stepmul = va_arg(argp, int);
       int stepsize = va_arg(argp, int);
       int stepsize = va_arg(argp, int);
-      g->gcpause = (pause == 0) ? LUAI_GCPAUSE : pause;
-      g->gcstepmul = (stepmul == 0) ? LUAI_GCMUL : stepmul;
-      g->gcstepsize = (stepsize == 0) ? LUAI_GCSTEPSIZE : stepsize;
+      if (pause != 0)
+        setgcparam(g->gcpause, pause);
+      if (stepmul != 0)
+        setgcparam(g->gcstepmul, stepmul);
+      if (stepsize != 0)
+        g->gcstepsize = stepsize;
       luaC_changemode(L, KGC_INC);
       luaC_changemode(L, KGC_INC);
       break;
       break;
     }
     }

+ 12 - 14
lgc.c

@@ -1,5 +1,5 @@
 /*
 /*
-** $Id: lgc.c,v 2.233 2017/06/29 15:06:44 roberto Exp roberto $
+** $Id: lgc.c,v 2.234 2017/08/31 16:06:51 roberto Exp roberto $
 ** Garbage Collector
 ** Garbage Collector
 ** See Copyright Notice in lua.h
 ** See Copyright Notice in lua.h
 */
 */
@@ -47,9 +47,9 @@
 
 
 /*
 /*
 ** The equivalent, in bytes, of one unit of "work" (visiting a slot,
 ** The equivalent, in bytes, of one unit of "work" (visiting a slot,
-** sweeping an object, etc.) * 10 (for scaling)
+** sweeping an object, etc.)
 */
 */
-#define WORK2MEM	(sizeof(TValue) * 10)
+#define WORK2MEM	sizeof(TValue)
 
 
 
 
 /*
 /*
@@ -1256,14 +1256,14 @@ static void fullgen (lua_State *L, global_State *g) {
 ** than last major collection (kept in 'g->GCestimate'), does a major
 ** than last major collection (kept in 'g->GCestimate'), does a major
 ** collection. Otherwise, does a minor collection and set debt to make
 ** collection. Otherwise, does a minor collection and set debt to make
 ** another collection when memory grows 'genminormul'% larger.
 ** another collection when memory grows 'genminormul'% larger.
-** 'GCdebt <= 0' means an explicity call to GC step with "size" zero;
+** 'GCdebt <= 0' means an explicit call to GC step with "size" zero;
 ** in that case, always do a minor collection.
 ** in that case, always do a minor collection.
 */
 */
 static void genstep (lua_State *L, global_State *g) {
 static void genstep (lua_State *L, global_State *g) {
   lu_mem majorbase = g->GCestimate;
   lu_mem majorbase = g->GCestimate;
-//lua_checkmemory(L);
+  int majormul = getgcparam(g->genmajormul);
   if (g->GCdebt > 0 &&
   if (g->GCdebt > 0 &&
-      gettotalbytes(g) > (majorbase / 100) * (100 + g->genmajormul)) {
+      gettotalbytes(g) > (majorbase / 100) * (100 + majormul)) {
     fullgen(L, g);
     fullgen(L, g);
   }
   }
   else {
   else {
@@ -1273,7 +1273,6 @@ static void genstep (lua_State *L, global_State *g) {
     luaE_setdebt(g, -((mem / 100) * g->genminormul));
     luaE_setdebt(g, -((mem / 100) * g->genminormul));
     g->GCestimate = majorbase;  /* preserve base value */
     g->GCestimate = majorbase;  /* preserve base value */
   }
   }
-//lua_checkmemory(L);
 }
 }
 
 
 /* }====================================================== */
 /* }====================================================== */
@@ -1288,13 +1287,13 @@ static void genstep (lua_State *L, global_State *g) {
 
 
 /*
 /*
 ** Set the "time" to wait before starting a new GC cycle; cycle will
 ** Set the "time" to wait before starting a new GC cycle; cycle will
-** start when memory use hits the threshold of ('estimate' * gcpause /
+** start when memory use hits the threshold of ('estimate' * pause /
 ** PAUSEADJ). (Division by 'estimate' should be OK: it cannot be zero,
 ** PAUSEADJ). (Division by 'estimate' should be OK: it cannot be zero,
 ** because Lua cannot even start with less than PAUSEADJ bytes).
 ** because Lua cannot even start with less than PAUSEADJ bytes).
 */
 */
 static void setpause (global_State *g) {
 static void setpause (global_State *g) {
   l_mem threshold, debt;
   l_mem threshold, debt;
-  int pause = g->gcpause + 100;
+  int pause = getgcparam(g->gcpause);
   l_mem estimate = g->GCestimate / PAUSEADJ;  /* adjust 'estimate' */
   l_mem estimate = g->GCestimate / PAUSEADJ;  /* adjust 'estimate' */
   lua_assert(estimate > 0);
   lua_assert(estimate > 0);
   threshold = (pause < MAX_LMEM / estimate)  /* overflow? */
   threshold = (pause < MAX_LMEM / estimate)  /* overflow? */
@@ -1482,16 +1481,15 @@ void luaC_runtilstate (lua_State *L, int statesmask) {
 ** controls when next step will be performed.
 ** controls when next step will be performed.
 */
 */
 static void incstep (lua_State *L, global_State *g) {
 static void incstep (lua_State *L, global_State *g) {
-  int stepmul = (g->gcstepmul | 1);  /* avoid division by 0 */
+  int stepmul = (getgcparam(g->gcstepmul) | 1);  /* avoid division by 0 */
   l_mem debt = (g->GCdebt / WORK2MEM) * stepmul;
   l_mem debt = (g->GCdebt / WORK2MEM) * stepmul;
   l_mem stepsize = (g->gcstepsize <= log2maxs(l_mem))
   l_mem stepsize = (g->gcstepsize <= log2maxs(l_mem))
-                   ? cast(l_mem, 1) << g->gcstepsize
-                   : MAX_LMEM;
-  stepsize = -((stepsize / WORK2MEM) * stepmul);
+                 ? ((cast(l_mem, 1) << g->gcstepsize) / WORK2MEM) * stepmul
+                 : MAX_LMEM;  /* overflow; keep maximum value */
   do {  /* repeat until pause or enough "credit" (negative debt) */
   do {  /* repeat until pause or enough "credit" (negative debt) */
     lu_mem work = singlestep(L);  /* perform one single step */
     lu_mem work = singlestep(L);  /* perform one single step */
     debt -= work;
     debt -= work;
-  } while (debt > stepsize && g->gcstate != GCSpause);
+  } while (debt > -stepsize && g->gcstate != GCSpause);
   if (g->gcstate == GCSpause)
   if (g->gcstate == GCSpause)
     setpause(g);  /* pause until next cycle */
     setpause(g);  /* pause until next cycle */
   else {
   else {

+ 14 - 4
lgc.h

@@ -1,5 +1,5 @@
 /*
 /*
-** $Id: lgc.h,v 2.97 2017/05/04 13:32:01 roberto Exp roberto $
+** $Id: lgc.h,v 2.98 2017/05/26 19:14:29 roberto Exp roberto $
 ** Garbage Collector
 ** Garbage Collector
 ** See Copyright Notice in lua.h
 ** See Copyright Notice in lua.h
 */
 */
@@ -119,10 +119,20 @@
 
 
 /* Default Values for GC parameters */
 /* Default Values for GC parameters */
 #define LUAI_GENMAJORMUL         100
 #define LUAI_GENMAJORMUL         100
-#define LUAI_GENMINORMUL         5
+#define LUAI_GENMINORMUL         12
+
+/* wait memory to double before starting new cycle */
+#define LUAI_GCPAUSE    200     /* 200% */
+
+/*
+** gc parameters are stored divided by 4 to allow a maximum value larger
+** than 1000 in an 'lu_byte'.
+*/
+#define getgcparam(p)	((p) * 4)
+#define setgcparam(p,v)	((p) = (v) / 4)
+
+#define LUAI_GCMUL      100
 
 
-#define LUAI_GCPAUSE    100     /* 100% */
-#define LUAI_GCMUL      10
 /* how much to allocate before next GC step (log2) */
 /* how much to allocate before next GC step (log2) */
 #define LUAI_GCSTEPSIZE 13      /* 8 KB */
 #define LUAI_GCSTEPSIZE 13      /* 8 KB */
 
 

+ 5 - 3
lstate.c

@@ -1,5 +1,5 @@
 /*
 /*
-** $Id: lstate.c,v 2.140 2017/05/26 19:14:29 roberto Exp roberto $
+** $Id: lstate.c,v 2.141 2017/06/29 15:06:44 roberto Exp roberto $
 ** Global State
 ** Global State
 ** See Copyright Notice in lua.h
 ** See Copyright Notice in lua.h
 */
 */
@@ -320,9 +320,11 @@ LUA_API lua_State *lua_newstate (lua_Alloc f, void *ud) {
   g->twups = NULL;
   g->twups = NULL;
   g->totalbytes = sizeof(LG);
   g->totalbytes = sizeof(LG);
   g->GCdebt = 0;
   g->GCdebt = 0;
-  g->gcpause = LUAI_GCPAUSE;
-  g->gcstepmul = LUAI_GCMUL;
+  setgcparam(g->gcpause, LUAI_GCPAUSE);
+  setgcparam(g->gcstepmul, LUAI_GCMUL);
   g->gcstepsize = LUAI_GCSTEPSIZE;
   g->gcstepsize = LUAI_GCSTEPSIZE;
+  setgcparam(g->genmajormul, LUAI_GENMAJORMUL);
+  g->genminormul = LUAI_GENMINORMUL;
   for (i=0; i < LUA_NUMTAGS; i++) g->mt[i] = NULL;
   for (i=0; i < LUA_NUMTAGS; i++) g->mt[i] = NULL;
   if (luaD_rawrunprotected(L, f_luaopen, NULL) != LUA_OK) {
   if (luaD_rawrunprotected(L, f_luaopen, NULL) != LUA_OK) {
     /* memory allocation error: free partial state */
     /* memory allocation error: free partial state */