From 7827c40c49d841daca2a40463b8a60f9a113f77e Mon Sep 17 00:00:00 2001 From: Roberto Ierusalimschy Date: Wed, 10 Jan 2024 14:45:58 -0300 Subject: A few more tweaks in the garbage collector --- lapi.c | 14 ++++++++++++++ 1 file changed, 14 insertions(+) (limited to 'lapi.c') diff --git a/lapi.c b/lapi.c index 9ff1b851..d18445e0 100644 --- a/lapi.c +++ b/lapi.c @@ -53,6 +53,16 @@ const char lua_ident[] = #define isupvalue(i) ((i) < LUA_REGISTRYINDEX) +/* Advance the garbage collector when creating large objects */ +static void advancegc (lua_State *L, size_t delta) { + delta >>= 5; /* one object for each 32 bytes (empirical) */ + if (delta > 0) { + global_State *g = G(L); + luaE_setdebt(g, g->GCdebt - delta); + } +} + + /* ** Convert an acceptable index to a pointer to its respective value. ** Non-valid indices return the special nil value 'G(L)->nilvalue'. @@ -530,6 +540,7 @@ LUA_API const char *lua_pushlstring (lua_State *L, const char *s, size_t len) { ts = (len == 0) ? luaS_new(L, "") : luaS_newlstr(L, s, len); setsvalue2s(L, L->top.p, ts); api_incr_top(L); + advancegc(L, len); luaC_checkGC(L); lua_unlock(L); return getstr(ts); @@ -544,6 +555,8 @@ LUA_API const char *lua_pushextlstring (lua_State *L, ts = luaS_newextlstr (L, s, len, falloc, ud); setsvalue2s(L, L->top.p, ts); api_incr_top(L); + if (falloc != NULL) /* non-static string? */ + advancegc(L, len); /* count its memory */ luaC_checkGC(L); lua_unlock(L); return getstr(ts); @@ -1336,6 +1349,7 @@ LUA_API void *lua_newuserdatauv (lua_State *L, size_t size, int nuvalue) { u = luaS_newudata(L, size, nuvalue); setuvalue(L, s2v(L->top.p), u); api_incr_top(L); + advancegc(L, size); luaC_checkGC(L); lua_unlock(L); return getudatamem(u); -- cgit v1.2.3-55-g6feb