Skip to content

Commit

Permalink
perf(router): remove NYI to be more JIT-friendly
Browse files Browse the repository at this point in the history
  • Loading branch information
ADD-SP committed Mar 14, 2024
1 parent 15a3797 commit 0d3b29b
Show file tree
Hide file tree
Showing 2 changed files with 60 additions and 46 deletions.
3 changes: 3 additions & 0 deletions changelog/unreleased/kong/speed_up_router.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
message: Speed up the router matching when the `router_flavor` is `traditional_compatible` or `expressions`.
type: performance
scope: Performance
103 changes: 57 additions & 46 deletions kong/router/fields.lua
Original file line number Diff line number Diff line change
Expand Up @@ -261,56 +261,29 @@ if is_http then
return params
end

local function gen_http_headers_field_accessor(name)
return function(params)
if not params.headers then
params.headers = get_http_params(get_headers, "headers", "lua_max_req_headers")
end

get_field_accessor = function(funcs, field)
local f = FIELDS_FUNCS[field] or funcs[field]
if f then
return f
return params.headers[name]
end
end

local prefix = field:sub(1, PREFIX_LEN)

-- generate for http.headers.*

if prefix == HTTP_HEADERS_PREFIX then
local name = field:sub(PREFIX_LEN + 1)

f = function(params)
if not params.headers then
params.headers = get_http_params(get_headers, "headers", "lua_max_req_headers")
end

return params.headers[name]
end -- f

funcs[field] = f
return f
end -- if prefix == HTTP_HEADERS_PREFIX

-- generate for http.queries.*

if prefix == HTTP_QUERIES_PREFIX then
local name = field:sub(PREFIX_LEN + 1)

f = function(params)
if not params.queries then
params.queries = get_http_params(get_uri_args, "queries", "lua_max_uri_args")
end

return params.queries[name]
end -- f

funcs[field] = f
return f
end -- if prefix == HTTP_QUERIES_PREFIX

-- generate for http.path.segments.*
local function gen_http_queries_field_accessor(name)
return function(params)
if not params.queries then
params.queries = get_http_params(get_uri_args, "queries", "lua_max_uri_args")
end

if field:sub(1, HTTP_SEGMENTS_PREFIX_LEN) == HTTP_SEGMENTS_PREFIX then
local range = field:sub(HTTP_SEGMENTS_PREFIX_LEN + 1)
return params.queries[name]
end
end

f = function(params)
local segments = get_http_segments(params)
local function gen_http_segments_field_accessor(range)
return function(params)
local segments = get_http_segments(params)

local value = segments[range]

Expand Down Expand Up @@ -359,9 +332,47 @@ if is_http then
segments[range] = value

return value
end -- f
end
end

get_field_accessor = function(funcs, field)
local f = FIELDS_FUNCS[field] or funcs[field]
if f then
return f
end

local prefix = field:sub(1, PREFIX_LEN)

-- generate for http.headers.*

if prefix == HTTP_HEADERS_PREFIX then
local name = field:sub(PREFIX_LEN + 1)

f = gen_http_headers_field_accessor(name)
funcs[field] = f

return f
end -- if prefix == HTTP_HEADERS_PREFIX

-- generate for http.queries.*

if prefix == HTTP_QUERIES_PREFIX then
local name = field:sub(PREFIX_LEN + 1)

f = gen_http_queries_field_accessor(name)
funcs[field] = f

return f
end -- if prefix == HTTP_QUERIES_PREFIX

-- generate for http.path.segments.*

if field:sub(1, HTTP_SEGMENTS_PREFIX_LEN) == HTTP_SEGMENTS_PREFIX then
local range = field:sub(HTTP_SEGMENTS_PREFIX_LEN + 1)

f = gen_http_segments_field_accessor(range)
funcs[field] = f

return f
end -- if field:sub(1, HTTP_SEGMENTS_PREFIX_LEN)

Expand Down

0 comments on commit 0d3b29b

Please sign in to comment.