diff options
author | antirez <antirez@gmail.com> | 2014-05-06 11:13:00 +0200 |
---|---|---|
committer | antirez <antirez@gmail.com> | 2014-05-07 16:12:32 +0200 |
commit | 48c49c485155ba9e4a7851fd1644c171674c6f0f (patch) | |
tree | 8d49aa0d34f2d3d8cb6a53d24be4049913a6ddca /src/scripting.c | |
parent | 3318b747053bf5009f16c4f10599d4fd9d418b1c (diff) | |
download | redis-48c49c485155ba9e4a7851fd1644c171674c6f0f.tar.gz |
Scripting: cache argv in luaRedisGenericCommand().
~ 4% consistently measured speed improvement.
Diffstat (limited to 'src/scripting.c')
-rw-r--r-- | src/scripting.c | 19 |
1 files 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 |