commit fd67e4dee61e2e538188759819bb01586f5c93f8 from: Igor Munkin date: Tue Jun 27 17:54:23 2023 UTC box: reduce luaT_pushtuple Lua GC memory usage function created a new GCfunc object for a tuple __gc handler (i.e. ), having no upvalues, on each call when tuple object for Lua world is created. The change introduces the reference to the single GCfunc object created on Tarantool startup and stored to Lua registry. Using this approach in scope of is aimed to reduce Lua GC memory usage. Part of #5201 NO_DOC=refactoring NO_TEST=refactoring NO_CHANGELOG=refactoring Reviewed-by: Vladimir Davydov Reviewed-by: Sergey Kaplun Signed-off-by: Igor Munkin (cherry picked from commit 8d7d14609853ac7020f15d5239ee05c65817ad29) commit - 0daf852a1b7a275ccb7c475fab4dc91aff6ec635 commit + fd67e4dee61e2e538188759819bb01586f5c93f8 blob - 387d5caedfc8d7c79bad17264e2260b4a3070e23 blob + bce56c960bffb184540674604234bfa71dcfad8d --- src/box/lua/tuple.c +++ src/box/lua/tuple.c @@ -80,6 +80,17 @@ uint32_t CTID_STRUCT_TUPLE_REF; * () or () on each invocation. */ static int luaT_tuple_encode_table_ref = LUA_NOREF; +/** + * reference in the Lua registry. + * + * There is no need to create a new GCfunc object each time + * is called, since there are no upvalues passed + * to (i.e. macro to ). + * Hence, to reduce Lua GC pressure when tuple object is created + * for Lua world, the common tuple __gc finalizer can be easily + * obtained from the Lua registry via this reference. + */ +static int luaT_tuple_gc_ref = LUA_NOREF; box_tuple_t * luaT_checktuple(struct lua_State *L, int idx) @@ -690,7 +701,8 @@ luaT_pushtuple(struct lua_State *L, box_tuple_t *tuple *ptr = tuple; /* The order is important - first reference tuple, next set gc */ box_tuple_ref(tuple); - lua_pushcfunction(L, lbox_tuple_gc); + assert(luaT_tuple_gc_ref != LUA_NOREF); + lua_rawgeti(L, LUA_REGISTRYINDEX, luaT_tuple_gc_ref); luaL_setcdatagc(L, -2); } @@ -760,4 +772,7 @@ box_lua_tuple_init(struct lua_State *L) lua_pushcfunction(L, luaT_tuple_encode_table); luaT_tuple_encode_table_ref = luaL_ref(L, LUA_REGISTRYINDEX); + + lua_pushcfunction(L, lbox_tuple_gc); + luaT_tuple_gc_ref = luaL_ref(L, LUA_REGISTRYINDEX); }