From e8173ca30b902202e289e93ab34cf40a907fd3d6 Mon Sep 17 00:00:00 2001 From: Chrono Date: Tue, 27 Jun 2023 16:31:23 +0800 Subject: [PATCH] perf(router): iterate routes with for loop (#11111) for loop has better performance than function ipairs(), especially for huge table. --- kong/router/atc.lua | 8 ++++++-- kong/router/utils.lua | 4 ++-- 2 files changed, 8 insertions(+), 4 deletions(-) diff --git a/kong/router/atc.lua b/kong/router/atc.lua index 487d63b62a2c..967ad9bd8f88 100644 --- a/kong/router/atc.lua +++ b/kong/router/atc.lua @@ -181,7 +181,9 @@ local function new_from_scratch(routes, get_exp_and_priority) local new_updated_at = 0 - for _, r in ipairs(routes) do + for i = 1, routes_n do + local r = routes[i] + local route = r.route local route_id = route.id @@ -240,7 +242,9 @@ local function new_from_previous(routes, get_exp_and_priority, old_router) local new_updated_at = 0 -- create or update routes - for _, r in ipairs(routes) do + for i = 1, #routes do + local r = routes[i] + local route = r.route local route_id = route.id diff --git a/kong/router/utils.lua b/kong/router/utils.lua index 3721feb8e5f5..0aa9a4b0f047 100644 --- a/kong/router/utils.lua +++ b/kong/router/utils.lua @@ -292,8 +292,8 @@ do local v0 = 0 local v1 = 0 - for _, route in ipairs(routes) do - local r = route.route + for i = 1, #routes do + local r = routes[i].route local paths_t = r.paths or empty_table local headers_t = r.headers or empty_table