Skip to content

Commit 5b663e9

Browse files
authored
perf: return the first route if the opts is empty. (#58)
1 parent e394d3f commit 5b663e9

File tree

1 file changed

+8
-5
lines changed

1 file changed

+8
-5
lines changed

lib/resty/radixtree.lua

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -619,6 +619,13 @@ end
619619

620620

621621
local function _match_from_routes(routes, path, opts, ...)
622+
if opts == empty_table then
623+
local route = routes[1]
624+
if not route or route.method == 0 then
625+
return route
626+
end
627+
end
628+
622629
local opts_matched_exists = (opts.matched ~= nil)
623630
for _, route in ipairs(routes) do
624631
if route.path_op == "=" then
@@ -657,8 +664,8 @@ local function match_route(self, path, opts, ...)
657664
end
658665

659666
local routes = self.hash_path[path]
660-
local opts_matched_exists = (opts.matched ~= nil)
661667
if routes then
668+
local opts_matched_exists = (opts.matched ~= nil)
662669
for _, route in ipairs(routes) do
663670
if match_route_opts(route, opts, ...) then
664671
if opts_matched_exists then
@@ -693,10 +700,6 @@ local function match_route(self, path, opts, ...)
693700
end
694701

695702
function _M.match(self, path, opts)
696-
if type(path) ~= "string" then
697-
error("invalid argument path", 2)
698-
end
699-
700703
local route, err = match_route(self, path, opts or empty_table)
701704
if not route then
702705
if err then

0 commit comments

Comments
 (0)