Skip to content

Commit

Permalink
perf(router): remove NYIs to be more JIT-friendly (#12467)
Browse files Browse the repository at this point in the history
  • Loading branch information
ADD-SP committed Mar 14, 2024
1 parent 35c292c commit c72f980
Show file tree
Hide file tree
Showing 2 changed files with 62 additions and 44 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: Speeded up the router matching when the `router_flavor` is `traditional_compatible` or `expressions`.
type: performance
scope: Performance
103 changes: 59 additions & 44 deletions kong/router/fields.lua
Original file line number Diff line number Diff line change
Expand Up @@ -262,55 +262,31 @@ if is_http then
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 = 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)
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

f = function(params)
if not params.queries then
params.queries = get_http_params(get_uri_args, "queries", "lua_max_uri_args")
end
return params.headers[name]
end
end

return params.queries[name]
end -- f

funcs[field] = f
return f
end -- if prefix == HTTP_QUERIES_PREFIX
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

-- generate for http.path.segments.*
return params.queries[name]
end
end

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

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 +335,48 @@ 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

1 comment on commit c72f980

@github-actions
Copy link
Contributor

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:c72f9803cf240d67976ef86113c55ded787784a3
Artifacts available https://github.com/Kong/kong/actions/runs/8275049994

Please sign in to comment.