From 1d8920dd7f508af5f2fd743678be1327f30c079b Mon Sep 17 00:00:00 2001 From: Roberto Ierusalimschy Date: Wed, 11 Oct 2017 09:38:45 -0300 Subject: some cleaning in GC parameters --- lapi.c | 25 +++++++++++++++---------- lgc.c | 26 ++++++++++++-------------- lgc.h | 18 ++++++++++++++---- lstate.c | 8 +++++--- 4 files changed, 46 insertions(+), 31 deletions(-) diff --git a/lapi.c b/lapi.c index 4985da6b..6e9b972d 100644 --- a/lapi.c +++ b/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 ** See Copyright Notice in lua.h */ @@ -1114,14 +1114,14 @@ LUA_API int lua_gc (lua_State *L, int what, ...) { } case LUA_GCSETPAUSE: { 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; } case LUA_GCSETSTEPMUL: { int data = va_arg(argp, int); - res = g->gcstepmul * 10; - g->gcstepmul = data / 10; + res = getgcparam(g->gcstepmul); + setgcparam(g->gcstepmul, data); break; } case LUA_GCISRUNNING: { @@ -1131,8 +1131,10 @@ LUA_API int lua_gc (lua_State *L, int what, ...) { case LUA_GCGEN: { int minormul = 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); break; } @@ -1140,9 +1142,12 @@ LUA_API int lua_gc (lua_State *L, int what, ...) { int pause = va_arg(argp, int); int stepmul = 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); break; } diff --git a/lgc.c b/lgc.c index e658a6f8..3e67d6d7 100644 --- a/lgc.c +++ b/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 ** See Copyright Notice in lua.h */ @@ -47,9 +47,9 @@ /* ** 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 ** collection. Otherwise, does a minor collection and set debt to make ** 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. */ static void genstep (lua_State *L, global_State *g) { lu_mem majorbase = g->GCestimate; -//lua_checkmemory(L); + int majormul = getgcparam(g->genmajormul); if (g->GCdebt > 0 && - gettotalbytes(g) > (majorbase / 100) * (100 + g->genmajormul)) { + gettotalbytes(g) > (majorbase / 100) * (100 + majormul)) { fullgen(L, g); } else { @@ -1273,7 +1273,6 @@ static void genstep (lua_State *L, global_State *g) { luaE_setdebt(g, -((mem / 100) * g->genminormul)); 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 -** 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, ** because Lua cannot even start with less than PAUSEADJ bytes). */ static void setpause (global_State *g) { l_mem threshold, debt; - int pause = g->gcpause + 100; + int pause = getgcparam(g->gcpause); l_mem estimate = g->GCestimate / PAUSEADJ; /* adjust 'estimate' */ lua_assert(estimate > 0); 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. */ 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 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) */ lu_mem work = singlestep(L); /* perform one single step */ debt -= work; - } while (debt > stepsize && g->gcstate != GCSpause); + } while (debt > -stepsize && g->gcstate != GCSpause); if (g->gcstate == GCSpause) setpause(g); /* pause until next cycle */ else { diff --git a/lgc.h b/lgc.h index 36f361ec..5113e922 100644 --- a/lgc.h +++ b/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 ** See Copyright Notice in lua.h */ @@ -119,10 +119,20 @@ /* Default Values for GC parameters */ #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) */ #define LUAI_GCSTEPSIZE 13 /* 8 KB */ diff --git a/lstate.c b/lstate.c index b2c3e0c4..626377ae 100644 --- a/lstate.c +++ b/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 ** 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->totalbytes = sizeof(LG); 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; + setgcparam(g->genmajormul, LUAI_GENMAJORMUL); + g->genminormul = LUAI_GENMINORMUL; for (i=0; i < LUA_NUMTAGS; i++) g->mt[i] = NULL; if (luaD_rawrunprotected(L, f_luaopen, NULL) != LUA_OK) { /* memory allocation error: free partial state */ -- cgit v1.2.3-55-g6feb