Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

refactor: change router implementation to lua-radix-router for better performance #22

Merged
merged 1 commit into from
Jan 17, 2024

Conversation

vm-001
Copy link
Contributor

@vm-001 vm-001 commented Jan 13, 2024

Summary

The rax based router is not efficient when it comes to the URL scenario. Only the partial path(before the first :) is stored in radix tree, which means it may become a linear search in the worst scenario.
See https://github.com/hanxi/lua-rax/blob/7c80520074c35d1afb99fc4698f6684c9575393d/rax.lua#L75.

Based on the benchamrks, the lua-radix-router is ~70x faster than rax based router in the GitHub API test suit. See benchmarks

@vm-001 vm-001 marked this pull request as draft January 13, 2024 15:45
@vm-001 vm-001 marked this pull request as ready for review January 13, 2024 15:59
end

function M:add_route(method, absolute_path, handlers)
self.router:insert(method, absolute_path, handlers)
local path = absolute_path -- todo, converts gin style to openapi style. /users/:name -> /users/{name}
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The convert_path func will like

local function convert_path(path)
  path = string.gsub(path, ":([^/]*)", "{%1}")
  path = string.gsub(path, "%*(%w*)", "{%*%1}")
  return path
end

assert("/api/{name}/{id}" == convert_path("/api/:name/:id"))
assert("/api/{}/{*}" == convert_path("/api/:/*"))
assert("/api/{name}/{id}/{*path}" == convert_path("/api/:name/:id/*path"))

@vm-001
Copy link
Contributor Author

vm-001 commented Jan 17, 2024

Thanks for merging this PR. I look at the master code:

The matched argument can be omitted if the web framework doesn't need it. So you save a table allocation overhead in the request path, which pretty hot path.
5cd6b8a#diff-42e73a3473d988464c4af442f7f11d8ab83033d03d1495e342777a7b1d04c392R29

@huahua132
Copy link
Owner

Thanks for the reminder, I will find ways to make relevant optimizations

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

2 participants