Skip to content

optimize: resty.core.regex: change regex cache key to eliminate string concatenations #3

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 1 commit into from
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 11 additions & 3 deletions lib/resty/core/regex.lua
Original file line number Diff line number Diff line change
Expand Up @@ -290,6 +290,7 @@ end
local function re_match_compile(regex, opts)
local flags = 0
local pcre_opts = 0
local complied_by_opts

if opts then
flags, pcre_opts = parse_regex_opts(opts)
Expand All @@ -312,8 +313,11 @@ local function re_match_compile(regex, opts)
end

if compile_once then
key = regex .. '\0' .. opts
compiled = lrucache_get(regex_match_cache, key)
local res = lrucache_get(regex_match_cache, regex)
if res ~= nil then
complied_by_opts = res
compiled = complied_by_opts[opts]
end
end

-- compile the regex
Expand All @@ -335,8 +339,12 @@ local function re_match_compile(regex, opts)
-- print("ncaptures: ", compiled.ncaptures)

if compile_once then
if not complied_by_opts then
complied_by_opts = new_tab(0, 1)
end
complied_by_opts[opts] = compiled
-- print("inserting compiled regex into cache")
lrucache_set(regex_match_cache, key, compiled)
lrucache_set(regex_match_cache, regex, complied_by_opts)
end
end

Expand Down