From 1b920ca144aed6669b2b1dbe5003683b4fcd18d3 Mon Sep 17 00:00:00 2001 From: Guilherme Salazar Date: Fri, 12 Apr 2024 13:28:15 -0300 Subject: [PATCH] perf(*): `ngx.re` uses are jitted and cached (#12857) This commit ensures that `ngx.re` uses (in critical (proxy path) codepaths) are both JITted and JIT-cached. --- kong/llm/init.lua | 2 +- kong/plugins/acme/client.lua | 4 ++-- kong/plugins/grpc-gateway/deco.lua | 2 +- kong/plugins/jwt/handler.lua | 2 +- kong/plugins/oauth2/access.lua | 3 ++- kong/plugins/proxy-cache/handler.lua | 2 +- kong/runloop/plugin_servers/mp_rpc.lua | 2 +- kong/runloop/plugin_servers/pb_rpc.lua | 2 +- 8 files changed, 10 insertions(+), 9 deletions(-) diff --git a/kong/llm/init.lua b/kong/llm/init.lua index f18e1a5ad8b1..1cef01b0ac49 100644 --- a/kong/llm/init.lua +++ b/kong/llm/init.lua @@ -616,7 +616,7 @@ function _M:ai_introspect_body(request, system_prompt, http_opts, response_regex -- this is useful for AI models that pad with assistant text, even when -- we ask them NOT to. if response_regex_match then - local matches, err = re_match(new_request_body, response_regex_match, "ijm") + local matches, err = re_match(new_request_body, response_regex_match, "ijom") if err then return nil, "failed regex matching ai response: " .. err end diff --git a/kong/plugins/acme/client.lua b/kong/plugins/acme/client.lua index 8254d92fefeb..a28c43d67eff 100644 --- a/kong/plugins/acme/client.lua +++ b/kong/plugins/acme/client.lua @@ -114,8 +114,8 @@ local function new(conf) -- backward compat local url = conf.api_uri - if not ngx_re_match(url, "/directory$") then - if not ngx_re_match(url, "/$") then + if not ngx_re_match(url, "/directory$", "jo") then + if not ngx_re_match(url, "/$", "jo") then url = url .. "/" end url = url .. "directory" diff --git a/kong/plugins/grpc-gateway/deco.lua b/kong/plugins/grpc-gateway/deco.lua index d3733fa55e25..25cff2adda3f 100644 --- a/kong/plugins/grpc-gateway/deco.lua +++ b/kong/plugins/grpc-gateway/deco.lua @@ -194,7 +194,7 @@ end local function add_to_table( t, path, v, typ ) local tab = t -- set up pointer to table root local msg_typ = typ; - for m in re_gmatch( path , "([^.]+)(\\.)?") do + for m in re_gmatch( path , "([^.]+)(\\.)?", "jo" ) do local key, dot = m[1], m[2] msg_typ = get_field_type(msg_typ, key) diff --git a/kong/plugins/jwt/handler.lua b/kong/plugins/jwt/handler.lua index 2288a8351e93..8bb7a954c65e 100644 --- a/kong/plugins/jwt/handler.lua +++ b/kong/plugins/jwt/handler.lua @@ -59,7 +59,7 @@ local function retrieve_tokens(conf) if type(token_header) == "table" then token_header = token_header[1] end - local iterator, iter_err = re_gmatch(token_header, "\\s*[Bb]earer\\s+(.+)") + local iterator, iter_err = re_gmatch(token_header, "\\s*[Bb]earer\\s+(.+)", "jo") if not iterator then kong.log.err(iter_err) break diff --git a/kong/plugins/oauth2/access.lua b/kong/plugins/oauth2/access.lua index 263317509e90..821e95c56552 100644 --- a/kong/plugins/oauth2/access.lua +++ b/kong/plugins/oauth2/access.lua @@ -438,7 +438,8 @@ local function retrieve_client_credentials(parameters, conf) from_authorization_header = true local iterator, iter_err = ngx_re_gmatch(authorization_header, - "\\s*[Bb]asic\\s*(.+)") + "\\s*[Bb]asic\\s*(.+)", + "jo") if not iterator then kong.log.err(iter_err) return diff --git a/kong/plugins/proxy-cache/handler.lua b/kong/plugins/proxy-cache/handler.lua index 0ba89dc7ca02..e1ceecc28c44 100644 --- a/kong/plugins/proxy-cache/handler.lua +++ b/kong/plugins/proxy-cache/handler.lua @@ -53,7 +53,7 @@ local function overwritable_header(header) local n_header = lower(header) return not hop_by_hop_headers[n_header] - and not ngx_re_match(n_header, "ratelimit-remaining") + and not ngx_re_match(n_header, "ratelimit-remaining", "jo") end local function set_header(conf, header, value) diff --git a/kong/runloop/plugin_servers/mp_rpc.lua b/kong/runloop/plugin_servers/mp_rpc.lua index 118c3694c05b..0895c44b600f 100644 --- a/kong/runloop/plugin_servers/mp_rpc.lua +++ b/kong/runloop/plugin_servers/mp_rpc.lua @@ -113,7 +113,7 @@ local function index_table(table, field) end local res = table - for segment, e in ngx.re.gmatch(field, "\\w+", "o") do + for segment, e in ngx.re.gmatch(field, "\\w+", "jo") do if res[segment[0]] then res = res[segment[0]] else diff --git a/kong/runloop/plugin_servers/pb_rpc.lua b/kong/runloop/plugin_servers/pb_rpc.lua index b94aca313ec5..8dbb85857abc 100644 --- a/kong/runloop/plugin_servers/pb_rpc.lua +++ b/kong/runloop/plugin_servers/pb_rpc.lua @@ -183,7 +183,7 @@ local function index_table(table, field) end local res = table - for segment, e in ngx.re.gmatch(field, "\\w+", "o") do + for segment, e in ngx.re.gmatch(field, "\\w+", "jo") do if res[segment[0]] then res = res[segment[0]] else