From 40565b4a089f44fdcb16f4ed0080b0ca3755e4aa Mon Sep 17 00:00:00 2001 From: Roberto Ierusalimschy Date: Tue, 13 Dec 2022 11:55:14 -0300 Subject: Revamp of GC parameters More uniformity when handling GC parameters + avoid divisions by 100 when applying them. --- lgc.h | 44 ++++++++++++++++++++++++++++++++++---------- 1 file changed, 34 insertions(+), 10 deletions(-) (limited to 'lgc.h') diff --git a/lgc.h b/lgc.h index 3c871969..6d82690d 100644 --- a/lgc.h +++ b/lgc.h @@ -8,6 +8,9 @@ #define lgc_h +#include + + #include "lobject.h" #include "lstate.h" @@ -122,20 +125,18 @@ /* Default Values for GC parameters */ -#define LUAI_GENMAJORMUL 100 -#define LUAI_GENMINORMUL 20 + +/* generational */ + +#define LUAI_GENMAJORMUL 100 /* major multiplier */ +#define LUAI_GENMINORMUL 20 /* minor multiplier */ + +/* incremental */ /* wait memory to double before starting new cycle */ #define LUAI_GCPAUSE 200 -/* -** some gc parameters are stored divided by 4 to allow a maximum value -** up to 1023 in a 'lu_byte'. -*/ -#define getgcparam(p) ((p) * 4) -#define setgcparam(p,v) ((p) = (v) / 4) - -#define LUAI_GCMUL 300 +#define LUAI_GCMUL 300 /* step multiplier */ /* how many objects to allocate before next GC step (log2) */ #define LUAI_GCSTEPSIZE 8 /* 256 objects */ @@ -149,6 +150,29 @@ #define GCSTPCLS 4 /* bit true when closing Lua state */ #define gcrunning(g) ((g)->gcstp == 0) +/* +** Macros to set and apply GC parameters. GC parameters are given in +** percentage points, but are stored as lu_byte. To reduce their +** values and avoid repeated divisions by 100, these macros store +** the original parameter multiplied by 2^n and divided by 100. +** To apply them, the value is divided by 2^n (a shift) and then +** multiplied by the stored parameter, yielding +** value / 2^n * (original parameter * 2^n / 100), or approximately +** (value * original parameter / 100). +** +** For most parameters, which are typically larger than 100%, 2^n is +** 16 (2^4), allowing maximum values up to 1599. For the minor +** multiplier, which is typically smaller, 2^n is 64 (2^6) to allow more +** precision. +*/ +#define gcparamshift(p) \ + (offsetof(global_State, p) == offsetof(global_State, genminormul) ? 6 : 4) + +#define setgcparam(g,p,v) \ + (g->p = (cast_uint(v) << gcparamshift(p)) / 100u) +#define applygcparam(g,p,v) (((v) >> gcparamshift(p)) * g->p) + + /* ** Does one step of collection when debt becomes positive. 'pre'/'pos' -- cgit v1.2.3-55-g6feb