Create empty lua tables with specified initial capacity as much as possible (#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 bb57dfe6303 (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>
This commit is contained in:
Masahiro Ide 2024-10-01 12:59:22 +09:00 committed by GitHub
parent 69eddb4874
commit ac569c09f8
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194

View File

@ -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);
@ -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);
@ -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);
@ -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);