Skip to content

Commit 76fda9f

Browse files
committed
Scripting: don't call lua_gc() after Lua script run.
Calling lua_gc() after every script execution is too expensive, and apparently does not make the execution smoother: the same peak latency was measured before and after the commit. This change accounts for scripts execution speedup in the order of 10%.
1 parent 48c49c4 commit 76fda9f

File tree

1 file changed

+17
-1
lines changed

1 file changed

+17
-1
lines changed

src/scripting.c

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -943,7 +943,23 @@ void evalGenericCommand(redisClient *c, int evalsha) {
943943
}
944944
server.lua_caller = NULL;
945945
selectDb(c,server.lua_client->db->id); /* set DB ID from Lua client */
946-
lua_gc(lua,LUA_GCSTEP,1);
946+
947+
/* Call the Lua garbage collector from time to time to avoid a
948+
* full cycle performed by Lua, which adds too latency.
949+
*
950+
* The call is performed every LUA_GC_CYCLE_PERIOD executed commands
951+
* (and for LUA_GC_CYCLE_PERIOD collection steps) because calling it
952+
* for every command uses too much CPU. */
953+
#define LUA_GC_CYCLE_PERIOD 50
954+
{
955+
static long gc_count = 0;
956+
957+
gc_count++;
958+
if (gc_count == LUA_GC_CYCLE_PERIOD) {
959+
lua_gc(lua,LUA_GCSTEP,LUA_GC_CYCLE_PERIOD);
960+
gc_count = 0;
961+
}
962+
}
947963

948964
if (err) {
949965
addReplyErrorFormat(c,"Error running script (call to %s): %s\n",

0 commit comments

Comments
 (0)