From b29d54d0c2fa9c9faee08915342688916e4125a8 Mon Sep 17 00:00:00 2001
From: Konstantin Osipov <kostja@tarantool.org>
Date: Wed, 29 Jul 2015 16:29:29 +0300
Subject: [PATCH] log: fix a sever performance bug

Any function of Lua log module would first evaluate the call stack,
and only then invoke the logger function -- even if the text is
then sent to /dev/null because log level is too low.

Short cut the log call in case the log level is low and the message
is muted.
---
 src/lua/log.lua | 5 +++++
 src/say.cc      | 9 ++++-----
 src/say.h       | 1 +
 3 files changed, 10 insertions(+), 5 deletions(-)

diff --git a/src/lua/log.lua b/src/lua/log.lua
index ae638a2866..d086bcc6df 100644
--- a/src/lua/log.lua
+++ b/src/lua/log.lua
@@ -19,9 +19,14 @@ ffi.cdef[[
     };
 
     pid_t logger_pid;
+    extern int log_level;
 ]]
 
 local function say(level, fmt, ...)
+    if ffi.C.log_level < level then
+-- don't waste cycles on debug.getinfo()
+        return
+    end
     local debug = require('debug')
     local str = string.format(tostring(fmt), ...)
     local frame = debug.getinfo(3, "Sl")
diff --git a/src/say.cc b/src/say.cc
index ec96fddcf1..9935e91a96 100644
--- a/src/say.cc
+++ b/src/say.cc
@@ -48,8 +48,7 @@ pid_t logger_pid;
 static bool booting = true;
 static bool logger_background = true;
 static const char *binary_filename;
-static int log_level_default = S_INFO;
-static int *log_level = &log_level_default;
+int log_level = S_INFO;
 
 static void
 sayf(int level, const char *filename, int line, const char *error,
@@ -89,7 +88,7 @@ say_init(const char *argv0)
 void
 say_set_log_level(int new_level)
 {
-	*log_level = new_level;
+	log_level = new_level;
 }
 
 /**
@@ -216,7 +215,7 @@ say_init_file()
 void
 say_logger_init(const char *path, int level, int nonblock, int background)
 {
-	*log_level = level;
+	log_level = level;
 	logger_nonblock = nonblock;
 	logger_background = background;
 	setvbuf(stderr, NULL, _IONBF, 0);
@@ -307,7 +306,7 @@ static void
 sayf(int level, const char *filename, int line, const char *error, const char *format, ...)
 {
 	int errsv = errno; /* Preserve the errno. */
-	if (*log_level < level)
+	if (log_level < level)
 		return;
 	va_list ap;
 	va_start(ap, format);
diff --git a/src/say.h b/src/say.h
index c2f74ed9ec..1896958e92 100644
--- a/src/say.h
+++ b/src/say.h
@@ -52,6 +52,7 @@ enum say_level {
 /** \endcond public */
 
 extern int log_fd;
+extern int log_level;
 extern pid_t logger_pid;
 
 void
-- 
GitLab