Skip to content

Commit

Permalink
Scripting: cache argv in luaRedisGenericCommand().
Browse files Browse the repository at this point in the history
~ 4% consistently measured speed improvement.
  • Loading branch information
antirez committed May 6, 2014
1 parent a5222d3 commit a285f0f
Showing 1 changed file with 15 additions and 4 deletions.
19 changes: 15 additions & 4 deletions src/scripting.c
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -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),
Expand All @@ -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;
Expand Down Expand Up @@ -339,7 +347,10 @@ int luaRedisGenericCommand(lua_State *lua, int raise_error) {
* 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
Expand Down

0 comments on commit a285f0f

Please sign in to comment.