From 501db64e7bc6fad34547e4d547ff9af61d9ab801 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 e2e5a9a357df..1c8db3b80c48 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.4.1.0 NGX_WASM_MODULE=21732b18fc46f409962ae77ddf01c713b568d078 # prerelease-0.1.1 diff --git a/changelog/unreleased/kong/atc_reuse_context.yml b/changelog/unreleased/kong/atc_reuse_context.yml new file mode 100644 index 000000000000..3af76d0a2d72 --- /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 000000000000..2013fd9dda69 --- /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 568272979a17..5cdce6f51836 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 @@ -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, @@ -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 @@ -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 diff --git a/kong/router/fields.lua b/kong/router/fields.lua index 11e2a09fe959..a33b27c8fcd5 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, }