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

feat: regex pattern in variables #36

Merged
merged 2 commits into from
Mar 1, 2024
Merged

feat: regex pattern in variables #36

merged 2 commits into from
Mar 1, 2024

Conversation

vm-001
Copy link
Owner

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

Summary

Allows regular expression to be defined in variables. The limitation of the current implementation is that it does not allow defining a regex pattern crossing multiple URL segments, for example, {var:[/0-9a-z]+}.

Usage:

local Router = require "radix-router"
local router = Router.new({
  {
    paths = { "/users/{id:\\d+}/profile-{year:\\d{4}}.{format:(html|pdf)}" },
    handler = "1"
  },
  {
    paths = { "/users/{uuid:[0-9a-f]{8}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{12}}" },
    handler = "2"
  },
})
assert("1" == router:match("/users/100/profile-2024.pdf"))
assert("2" == router:match("/users/00000000-0000-0000-0000-000000000000"))

Benchmark

From the benchmark result, the simple regex is only ~1x slower than the simple variable.

test case route number ns/op OPS RSS
static path 100000 0.0171333 58,365,872 48.69 MB
simple variable 100000 0.0844033 11,847,877 99.97 MB
simple variable 1000000 0.087095 11,481,675 1000.41 MB
simple prefix 100000 0.0730344 13,692,177 99.92 MB
simple regex 100000 0.14444 6,923,289 126.64 MB
complex variable 100000 0.858975 1,164,178 140.08 MB
simple variable binding 100000 0.1843245 5,425,214 99.94 MB
github 609 0.38436 2,601,727 2.69 MB
Expand output
RADIX_ROUTER_ROUTES=100000 RADIX_ROUTER_TIMES=10000000 luajit benchmark/static-paths.lua
========== static path ==========
routes  :	100000
times   :	10000000
elapsed :	0.171333 s
QPS     :	58365872
ns/op   :	0.0171333 ns
path    :	/50000
handler :	50000
Memory  :	48.69 MB

RADIX_ROUTER_ROUTES=100000 RADIX_ROUTER_TIMES=10000000 luajit benchmark/simple-variable.lua
========== variable ==========
routes  :	100000
times   :	10000000
elapsed :	0.844033 s
QPS     :	11847877
ns/op   :	0.0844033 ns
path    :	/1/foo
handler :	1
Memory  :	99.97 MB

RADIX_ROUTER_ROUTES=1000000 RADIX_ROUTER_TIMES=10000000 luajit benchmark/simple-variable.lua
========== variable ==========
routes  :	1000000
times   :	10000000
elapsed :	0.870953 s
QPS     :	11481675
ns/op   :	0.0870953 ns
path    :	/1/foo
handler :	1
Memory  :	1000.41 MB

RADIX_ROUTER_ROUTES=100000 RADIX_ROUTER_TIMES=10000000 luajit benchmark/simple-prefix.lua
========== prefix ==========
routes  :	100000
times   :	10000000
elapsed :	0.730344 s
QPS     :	13692177
ns/op   :	0.0730344 ns
path    :	/1/a
handler :	1
Memory  :	99.92 MB

RADIX_ROUTER_ROUTES=100000 RADIX_ROUTER_TIMES=1000000 luajit benchmark/simple-regex.lua
========== regex ==========
routes  :	100000
times   :	1000000
elapsed :	0.14444 s
QPS     :	6923289
ns/op   :	0.14444 ns
path    :	/1/a
handler :	1
Memory  :	126.64 MB

RADIX_ROUTER_ROUTES=100000 RADIX_ROUTER_TIMES=1000000 luajit benchmark/complex-variable.lua
========== variable ==========
routes  :	100000
times   :	1000000
elapsed :	0.858975 s
QPS     :	1164178
ns/op   :	0.858975 ns
path    :	/aa/bb/cc/dd/ee/ff/gg/hh/ii/jj/kk/ll/mm/nn/oo/pp/qq/rr/ss/tt/uu/vv/ww/xx/yy/zz50000
handler :	50000
Memory  :	140.08 MB

RADIX_ROUTER_ROUTES=100000 RADIX_ROUTER_TIMES=10000000 luajit benchmark/simple-variable-binding.lua
========== variable ==========
routes  :	100000
times   :	10000000
elapsed :	1.843245 s
QPS     :	5425214
ns/op   :	0.1843245 ns
path    :	/1/foo
handler :	1
params : name = foo
Memory  :	99.94 MB

RADIX_ROUTER_TIMES=1000000 luajit benchmark/github-routes.lua
========== github apis ==========
routes  :	609
times   :	1000000
elapsed :	0.38436 s
QPS     :	2601727
ns/op   :	0.38436 ns
path    :	/repos/vm-001/lua-radix-router/import
handler :	/repos/{owner}/{repo}/import
Memory  :	2.69 MB

@vm-001 vm-001 marked this pull request as draft January 28, 2024 16:02
@vm-001 vm-001 closed this Jan 28, 2024
@vm-001 vm-001 reopened this Jan 30, 2024
@vm-001 vm-001 changed the title wip feat: regular expression in variable Jan 30, 2024
@vm-001 vm-001 force-pushed the feat/regex branch 3 times, most recently from 82feba5 to bbe22f7 Compare February 26, 2024 16:34
@vm-001 vm-001 marked this pull request as ready for review February 27, 2024 17:10
@vm-001 vm-001 force-pushed the feat/regex branch 4 times, most recently from c968048 to d885a70 Compare February 29, 2024 17:40
@vm-001 vm-001 changed the title feat: regular expression in variable feat: regex pattern in variables Mar 1, 2024
@vm-001 vm-001 merged commit 06a5e1c into main Mar 1, 2024
9 checks passed
@vm-001 vm-001 deleted the feat/regex branch March 1, 2024 13:26
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.

1 participant