Skip to content

Commit 0395454

Browse files
authored
optimize: deleted some useless logic. (#15)
1 parent 17a94cb commit 0395454

File tree

1 file changed

+22
-39
lines changed

1 file changed

+22
-39
lines changed

lib/resty/radixtree.lua

Lines changed: 22 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,6 @@ local type = type
2020
local error = error
2121
local newproxy = newproxy
2222
local tostring = tostring
23-
local sort_tab = table.sort
2423
local cur_level = ngx.config.subsystem == "http" and
2524
require "ngx.errlog" .get_sys_filter_level()
2625
local ngx_var = ngx.var
@@ -427,37 +426,39 @@ local function match_route_opts(route, opts)
427426
return true
428427
end
429428

430-
431-
local function sort_route(l, r)
432-
return #l.path >= #r.path
433-
end
434-
435-
436-
local matched_routes = {}
437429
local radix_it = radix.radix_tree_new_it()
438430
if radix_it == nil then
439431
error("failed to new radixtree it")
440432
end
441433
-- use gc to free
442434
ffi.gc(radix_it, ffi.C.free)
443435

436+
local function _match_from_routes(routes, path, opts)
437+
for _, route in ipairs(routes) do
438+
if route.path_op == "=" then
439+
if route.path == path then
440+
if match_route_opts(route, opts) then
441+
return route
442+
end
443+
end
444+
445+
else
446+
if match_route_opts(route, opts) then
447+
return route
448+
end
449+
end
450+
end
451+
return nil
452+
end
453+
444454
local function match_route(self, path, opts)
445-
clear_tab(matched_routes)
446455
local routes = self.hash_path[path]
447456
if routes then
448457
for _, route in ipairs(routes) do
449-
insert_tab(matched_routes, route)
450-
end
451-
end
452-
453-
if #matched_routes > 0 then
454-
for _, route in ipairs(matched_routes) do
455458
if match_route_opts(route, opts) then
456459
return route
457460
end
458461
end
459-
460-
clear_tab(matched_routes)
461462
end
462463

463464
local it = radix.radix_tree_search(self.tree, radix_it, path, #path)
@@ -476,33 +477,15 @@ local function match_route(self, path, opts)
476477
routes = self.match_data[idx]
477478
-- log_info("route: ", require("cjson").encode(routes))
478479
if routes then
479-
for _, route in ipairs(routes) do
480-
if route.path_op == "=" then
481-
if route.path == path then
482-
insert_tab(matched_routes, route)
483-
break
484-
end
485-
else
486-
insert_tab(matched_routes, route)
487-
end
480+
local route = _match_from_routes(routes, path, opts)
481+
if route then
482+
radix.radix_tree_stop(it)
483+
return route
488484
end
489485
end
490486
end
491487

492488
radix.radix_tree_stop(it)
493-
494-
if #matched_routes == 0 then
495-
return nil
496-
end
497-
498-
sort_tab(matched_routes, sort_route)
499-
500-
for _, route in ipairs(matched_routes) do
501-
if match_route_opts(route, opts) then
502-
return route
503-
end
504-
end
505-
506489
return nil
507490
end
508491

0 commit comments

Comments
 (0)