Skip to content

Commit

Permalink
perf(router): reuse ATC context in router match instead of creating a…
Browse files Browse the repository at this point in the history
… new context (#12258)

To avoid frequent memory allocation/deallocations.

KAG-3448
  • Loading branch information
chronolaw committed Mar 4, 2024
1 parent 9db59c0 commit d04991f
Show file tree
Hide file tree
Showing 5 changed files with 19 additions and 9 deletions.
2 changes: 1 addition & 1 deletion .requirements
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ LUA_KONG_NGINX_MODULE=4fbc3ddc7dcbc706ed286b95344f3cb6da17e637 # 0.8.0
LUA_RESTY_LMDB=951926f20b674a0622236a0e331b359df1c02d9b # 1.3.0
LUA_RESTY_EVENTS=8448a92cec36ac04ea522e78f6496ba03c9b1fd8 # 0.2.0
LUA_RESTY_WEBSOCKET=60eafc3d7153bceb16e6327074e0afc3d94b1316 # 0.4.0
ATC_ROUTER=b0d5e7e2a2ca59bb051959385d3e42d96c93bb98 # 1.2.0
ATC_ROUTER=ac71b24ea5556b38b0f9903850ed666c36ad7843 # 1.4.1

KONG_MANAGER=v3.4.1.0
NGX_WASM_MODULE=21732b18fc46f409962ae77ddf01c713b568d078 # prerelease-0.1.1
Expand Down
3 changes: 3 additions & 0 deletions changelog/unreleased/kong/atc_reuse_context.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
message: "Reuse match copntext between requests to avoid frequent memory allocation/deallocation"
type: performance
scope: Core
3 changes: 3 additions & 0 deletions changelog/unreleased/kong/bump-atc-router.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
message: Bumped atc-router from 1.2.0 to 1.4.1
type: dependency
scope: Core
13 changes: 9 additions & 4 deletions kong/router/atc.lua
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ local _MT = { __index = _M, }

local buffer = require("string.buffer")
local schema = require("resty.router.schema")
local context = require("resty.router.context")
local router = require("resty.router.router")
local lrucache = require("resty.lrucache")
local tb_new = require("table.new")
Expand Down Expand Up @@ -35,7 +36,7 @@ local check_select_params = utils.check_select_params
local get_service_info = utils.get_service_info
local route_match_stat = utils.route_match_stat
local get_cache_key = fields.get_cache_key
local get_atc_context = fields.get_atc_context
local fill_atc_context = fields.fill_atc_context


local DEFAULT_MATCH_LRUCACHE_SIZE = utils.DEFAULT_MATCH_LRUCACHE_SIZE
Expand Down Expand Up @@ -225,7 +226,7 @@ local function new_from_scratch(routes, get_exp_and_priority)
local fields = inst:get_fields()

return setmetatable({
schema = CACHED_SCHEMA,
context = context.new(CACHED_SCHEMA),
router = inst,
routes = routes_t,
services = services_t,
Expand Down Expand Up @@ -416,7 +417,9 @@ function _M:matching(params)
params.host = host
params.port = port

local c, err = get_atc_context(self.schema, self.fields, params)
self.context:reset()

local c, err = fill_atc_context(self.context, self.fields, params)

if not c then
return nil, err
Expand Down Expand Up @@ -556,7 +559,9 @@ function _M:matching(params)
params.dst_ip, params.dst_port,
sni)

local c, err = get_atc_context(self.schema, self.fields, params)
self.context:reset()

local c, err = fill_atc_context(self.context, self.fields, params)
if not c then
return nil, err
end
Expand Down
7 changes: 3 additions & 4 deletions kong/router/fields.lua
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
local buffer = require("string.buffer")
local context = require("resty.router.context")


local type = type
Expand Down Expand Up @@ -288,8 +287,8 @@ local function get_cache_key(fields, params, ctx)
end


local function get_atc_context(schema, fields, params)
local c = context.new(schema)
local function fill_atc_context(context, fields, params)
local c = context

local res, err =
fields_visitor(fields, params, nil, function(field, value)
Expand Down Expand Up @@ -354,7 +353,7 @@ end

return {
get_cache_key = get_cache_key,
get_atc_context = get_atc_context,
fill_atc_context = fill_atc_context,

_set_ngx = _set_ngx,
}

0 comments on commit d04991f

Please sign in to comment.