From b74504ba725bd5842f99bd77554ae69c8efead43 Mon Sep 17 00:00:00 2001 From: Chrono Date: Thu, 28 Dec 2023 16:49:25 +0800 Subject: [PATCH] perf(router): reuse ATC context in router match instead of creating a new context (#12258) To avoid frequent memory allocation/deallocations. KAG-3448 --- .requirements | 2 +- changelog/unreleased/kong/atc_reuse_context.yml | 3 +++ changelog/unreleased/kong/bump-atc-router.yml | 3 +++ kong/router/atc.lua | 13 +++++++++---- kong/router/fields.lua | 7 +++---- 5 files changed, 19 insertions(+), 9 deletions(-) create mode 100644 changelog/unreleased/kong/atc_reuse_context.yml create mode 100644 changelog/unreleased/kong/bump-atc-router.yml diff --git a/.requirements b/.requirements index ef36191763a..c7388ffe885 100644 --- a/.requirements +++ b/.requirements @@ -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.5.0.0 NGX_WASM_MODULE=388d5720293f5091ccee1f859a42683fbfd14e7d # prerelease-0.2.0 diff --git a/changelog/unreleased/kong/atc_reuse_context.yml b/changelog/unreleased/kong/atc_reuse_context.yml new file mode 100644 index 00000000000..3af76d0a2d7 --- /dev/null +++ b/changelog/unreleased/kong/atc_reuse_context.yml @@ -0,0 +1,3 @@ +message: "Reuse match copntext between requests to avoid frequent memory allocation/deallocation" +type: performance +scope: Core diff --git a/changelog/unreleased/kong/bump-atc-router.yml b/changelog/unreleased/kong/bump-atc-router.yml new file mode 100644 index 00000000000..2013fd9dda6 --- /dev/null +++ b/changelog/unreleased/kong/bump-atc-router.yml @@ -0,0 +1,3 @@ +message: Bumped atc-router from 1.2.0 to 1.4.1 +type: dependency +scope: Core diff --git a/kong/router/atc.lua b/kong/router/atc.lua index a11ccb112f0..25d95ab0e62 100644 --- a/kong/router/atc.lua +++ b/kong/router/atc.lua @@ -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") @@ -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 @@ -223,7 +224,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, @@ -412,7 +413,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 @@ -552,7 +555,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 diff --git a/kong/router/fields.lua b/kong/router/fields.lua index 11e2a09fe95..a33b27c8fcd 100644 --- a/kong/router/fields.lua +++ b/kong/router/fields.lua @@ -1,5 +1,4 @@ local buffer = require("string.buffer") -local context = require("resty.router.context") local type = type @@ -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) @@ -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, }