summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorantirez <antirez@gmail.com>2014-05-06 11:13:00 +0200
committerantirez <antirez@gmail.com>2014-05-07 16:12:32 +0200
commit48c49c485155ba9e4a7851fd1644c171674c6f0f (patch)
tree8d49aa0d34f2d3d8cb6a53d24be4049913a6ddca
parent3318b747053bf5009f16c4f10599d4fd9d418b1c (diff)
downloadredis-48c49c485155ba9e4a7851fd1644c171674c6f0f.tar.gz
Scripting: cache argv in luaRedisGenericCommand().
~ 4% consistently measured speed improvement.
-rw-r--r--src/scripting.c19
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