Skip to content

Commit

Permalink
refactor(pdk): serialize log msg with string.buffer (#11811)
Browse files Browse the repository at this point in the history
Use string.buffer to optimize string operation.

Here I simply replace table.insert and table.concat, but not sure the serializers[n]'s effect, so just keep them.
  • Loading branch information
chronolaw authored Nov 7, 2023
1 parent 8cac765 commit 5d2c511
Showing 1 changed file with 27 additions and 25 deletions.
52 changes: 27 additions & 25 deletions kong/pdk/log.lua
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
-- @module kong.log


local buffer = require "string.buffer"
local errlog = require "ngx.errlog"
local ngx_re = require "ngx.re"
local inspect = require "inspect"
Expand Down Expand Up @@ -137,34 +138,34 @@ end


local serializers = {
[1] = function(buf, to_string, ...)
buf[1] = to_string((select(1, ...)))
[1] = function(buf, sep, to_string, ...)
buf:put(to_string((select(1, ...))))
end,

[2] = function(buf, to_string, ...)
buf[1] = to_string((select(1, ...)))
buf[2] = to_string((select(2, ...)))
[2] = function(buf, sep, to_string, ...)
buf:put(to_string((select(1, ...)))):put(sep)
:put(to_string((select(2, ...))))
end,

[3] = function(buf, to_string, ...)
buf[1] = to_string((select(1, ...)))
buf[2] = to_string((select(2, ...)))
buf[3] = to_string((select(3, ...)))
[3] = function(buf, sep, to_string, ...)
buf:put(to_string((select(1, ...)))):put(sep)
:put(to_string((select(2, ...)))):put(sep)
:put(to_string((select(3, ...))))
end,

[4] = function(buf, to_string, ...)
buf[1] = to_string((select(1, ...)))
buf[2] = to_string((select(2, ...)))
buf[3] = to_string((select(3, ...)))
buf[4] = to_string((select(4, ...)))
[4] = function(buf, sep, to_string, ...)
buf:put(to_string((select(1, ...)))):put(sep)
:put(to_string((select(2, ...)))):put(sep)
:put(to_string((select(3, ...)))):put(sep)
:put(to_string((select(4, ...))))
end,

[5] = function(buf, to_string, ...)
buf[1] = to_string((select(1, ...)))
buf[2] = to_string((select(2, ...)))
buf[3] = to_string((select(3, ...)))
buf[4] = to_string((select(4, ...)))
buf[5] = to_string((select(5, ...)))
[5] = function(buf, sep, to_string, ...)
buf:put(to_string((select(1, ...)))):put(sep)
:put(to_string((select(2, ...)))):put(sep)
:put(to_string((select(3, ...)))):put(sep)
:put(to_string((select(4, ...)))):put(sep)
:put(to_string((select(5, ...))))
end,
}

Expand Down Expand Up @@ -282,7 +283,7 @@ local function gen_log_func(lvl_const, imm_buf, to_string, stack_level, sep)
to_string = to_string or tostring
stack_level = stack_level or 2

local variadic_buf = {}
local variadic_buf = buffer.new()

return function(...)
local sys_log_level = nil
Expand Down Expand Up @@ -320,15 +321,16 @@ local function gen_log_func(lvl_const, imm_buf, to_string, stack_level, sep)
end

if serializers[n] then
serializers[n](variadic_buf, to_string, ...)
serializers[n](variadic_buf, sep or "" , to_string, ...)

else
for i = 1, n do
variadic_buf[i] = to_string((select(i, ...)))
for i = 1, n - 1 do
variadic_buf:put(to_string((select(i, ...)))):put(sep or "")
end
variadic_buf:put(to_string((select(n, ...))))
end

local msg = concat(variadic_buf, sep, 1, n)
local msg = variadic_buf:get()

for i = 1, imm_buf.n_messages do
imm_buf[imm_buf.message_idxs[i]] = msg
Expand Down

1 comment on commit 5d2c511

@khcp-gha-bot
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Bazel Build

Docker image available kong/kong:5d2c51100de727a582d17f20bbdeae9c2e710b9d
Artifacts available https://github.com/Kong/kong/actions/runs/6779509422

Please sign in to comment.