From e7eea585fbd337d7c063c4b12b6957f68a3cf5a0 Mon Sep 17 00:00:00 2001
From: Konstantin Osipov <kostja@tarantool.org>
Date: Fri, 6 Dec 2013 23:47:23 +0400
Subject: [PATCH] gh-26: minor optimization of session destructor

The patch should use struct session, which is non-existent
in 1.5, so at least make session_storage_destroy a bit faster.
---
 src/lua/session.cc | 20 ++++++++++----------
 1 file changed, 10 insertions(+), 10 deletions(-)

diff --git a/src/lua/session.cc b/src/lua/session.cc
index 45a97ba6b7..05a21ce4ba 100644
--- a/src/lua/session.cc
+++ b/src/lua/session.cc
@@ -179,21 +179,21 @@ lbox_session_on_disconnect(struct lua_State *L)
 void
 session_storage_cleanup(int sid)
 {
-	int top = lua_gettop(root_L);
-	lua_pushliteral(root_L, "box");
-	lua_rawget(root_L, LUA_GLOBALSINDEX);
-
-	lua_pushliteral(root_L, "session");
-	lua_rawget(root_L, -2);
+	static int ref = LUA_REFNIL;
 
-	lua_getmetatable(root_L, -1);
+	int top = lua_gettop(root_L);
 
-	lua_pushliteral(root_L, "aggregate_storage");
-	lua_rawget(root_L, -2);
+	if (ref == LUA_REFNIL) {
+		lua_getfield(root_L, LUA_GLOBALSINDEX, "box");
+		lua_getfield(root_L, -1, "session");
+		lua_getmetatable(root_L, -1);
+		lua_getfield(root_L, -1, "aggregate_storage");
+		ref = luaL_ref(root_L, LUA_REGISTRYINDEX);
+	}
+	lua_rawgeti(root_L, LUA_REGISTRYINDEX, ref);
 
 	lua_pushnil(root_L);
 	lua_rawseti(root_L, -2, sid);
-
 	lua_settop(root_L, top);
 }
 
-- 
GitLab