Skip to content

Commit

Permalink
Create empty lua tables with specified initial capacity as much as po…
Browse files Browse the repository at this point in the history
…ssible (valkey-io#1092)

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 when converting RESP serialized object to Lua object and it
consumes extra cpu resources a bit when we need to transfer
RESP-serialized data to Lua world.

This patch try to remove this extra resize to reduce (re-)allocation
overhead.

| name | unstable bb57dfe (rps) | this patch(rps) | improvements |
| --------------- | -------- | --------- | -------------- |
| evalsha - hgetall h1 | 60565.68 | 64487.01 |  6.47% |
| evalsha - hgetall h10 | 47023.41 | 50602.17 | 7.61% |
| evalsha - hgetall h25 | 33572.82 | 37345.48 | 11.23% |
| evalsha - hgetall h50 | 24206.63 | 25276.14 | 4.42% |
| evalsha - hgetall h100 | 15068.87 | 15656.8 | 3.90% |
| evalsha - hgetall h300 | 5948.56 | 6094.74 | 2.46% |

Signed-off-by: Masahiro Ide <masahiro.ide@lycorp.co.jp>
Co-authored-by: Masahiro Ide <masahiro.ide@lycorp.co.jp>
  • Loading branch information
imasahiro and Masahiro Ide authored Oct 1, 2024
1 parent 69eddb4 commit ac569c0
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 ac569c0

Please sign in to comment.