Skip to content

Commit d388f7b

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

File tree

1 file changed

+11
-3
lines changed

1 file changed

+11
-3
lines changed

lib/resty/core/regex.lua

Lines changed: 11 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 complied_by_opts
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+
complied_by_opts = res
319+
compiled = complied_by_opts[opts]
320+
end
317321
end
318322

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

337341
if compile_once then
342+
if not complied_by_opts then
343+
complied_by_opts = new_tab(0, 1)
344+
end
345+
complied_by_opts[opts] = compiled
338346
-- print("inserting compiled regex into cache")
339-
lrucache_set(regex_match_cache, key, compiled)
347+
lrucache_set(regex_match_cache, regex, complied_by_opts)
340348
end
341349
end
342350

0 commit comments

Comments
 (0)