Skip to content

Commit

Permalink
perf(router): use resty.core.utils.str_replace_char() for dashes (#…
Browse files Browse the repository at this point in the history
…11721)

resty.core.utils.str_replace_char() is a better way to replace - to _.
In the future string.lua will gather more functions to simplify tools.utils.lua.

See: #10443
  • Loading branch information
chronolaw authored and locao committed Mar 5, 2024
1 parent ec49e80 commit b7cdced
Show file tree
Hide file tree
Showing 5 changed files with 52 additions and 19 deletions.
1 change: 1 addition & 0 deletions kong-3.5.1-0.rockspec
Original file line number Diff line number Diff line change
Expand Up @@ -164,6 +164,7 @@ build = {
["kong.tools.protobuf"] = "kong/tools/protobuf.lua",
["kong.tools.mime_type"] = "kong/tools/mime_type.lua",
["kong.tools.request_aware_table"] = "kong/tools/request_aware_table.lua",
["kong.tools.string"] = "kong/tools/string.lua",

["kong.runloop.handler"] = "kong/runloop/handler.lua",
["kong.runloop.events"] = "kong/runloop/events.lua",
Expand Down
17 changes: 1 addition & 16 deletions kong/pdk/request.lua
Original file line number Diff line number Diff line change
Expand Up @@ -41,9 +41,6 @@ local get_body_file = req.get_body_file
local decode_args = ngx.decode_args


local is_http_subsystem = ngx and ngx.config.subsystem == "http"


local PHASES = phase_checker.phases


Expand Down Expand Up @@ -85,19 +82,7 @@ local function new(self)
end
end

local replace_dashes do
-- 1.000.000 iterations with input of "my-header":
-- string.gsub: 81ms
-- ngx.re.gsub: 74ms
-- loop/string.buffer: 28ms
-- str_replace_char: 14ms
if is_http_subsystem then
local str_replace_char = require("resty.core.utils").str_replace_char
replace_dashes = function(str)
return str_replace_char(str, "-", "_")
end
end
end
local replace_dashes = require("kong.tools.string").replace_dashes


---
Expand Down
3 changes: 2 additions & 1 deletion kong/router/atc.lua
Original file line number Diff line number Diff line change
Expand Up @@ -562,6 +562,7 @@ local get_queries_key
do
local tb_sort = table.sort
local tb_concat = table.concat
local replace_dashes_lower = require("kong.tools.string").replace_dashes_lower

local str_buf = buffer.new(64)

Expand All @@ -570,7 +571,7 @@ do

-- NOTE: DO NOT yield until str_buf:get()
for name, value in pairs(headers) do
local name = name:gsub("-", "_"):lower()
local name = replace_dashes_lower(name)

if type(value) == "table" then
for i, v in ipairs(value) do
Expand Down
5 changes: 3 additions & 2 deletions kong/router/compat.lua
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,8 @@ local tb_nkeys = require("table.nkeys")
local uuid = require("resty.jit-uuid")


local shallow_copy = require("kong.tools.utils").shallow_copy
local shallow_copy = require("kong.tools.utils").shallow_copy
local replace_dashes_lower = require("kong.tools.string").replace_dashes_lower


local is_regex_magic = utils.is_regex_magic
Expand Down Expand Up @@ -251,7 +252,7 @@ local function get_expression(route)
single_header_buf:reset():put("(")

for i, value in ipairs(v) do
local name = "any(http.headers." .. h:gsub("-", "_"):lower() .. ")"
local name = "any(http.headers." .. replace_dashes_lower(h) .. ")"
local op = OP_EQUAL

-- value starts with "~*"
Expand Down
45 changes: 45 additions & 0 deletions kong/tools/string.lua
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
local find = string.find
local gsub = string.gsub


local _M = {}


local replace_dashes
local replace_dashes_lower
do
local str_replace_char

if ngx and ngx.config.subsystem == "http" then

-- 1,000,000 iterations with input of "my-header":
-- string.gsub: 81ms
-- ngx.re.gsub: 74ms
-- loop/string.buffer: 28ms
-- str_replace_char: 14ms
str_replace_char = require("resty.core.utils").str_replace_char

else -- stream subsystem
str_replace_char = function(str, ch, replace)
if not find(str, ch, nil, true) then
return str
end

return gsub(str, ch, replace)
end
end

replace_dashes = function(str)
return str_replace_char(str, "-", "_")
end

replace_dashes_lower = function(str)
return str_replace_char(str:lower(), "-", "_")
end
end
_M.replace_dashes = replace_dashes
_M.replace_dashes_lower = replace_dashes_lower


return _M

0 comments on commit b7cdced

Please sign in to comment.