From 48c49c485155ba9e4a7851fd1644c171674c6f0f Mon Sep 17 00:00:00 2001 From: antirez Date: Tue, 6 May 2014 11:13:00 +0200 Subject: [PATCH] Scripting: cache argv in luaRedisGenericCommand(). ~ 4% consistently measured speed improvement. --- src/scripting.c | 19 +++++++++++++++---- 1 file changed, 15 insertions(+), 4 deletions(-) diff --git a/src/scripting.c b/src/scripting.c index c0dcbeac6..b893a4a12 100644 --- a/src/scripting.c +++ b/src/scripting.c @@ -203,10 +203,13 @@ void luaSortArray(lua_State *lua) { int luaRedisGenericCommand(lua_State *lua, int raise_error) { int j, argc = lua_gettop(lua); struct redisCommand *cmd; - robj **argv; redisClient *c = server.lua_client; sds reply; + /* Cached across calls. */ + static robj **argv = NULL; + static int argv_size = 0; + /* Require at least one argument */ if (argc == 0) { luaPushError(lua, @@ -215,7 +218,13 @@ int luaRedisGenericCommand(lua_State *lua, int raise_error) { } /* Build the arguments vector */ - argv = zmalloc(sizeof(robj*)*argc); + if (!argv) { + argv = zmalloc(sizeof(robj*)*argc); + } else if (argv_size < argc) { + argv = zrealloc(argv,sizeof(robj*)*argc); + argv_size = argc; + } + for (j = 0; j < argc; j++) { if (!lua_isstring(lua,j+1)) break; argv[j] = createStringObject((char*)lua_tostring(lua,j+1), @@ -231,7 +240,6 @@ int luaRedisGenericCommand(lua_State *lua, int raise_error) { decrRefCount(argv[j]); j--; } - zfree(argv); luaPushError(lua, "Lua redis() command arguments must be strings or integers"); return 1; @@ -339,7 +347,10 @@ cleanup: * argv/argc of the client instead of the local variables. */ for (j = 0; j < c->argc; j++) decrRefCount(c->argv[j]); - zfree(c->argv); + if (c->argv != argv) { + zfree(c->argv); + argv = NULL; + } if (raise_error) { /* If we are here we should have an error in the stack, in the