From c7bdc0e0e8ab03820b472a87b87c04475def5997 Mon Sep 17 00:00:00 2001 From: Roberto Ierusalimschy Date: Wed, 19 Apr 2017 14:02:50 -0300 Subject: [PATCH] first version of control for the generational collector --- lapi.c | 6 +++++- lgc.c | 18 +++++++++++++----- lstate.h | 4 +++- 3 files changed, 21 insertions(+), 7 deletions(-) diff --git a/lapi.c b/lapi.c index eb67d090..9bd4a55b 100644 --- a/lapi.c +++ b/lapi.c @@ -1,5 +1,5 @@ /* -** $Id: lapi.c,v 2.261 2017/04/06 13:08:56 roberto Exp roberto $ +** $Id: lapi.c,v 2.262 2017/04/11 18:41:09 roberto Exp roberto $ ** Lua API ** See Copyright Notice in lua.h */ @@ -1098,6 +1098,10 @@ LUA_API int lua_gc (lua_State *L, int what, int data) { break; } case LUA_GCGEN: { + lu_byte aux = data & 0xff; + g->genminormul = (aux == 0) ? 20 : aux; + aux = (data >> 8) & 0xff; + g->genmajormul = (aux == 0) ? 100 : aux; luaC_changemode(L, KGC_GEN); break; } diff --git a/lgc.c b/lgc.c index c8d401e6..9995fbed 100644 --- a/lgc.c +++ b/lgc.c @@ -1,5 +1,5 @@ /* -** $Id: lgc.c,v 2.221 2017/04/11 19:00:27 roberto Exp roberto $ +** $Id: lgc.c,v 2.222 2017/04/12 18:01:40 roberto Exp roberto $ ** Garbage Collector ** See Copyright Notice in lua.h */ @@ -1158,6 +1158,7 @@ static void entergen (lua_State *L, global_State *g) { finishgencycle(L, g); g->gckind = KGC_GEN; + g->GCestimate = gettotalbytes(g); /* base for memory control */ } @@ -1205,10 +1206,17 @@ static void fullgen (lua_State *L, global_State *g) { ** collection. (We still has to implement the full control.) */ static void genstep (lua_State *L, global_State *g) { - lu_mem mem; - youngcollection(L, g); - mem = gettotalbytes(g); - luaE_setdebt(g, -((mem / 100) * 20)); + lu_mem majorbase = g->GCestimate; +lua_checkmemory(L); + if (gettotalbytes(g) > (majorbase / 100) * (100 + g->genmajormul)) + fullgen(L, g); + else { + lu_mem mem; + youngcollection(L, g); + mem = gettotalbytes(g); + luaE_setdebt(g, -((mem / 100) * g->genminormul)); + g->GCestimate = mem; + } lua_checkmemory(L); } diff --git a/lstate.h b/lstate.h index dc74370b..d844fc8c 100644 --- a/lstate.h +++ b/lstate.h @@ -1,5 +1,5 @@ /* -** $Id: lstate.h,v 2.136 2017/04/05 16:50:51 roberto Exp roberto $ +** $Id: lstate.h,v 2.137 2017/04/11 18:41:09 roberto Exp roberto $ ** Global State ** See Copyright Notice in lua.h */ @@ -148,6 +148,8 @@ typedef struct global_State { lu_byte currentwhite; lu_byte gcstate; /* state of garbage collector */ lu_byte gckind; /* kind of GC running */ + lu_byte genminormul; /* control for minor generational collections */ + lu_byte genmajormul; /* control for major generational collections */ lu_byte gcrunning; /* true if GC is running */ GCObject *allgc; /* list of all collectable objects */ GCObject **sweepgc; /* current position of sweep in list */