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

🚀 Improve routing behavior #715

Merged
merged 14 commits into from
Aug 10, 2020

Conversation

ReneWerner87
Copy link
Member

@ReneWerner87 ReneWerner87 commented Aug 10, 2020

🌵 hybrid routing with tree and fast router:
for performance reasons, a new layer has been added to reduce the number of routes to be checked
NEW:

Benchmark_Router_NotFound-12                  4345894       249 ns/op      80 B/op       2 allocs/op
Benchmark_Router_NotFound-12                  5195691       252 ns/op      80 B/op       2 allocs/op
Benchmark_Router_NotFound-12                  4069700       262 ns/op      80 B/op       2 allocs/op
Benchmark_Router_Handler-12                   6323901       200 ns/op      16 B/op       1 allocs/op
Benchmark_Router_Handler-12                   5701125       207 ns/op      16 B/op       1 allocs/op
Benchmark_Router_Handler-12                   5515296       204 ns/op      16 B/op       1 allocs/op
Benchmark_Router_Handler_Strict_Case-12       8015640       163 ns/op       0 B/op       0 allocs/op
Benchmark_Router_Handler_Strict_Case-12       6137475       166 ns/op       0 B/op       0 allocs/op
Benchmark_Router_Handler_Strict_Case-12       6816235       161 ns/op       0 B/op       0 allocs/op
Benchmark_Router_Chain-12                     7098813       181 ns/op       1 B/op       1 allocs/op
Benchmark_Router_Chain-12                     5729186       184 ns/op       1 B/op       1 allocs/op
Benchmark_Router_Chain-12                     6193328       193 ns/op       1 B/op       1 allocs/op
Benchmark_Router_WithCompression-12           7151931       210 ns/op       1 B/op       1 allocs/op
Benchmark_Router_WithCompression-12           5523699       253 ns/op       1 B/op       1 allocs/op
Benchmark_Router_WithCompression-12           5808300       196 ns/op       1 B/op       1 allocs/op
Benchmark_Router_Next-12                      6674808       168 ns/op       0 B/op       0 allocs/op
Benchmark_Router_Next-12                      7946442       170 ns/op       0 B/op       0 allocs/op
Benchmark_Router_Next-12                      6494916       181 ns/op       0 B/op       0 allocs/op
Benchmark_Router_Handler_CaseSensitive-12     5816659       181 ns/op       0 B/op       0 allocs/op
Benchmark_Router_Handler_CaseSensitive-12     6736150       184 ns/op       0 B/op       0 allocs/op
Benchmark_Router_Handler_CaseSensitive-12     5855480       177 ns/op       0 B/op       0 allocs/op
Benchmark_Router_Handler_StrictRouting-12     5917713       171 ns/op       0 B/op       0 allocs/op
Benchmark_Router_Handler_StrictRouting-12     7801450       172 ns/op       0 B/op       0 allocs/op
Benchmark_Router_Handler_StrictRouting-12     6779619       185 ns/op       0 B/op       0 allocs/op

OLD:

Benchmark_Router_NotFound-12                   180708      6370 ns/op      81 B/op       2 allocs/op
Benchmark_Router_NotFound-12                   182854      6283 ns/op      81 B/op       2 allocs/op
Benchmark_Router_NotFound-12                   186518      6678 ns/op      81 B/op       2 allocs/op
Benchmark_Router_Handler-12                   1287981       932 ns/op      16 B/op       1 allocs/op
Benchmark_Router_Handler-12                   1382036       874 ns/op      16 B/op       1 allocs/op
Benchmark_Router_Handler-12                   1323716       939 ns/op      16 B/op       1 allocs/op
Benchmark_Router_Handler_Strict_Case-12       1359414       861 ns/op       0 B/op       0 allocs/op
Benchmark_Router_Handler_Strict_Case-12       1343726       816 ns/op       0 B/op       0 allocs/op
Benchmark_Router_Handler_Strict_Case-12       1398574       833 ns/op       0 B/op       0 allocs/op
Benchmark_Router_Chain-12                     8485028       151 ns/op       1 B/op       1 allocs/op
Benchmark_Router_Chain-12                     7041121       159 ns/op       1 B/op       1 allocs/op
Benchmark_Router_Chain-12                     8779014       145 ns/op       1 B/op       1 allocs/op
Benchmark_Router_WithCompression-12           7582238       162 ns/op       1 B/op       1 allocs/op
Benchmark_Router_WithCompression-12           7094360       151 ns/op       1 B/op       1 allocs/op
Benchmark_Router_WithCompression-12           8705498       161 ns/op       1 B/op       1 allocs/op
Benchmark_Router_Next-12                      1387620       854 ns/op       0 B/op       0 allocs/op
Benchmark_Router_Next-12                      1495340       841 ns/op       0 B/op       0 allocs/op
Benchmark_Router_Next-12                      1441519       897 ns/op       0 B/op       0 allocs/op
Benchmark_Router_Handler_CaseSensitive-12     1362174       894 ns/op       0 B/op       0 allocs/op
Benchmark_Router_Handler_CaseSensitive-12     1443130       830 ns/op       0 B/op       0 allocs/op
Benchmark_Router_Handler_CaseSensitive-12     1422104       811 ns/op       0 B/op       0 allocs/op
Benchmark_Router_Handler_StrictRouting-12     1421395       856 ns/op       0 B/op       0 allocs/op
Benchmark_Router_Handler_StrictRouting-12     1412744       939 ns/op       0 B/op       0 allocs/op
Benchmark_Router_Handler_StrictRouting-12     1360618       834 ns/op       0 B/op       0 allocs/op

🐞 Benchmark_Router_Handler_Unescape
-> there was a bug in benchmark, which caused the slow "methodNotAllowed" logic to be addressed

NEW:
Benchmark_Router_Handler_Unescape-12          3236104       359 ns/op       8 B/op       1 allocs/op
Benchmark_Router_Handler_Unescape-12          3056844       350 ns/op       8 B/op       1 allocs/op
Benchmark_Router_Handler_Unescape-12          4107015       330 ns/op       8 B/op       1 allocs/op
OLD:
Benchmark_Router_Handler_Unescape-12           218133      6616 ns/op      17 B/op       1 allocs/op
Benchmark_Router_Handler_Unescape-12           157705      6470 ns/op      17 B/op       1 allocs/op
Benchmark_Router_Handler_Unescape-12           189160      6358 ns/op      17 B/op       1 allocs/op

🚀 improve routing behavior:
#707
routing behaviour has been rewritten, simplified and made more readable, additionally the greedy logic has been improved.
-> the greedy logic is a bit slower, but more correct, should be bearable

Now something like this is possible 🔥

"/shop/product/::filter/color::color/size::size"
"/api/v1/:param/+"
"/test:sign:param"
"/:param1:param2?:param3"
"/test:optional?:mandatory"
"/test:optional?:optional2?"
"/foo:param?bar"
"/foo*bar"
"/foo+bar"
"/a*cde*g/"
"/*v1*/proxy"
"/foo***bar"
"/name::name"
"/@:name"
"/-:name"
"/.:name"
"/config/+.json"

🗽The logic for parsing the route now better recognizes the parts which are not parameters, so the parameter can be placed anywhere, it is only important that the normal non-optional parameters are terminated by a delimiter character:

"/test::param/"

➕ Added support for the plus parameter, this is greedy like the wildcard parameter with the difference that it is required:

route: "/config/+.json"
request uris:
/config/abc.json -> match
/config/.json    -> no match

access per ctx.Params("+") or ctx.Params("+1")

🌟 ✨ Support for multiple wildcard and plus parameters has been added, they can now be accessed via wild or plus position in the route or normally for the first as in the current fiber version:

// request uri -> /customer/v1/cart/proxy
app.Get("/*v1*/proxy", func(c *Ctx) {
	utils.AssertEqual(t, "customer/", c.Params("*1"))
	utils.AssertEqual(t, "/cart", c.Params("*2"))
        // old way is still possible
	utils.AssertEqual(t, "customer/", c.Params("*"))
})

🚨 And some more testcases for the route parser....

❗ attention 3 routing cases are still open, these should be negligible, because they are not used in the real world, but they are necessary to reproduce the express behaviour

@ReneWerner87 ReneWerner87 requested a review from a team as a code owner August 10, 2020 09:20
@ReneWerner87 ReneWerner87 changed the title Improve route behavior 🚀 improve routing behavior Aug 10, 2020
@ReneWerner87 ReneWerner87 changed the title 🚀 improve routing behavior 🚀 Improve routing behavior Aug 10, 2020
@Fenny Fenny merged commit 0d31dc1 into gofiber:master Aug 10, 2020
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.

2 participants