Skip to content

Commit 26ae119

Browse files
author
Guanlan Dai
committed
optimize: resty.core.regex: change regex cache key to eliminate string concatenations
1 parent 5496e3b commit 26ae119

File tree

1 file changed

+8
-3
lines changed

1 file changed

+8
-3
lines changed

lib/resty/core/regex.lua

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -290,6 +290,7 @@ end
290290
local function re_match_compile(regex, opts)
291291
local flags = 0
292292
local pcre_opts = 0
293+
local regex_match_tab = new_tab(0, 4)
293294

294295
if opts then
295296
flags, pcre_opts = parse_regex_opts(opts)
@@ -312,8 +313,11 @@ local function re_match_compile(regex, opts)
312313
end
313314

314315
if compile_once then
315-
key = regex .. '\0' .. opts
316-
compiled = lrucache_get(regex_match_cache, key)
316+
local res = lrucache_get(regex_match_cache, regex)
317+
if res ~= nil then
318+
regex_match_tab = res
319+
compiled = regex_match_tab[opts]
320+
end
317321
end
318322

319323
-- compile the regex
@@ -335,8 +339,9 @@ local function re_match_compile(regex, opts)
335339
-- print("ncaptures: ", compiled.ncaptures)
336340

337341
if compile_once then
342+
regex_match_tab[opts] = compiled
338343
-- print("inserting compiled regex into cache")
339-
lrucache_set(regex_match_cache, key, compiled)
344+
lrucache_set(regex_match_cache, regex, regex_match_tab)
340345
end
341346
end
342347

0 commit comments

Comments
 (0)