diff --git a/src/scripting.c b/src/scripting.c index b893a4a1..7537f37d 100644 --- a/src/scripting.c +++ b/src/scripting.c @@ -943,7 +943,23 @@ void evalGenericCommand(redisClient *c, int evalsha) { } server.lua_caller = NULL; selectDb(c,server.lua_client->db->id); /* set DB ID from Lua client */ - lua_gc(lua,LUA_GCSTEP,1); + + /* Call the Lua garbage collector from time to time to avoid a + * full cycle performed by Lua, which adds too latency. + * + * The call is performed every LUA_GC_CYCLE_PERIOD executed commands + * (and for LUA_GC_CYCLE_PERIOD collection steps) because calling it + * for every command uses too much CPU. */ + #define LUA_GC_CYCLE_PERIOD 50 + { + static long gc_count = 0; + + gc_count++; + if (gc_count == LUA_GC_CYCLE_PERIOD) { + lua_gc(lua,LUA_GCSTEP,LUA_GC_CYCLE_PERIOD); + gc_count = 0; + } + } if (err) { addReplyErrorFormat(c,"Error running script (call to %s): %s\n",