Hi,
I noticed in some bench that my ngx.log(ngx.DEBUG, '[Lua] data: ', data) seemed to be expensive.
I think probably wrapping the whole thing like this, would fix the issue :
local DEBUG = true
if DEBUG then
ngx.log(ngx.DEBUG, '[Lua] data: ', data)
end
But this seems very redundant.
So I looked into ngx_http_lua_log.c and I found that potentially we allocate a ton of memory just to do some logging. And the string might not get logged if the level is lower than log_level.
Which brings me to the patch below, it basically bails out very early on. If the log level wouldn't let the message print.
We could also push this even further and prevent most memory allocation, by using static char* for "nil", "true", "false" iterate over the arguments and directly use :
ngx_write_fd(log->file->fd, errstr, errstr_size);
Regards,
Matthieu.
------- DIFF -------
diff --git a/src/ngx_http_lua_log.c b/src/ngx_http_lua_log.c
index fab0590..9a4e352 100644
--- a/src/ngx_http_lua_log.c
+++ b/src/ngx_http_lua_log.c
@@ -10,7 +10,7 @@ static int ngx_http_lua_print(lua_State *L);
static int ngx_http_lua_ngx_log(lua_State *L);
-static int log_wrapper(ngx_http_request_t *r, const char *ident, int level,
+static int log_wrapper(ngx_http_request_t *r, const char *ident, ngx_uint_t level,
lua_State *L);
static void ngx_http_lua_inject_log_consts(lua_State *L);
@@ -75,7 +75,7 @@ ngx_http_lua_print(lua_State *L)
static int
-log_wrapper(ngx_http_request_t *r, const char *ident, int level, lua_State *L)
+log_wrapper(ngx_http_request_t *r, const char *ident, ngx_uint_t level, lua_State *L)
{
u_char *buf;
u_char *p;
@@ -91,6 +91,10 @@ log_wrapper(ngx_http_request_t *r, const char *ident, int level, lua_State *L)
goto done;
}
+ if (level < r->connection->log->log_level) {
+ return 0;
+ }
+
size = 0;
for (i = 1; i <= nargs; i++) {
@@ -185,7 +189,7 @@ log_wrapper(ngx_http_request_t *r, const char *ident, int level, lua_State *L)
*p++ = '\0';
done:
- ngx_log_error((ngx_uint_t) level, r->connection->log, 0,
+ ngx_log_error(level, r->connection->log, 0,
"%s%s", ident, (buf == NULL) ? (u_char *) "(null)" : buf);
return 0;
}