Skip to content

Commit 5b1d055

Browse files
authored
perf: avoid calling pcre to match every request, it is slower. (#51)
* perf: avoid calling `pcre` to match every request, it is slower. * test: removed wrong test case and fixed title.
1 parent 68f68e5 commit 5b1d055

File tree

3 files changed

+27
-46
lines changed

3 files changed

+27
-46
lines changed

lib/resty/radixtree.lua

Lines changed: 22 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,7 @@ local sort_tab = table.sort
5050
local ngx_re = require("ngx.re")
5151
local ngx_null = ngx.null
5252
local empty_table = {}
53+
local str_find = string.find
5354

5455

5556
setmetatable(empty_table, {__newindex = function()
@@ -280,16 +281,21 @@ function pre_insert_route(self, path, route)
280281
end
281282

282283
route_opts.path_org = path
284+
route_opts.param = false
283285

284-
local pos = string.find(path, ':', 1, true)
286+
local pos = str_find(path, ':', 1, true)
285287
if pos then
286288
path = path:sub(1, pos - 1)
287289
route_opts.path_op = "<="
288290
route_opts.path = path
291+
route_opts.param = true
289292

290293
else
291-
pos = string.find(path, '*', 1, true)
294+
pos = str_find(path, '*', 1, true)
292295
if pos then
296+
if pos ~= #path then
297+
route_opts.param = true
298+
end
293299
path = path:sub(1, pos - 1)
294300
route_opts.path_op = "<="
295301
else
@@ -434,10 +440,14 @@ local function fetch_pat(path)
434440
return pat, names
435441
end
436442

437-
local function compare_gin(l_v, r_v, opts)
438-
local pat, names = fetch_pat(r_v)
439-
-- log_info("pat: ", require("cjson").encode(pat))
440-
local m = re_match(l_v, pat, "jo")
443+
local function compare_param(req_path, route, opts)
444+
if not opts.matched and not route.param then
445+
return true
446+
end
447+
448+
local pat, names = fetch_pat(route.path_org)
449+
log_info("pcre pat: ", pat)
450+
local m = re_match(req_path, pat, "jo")
441451
if not m then
442452
return false
443453
end
@@ -502,8 +512,8 @@ local compare_funcs = {
502512
end
503513
return false
504514
end,
505-
["IN"] = in_array,
506-
["in"] = in_array,
515+
["IN"] = in_array,
516+
["in"] = in_array,
507517
}
508518

509519

@@ -622,10 +632,10 @@ local function _match_from_routes(routes, path, opts, ...)
622632
if match_route_opts(route, opts, ...) then
623633
-- log_info("matched route: ", require("cjson").encode(route))
624634
-- log_info("matched path: ", path)
625-
if compare_gin(path, route.path_org, opts) then
626-
if opts_matched_exists then
627-
opts.matched._path = route.path_org
628-
end
635+
if compare_param(path, route, opts) then
636+
if opts_matched_exists then
637+
opts.matched._path = route.path_org
638+
end
629639
return route
630640
end
631641
end

t/parameter.t

Lines changed: 4 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -171,39 +171,7 @@ matched: {}
171171
172172
173173
174-
=== TEST 6: /name/:name/id/:id
175-
--- config
176-
location /t {
177-
content_by_lua_block {
178-
local json = require("cjson.safe")
179-
local radix = require("resty.radixtree")
180-
local rx = radix.new({
181-
{
182-
paths = {"/aa/*", "/bb/cc/*", "/dd/ee/index.html"},
183-
methods = {"GET", "POST", "PUT"},
184-
hosts = {"foo.com", "*.bar.com"},
185-
metadata = "metadata /asf",
186-
},
187-
})
188-
189-
local opts = {matched = {}, method = "GET", host = "aa.bar.com"}
190-
local meta = rx:match("/bb/cc/xx", opts)
191-
ngx.say("match meta: ", meta)
192-
ngx.say("matched: ", json.encode(opts.matched))
193-
194-
}
195-
}
196-
--- request
197-
GET /t
198-
--- no_error_log
199-
[error]
200-
--- response_body
201-
match meta: metadata /asf
202-
matched: {"_path":"\/bb\/cc\/*",":ext":"xx","_method":"GET","_host":"*.bar.com"}
203-
204-
205-
206-
=== TEST 7: /name/*name/foo (cached parameter)
174+
=== TEST 6: /name/:name/foo (cached parameter)
207175
--- config
208176
location /t {
209177
content_by_lua_block {
@@ -238,7 +206,7 @@ matched: {}
238206
239207
240208
241-
=== TEST 8: /name/*name/foo (no cached parameter)
209+
=== TEST 7: /name/:name/foo (no cached parameter)
242210
--- config
243211
location /t {
244212
content_by_lua_block {
@@ -265,3 +233,5 @@ GET /t
265233
--- response_body
266234
match meta: metadata /name
267235
match meta: nil
236+
--- error_log
237+
pcre pat:

t/path.t

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -96,6 +96,7 @@ nil
9696
GET /t
9797
--- no_error_log
9898
[error]
99+
pcre pat:
99100
--- response_body
100101
metadata multipe path 1
101102
metadata multipe path 1

0 commit comments

Comments
 (0)