Skip to content

Commit

Permalink
Creates an empty lua table with specified initial capacity as much as…
Browse files Browse the repository at this point in the history
… possible

Currently, we create a Lua table without initial capacity even when the
capacity is known. As a result, we need to resize the Lua tables repeatedly
and it consumes extra cpu resources when we need to transfer
RESP-serialized data to Lua world.

This patch try to remove this extra resize to reduce (re-)allocation overhead.
  • Loading branch information
Masahiro Ide committed Sep 30, 2024
1 parent bb57dfe commit 2d9f55a
Showing 1 changed file with 4 additions and 4 deletions.
8 changes: 4 additions & 4 deletions src/script_lua.c
Original file line number Diff line number Diff line change
Expand Up @@ -361,7 +361,7 @@ static void redisProtocolToLuaType_Map(struct ReplyParser *parser, void *ctx, si
}
lua_newtable(lua);
lua_pushstring(lua, "map");
lua_newtable(lua);
lua_createtable(lua, 0, len);
}
for (size_t j = 0; j < len; j++) {
parseReply(parser, lua);
Expand All @@ -383,7 +383,7 @@ static void redisProtocolToLuaType_Set(struct ReplyParser *parser, void *ctx, si
}
lua_newtable(lua);
lua_pushstring(lua, "set");
lua_newtable(lua);
lua_createtable(lua, 0, len);
}
for (size_t j = 0; j < len; j++) {
parseReply(parser, lua);
Expand Down Expand Up @@ -412,7 +412,7 @@ static void redisProtocolToLuaType_Array(struct ReplyParser *parser, void *ctx,
* to push elements to the stack. On failure, exit with panic. */
serverPanic("lua stack limit reach when parsing server.call reply");
}
lua_newtable(lua);
lua_createtable(lua, len, 0);
}
for (size_t j = 0; j < len; j++) {
if (lua) lua_pushnumber(lua, j + 1);
Expand Down Expand Up @@ -1534,7 +1534,7 @@ void luaRegisterServerAPI(lua_State *lua) {
static void luaCreateArray(lua_State *lua, robj **elev, int elec) {
int j;

lua_newtable(lua);
lua_createtable(lua, elec, 0);
for (j = 0; j < elec; j++) {
lua_pushlstring(lua, (char *)elev[j]->ptr, sdslen(elev[j]->ptr));
lua_rawseti(lua, -2, j + 1);
Expand Down

0 comments on commit 2d9f55a

Please sign in to comment.