Skip to content

Commit 48c49c4

Browse files
committed
Scripting: cache argv in luaRedisGenericCommand().
~ 4% consistently measured speed improvement.
1 parent 3318b74 commit 48c49c4

File tree

1 file changed

+15
-4
lines changed

1 file changed

+15
-4
lines changed

src/scripting.c

Lines changed: 15 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -203,10 +203,13 @@ void luaSortArray(lua_State *lua) {
203203
int luaRedisGenericCommand(lua_State *lua, int raise_error) {
204204
int j, argc = lua_gettop(lua);
205205
struct redisCommand *cmd;
206-
robj **argv;
207206
redisClient *c = server.lua_client;
208207
sds reply;
209208

209+
/* Cached across calls. */
210+
static robj **argv = NULL;
211+
static int argv_size = 0;
212+
210213
/* Require at least one argument */
211214
if (argc == 0) {
212215
luaPushError(lua,
@@ -215,7 +218,13 @@ int luaRedisGenericCommand(lua_State *lua, int raise_error) {
215218
}
216219

217220
/* Build the arguments vector */
218-
argv = zmalloc(sizeof(robj*)*argc);
221+
if (!argv) {
222+
argv = zmalloc(sizeof(robj*)*argc);
223+
} else if (argv_size < argc) {
224+
argv = zrealloc(argv,sizeof(robj*)*argc);
225+
argv_size = argc;
226+
}
227+
219228
for (j = 0; j < argc; j++) {
220229
if (!lua_isstring(lua,j+1)) break;
221230
argv[j] = createStringObject((char*)lua_tostring(lua,j+1),
@@ -231,7 +240,6 @@ int luaRedisGenericCommand(lua_State *lua, int raise_error) {
231240
decrRefCount(argv[j]);
232241
j--;
233242
}
234-
zfree(argv);
235243
luaPushError(lua,
236244
"Lua redis() command arguments must be strings or integers");
237245
return 1;
@@ -339,7 +347,10 @@ int luaRedisGenericCommand(lua_State *lua, int raise_error) {
339347
* argv/argc of the client instead of the local variables. */
340348
for (j = 0; j < c->argc; j++)
341349
decrRefCount(c->argv[j]);
342-
zfree(c->argv);
350+
if (c->argv != argv) {
351+
zfree(c->argv);
352+
argv = NULL;
353+
}
343354

344355
if (raise_error) {
345356
/* If we are here we should have an error in the stack, in the

0 commit comments

Comments
 (0)