From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: from smtpng2.m.smailru.net (smtpng2.m.smailru.net [94.100.179.3]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by dev.tarantool.org (Postfix) with ESMTPS id 4B16C42F4AD for ; Fri, 19 Jun 2020 23:50:05 +0300 (MSK) From: Igor Munkin Date: Fri, 19 Jun 2020 23:40:55 +0300 Message-Id: <7b8e95f3d6da39f0c3c6ca742abccddda817ac5e.1592597647.git.imun@tarantool.org> In-Reply-To: References: MIME-Version: 1.0 Content-Transfer-Encoding: 8bit Subject: [Tarantool-patches] [PATCH 2/2] box: reduce box_process_lua Lua GC memory usage List-Id: Tarantool development patches List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , To: Vladislav Shpilevoy , Sergey Ostanevich Cc: tarantool-patches@dev.tarantool.org function created a new GCfunc object for a handler having no upvalues depending on the request context on each call. The changes introduces the folliwing mapping: | -> Initializing this mapping on Tarantool startup is aimed to reduce Lua GC memory usage. Signed-off-by: Igor Munkin --- src/box/lua/call.c | 32 +++++++++++++++++++++++++++----- 1 file changed, 27 insertions(+), 5 deletions(-) diff --git a/src/box/lua/call.c b/src/box/lua/call.c index 6588ec2fa..e1b1a5e81 100644 --- a/src/box/lua/call.c +++ b/src/box/lua/call.c @@ -48,6 +48,15 @@ #include "mpstream/mpstream.h" #include "box/session.h" +enum handlers { + HANDLER_CALL, + HANDLER_CALL_BY_REF, + HANDLER_EVAL, + HANDLER_MAX, +}; + +static int execute_lua_refs[HANDLER_MAX]; + /** * A helper to find a Lua function by name and put it * on top of the stack. @@ -527,7 +536,7 @@ static const struct port_vtab port_lua_vtab = { }; static inline int -box_process_lua(lua_CFunction handler, struct execute_lua_ctx *ctx, +box_process_lua(enum handlers handler, struct execute_lua_ctx *ctx, struct port *ret) { lua_State *L = luaT_newthread(tarantool_L); @@ -537,7 +546,8 @@ box_process_lua(lua_CFunction handler, struct execute_lua_ctx *ctx, port_lua_create(ret, L); ((struct port_lua *) ret)->ref = coro_ref; - lua_pushcfunction(L, handler); + lua_rawgeti(L, LUA_REGISTRYINDEX, execute_lua_refs[handler]); + assert(lua_isfunction(L, -1)); lua_pushlightuserdata(L, ctx); if (luaT_call(L, 1, LUA_MULTRET) != 0) { port_lua_destroy(ret); @@ -554,7 +564,7 @@ box_lua_call(const char *name, uint32_t name_len, ctx.name = name; ctx.name_len = name_len; ctx.args = args; - return box_process_lua(execute_lua_call, &ctx, ret); + return box_process_lua(HANDLER_CALL, &ctx, ret); } int @@ -565,7 +575,7 @@ box_lua_eval(const char *expr, uint32_t expr_len, ctx.name = expr; ctx.name_len = expr_len; ctx.args = args; - return box_process_lua(execute_lua_eval, &ctx, ret); + return box_process_lua(HANDLER_EVAL, &ctx, ret); } struct func_lua { @@ -781,7 +791,7 @@ func_persistent_lua_call(struct func *base, struct port *args, struct port *ret) struct execute_lua_ctx ctx; ctx.lua_ref = func->lua_ref; ctx.args = args; - return box_process_lua(execute_lua_call_by_ref, &ctx, ret); + return box_process_lua(HANDLER_CALL_BY_REF, &ctx, ret); } @@ -998,6 +1008,18 @@ box_lua_call_init(struct lua_State *L) */ on_alter_func_in_lua.data = L; trigger_add(&on_alter_func, &on_alter_func_in_lua); + + lua_CFunction handles[] = { + [HANDLER_CALL] = execute_lua_call, + [HANDLER_CALL_BY_REF] = execute_lua_call_by_ref, + [HANDLER_EVAL] = execute_lua_eval, + }; + + for (int i = 0; i < HANDLER_MAX; i++) { + lua_pushcfunction(L, handles[i]); + execute_lua_refs[i] = luaL_ref(L, LUA_REGISTRYINDEX); + } + #if 0 /* Get CTypeID for `struct port *' */ int rc = luaL_cdef(L, "struct port;"); -- 2.25.0