more efficient way to apply 'stepmul' + some changes in GC parameters

This commit is contained in:
Roberto Ierusalimschy
2012-05-28 17:41:00 -03:00
parent 5adb5a4812
commit 51e8f08e60

47
lgc.c
View File

@@ -1,5 +1,5 @@
/* /*
** $Id: lgc.c,v 2.127 2012/05/22 18:38:56 roberto Exp roberto $ ** $Id: lgc.c,v 2.128 2012/05/23 15:43:14 roberto Exp roberto $
** Garbage Collector ** Garbage Collector
** See Copyright Notice in lua.h ** See Copyright Notice in lua.h
*/ */
@@ -24,8 +24,11 @@
/* cost of sweeping one element (half the size of a small object) */ /*
#define GCSWEEPCOST ((sizeof(TString) + 4) / 2) ** cost of sweeping one element (the size of a small object divided
** by some adjust for the sweep speed)
*/
#define GCSWEEPCOST ((sizeof(TString) + 4) / 4)
/* maximum number of elements to sweep in each single step */ /* maximum number of elements to sweep in each single step */
#define GCSWEEPMAX (cast_int((GCSTEPSIZE / GCSWEEPCOST) / 4)) #define GCSWEEPMAX (cast_int((GCSTEPSIZE / GCSWEEPCOST) / 4))
@@ -33,25 +36,27 @@
/* maximum number of finalizers to call in each GC step */ /* maximum number of finalizers to call in each GC step */
#define GCFINALIZENUM 4 #define GCFINALIZENUM 4
/* (arbitrary) cost of atomic step */
#define GCATOMICCOST GCSTEPSIZE
/* /*
** macro to apply the "speed" of the garbage collector: the constant ** macro to adjust 'stepmul': 'stepmul' is actually used like
** 80 makes the standard 'stepmul' of 200 results in the GC handling ** 'stepmul / STEPMULADJ' (value chosen by tests)
** 80/200 = 1/2.5 = 0.4Kbytes for every 1Kb allocated.
** (The computation tries to avoid overflows or underflows.)
*/ */
#define workrate(x,mul) \ #define STEPMULADJ 200
((x) < MAX_INT/80 ? ((x) * 80) / mul : ((x) / mul) * 80)
/*
** macro to adjust 'pause': 'pause' is actually used like
** 'pause / PAUSEADJ' (value chosen by tests)
*/
#define PAUSEADJ 200
/* /*
** standard negative debt for GC; a reasonable "time" to wait before ** standard negative debt for GC; a reasonable "time" to wait before
** starting a new cycle ** starting a new cycle
*/ */
#define stddebtest(g,e) (-cast(l_mem, (e)/100) * g->gcpause) #define stddebtest(g,e) (-cast(l_mem, (e)/PAUSEADJ) * g->gcpause)
#define stddebt(g) stddebtest(g, gettotalbytes(g)) #define stddebt(g) stddebtest(g, gettotalbytes(g))
@@ -955,8 +960,9 @@ void luaC_freeallobjects (lua_State *L) {
} }
static void atomic (lua_State *L) { static l_mem atomic (lua_State *L) {
global_State *g = G(L); global_State *g = G(L);
l_mem trav = g->GCmemtrav;
GCObject *origweak, *origall; GCObject *origweak, *origall;
lua_assert(!iswhite(obj2gco(g->mainthread))); lua_assert(!iswhite(obj2gco(g->mainthread)));
markobject(g, L); /* mark running thread */ markobject(g, L); /* mark running thread */
@@ -976,6 +982,7 @@ static void atomic (lua_State *L) {
separatetobefnz(L, 0); /* separate objects to be finalized */ separatetobefnz(L, 0); /* separate objects to be finalized */
markbeingfnz(g); /* mark userdata that will be finalized */ markbeingfnz(g); /* mark userdata that will be finalized */
propagateall(g); /* remark, to propagate `preserveness' */ propagateall(g); /* remark, to propagate `preserveness' */
trav = g->GCmemtrav - trav; /* avoid adding convergence twice */
convergeephemerons(g); convergeephemerons(g);
/* at this point, all resurrected objects are marked. */ /* at this point, all resurrected objects are marked. */
/* remove dead objects from weak tables */ /* remove dead objects from weak tables */
@@ -987,6 +994,7 @@ static void atomic (lua_State *L) {
g->currentwhite = cast_byte(otherwhite(g)); /* flip current white */ g->currentwhite = cast_byte(otherwhite(g)); /* flip current white */
entersweep(L); /* prepare to sweep strings */ entersweep(L); /* prepare to sweep strings */
/*lua_checkmemory(L);*/ /*lua_checkmemory(L);*/
return trav; /* reasonable estimate of the work done by 'atomic' */
} }
@@ -1012,8 +1020,7 @@ static lu_mem singlestep (lua_State *L) {
else { /* no more `gray' objects */ else { /* no more `gray' objects */
g->gcstate = GCSatomic; /* finish mark phase */ g->gcstate = GCSatomic; /* finish mark phase */
g->GCestimate = g->GCmemtrav; /* save what was counted */ g->GCestimate = g->GCmemtrav; /* save what was counted */
atomic(L); return atomic(L);
return GCATOMICCOST;
} }
} }
case GCSsweepstring: { case GCSsweepstring: {
@@ -1086,14 +1093,18 @@ static void step (lua_State *L) {
global_State *g = G(L); global_State *g = G(L);
l_mem debt = g->GCdebt; l_mem debt = g->GCdebt;
int stepmul = g->gcstepmul; int stepmul = g->gcstepmul;
if (stepmul <= 0) stepmul = 1; if (stepmul < 40) stepmul = 40; /* avoid ridiculous low values */
/* convert debt from Kb to 'work units' (avoid zero debt and overflows) */
debt = (debt / STEPMULADJ) + 1;
debt = (debt < MAX_LMEM / stepmul) ? debt * stepmul : MAX_LMEM;
do { /* always perform at least one single step */ do { /* always perform at least one single step */
lu_mem work = singlestep(L); /* do some work */ lu_mem work = singlestep(L); /* do some work */
work = workrate(work, stepmul); /* apply work rate */
debt -= work; debt -= work;
} while (debt > -GCSTEPSIZE && g->gcstate != GCSpause); } while (debt > -GCSTEPSIZE && g->gcstate != GCSpause);
if (g->gcstate == GCSpause) if (g->gcstate == GCSpause)
debt = stddebtest(g, g->GCestimate); /* pause until next cycle */ debt = stddebtest(g, g->GCestimate); /* pause until next cycle */
else
debt = (debt / stepmul) * STEPMULADJ; /* convert 'work units' to Kb */
luaE_setdebt(g, debt); luaE_setdebt(g, debt);
} }