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

Fix routing conflict for dynamic routes and static route with common prefix (#1509) #1512

Merged
merged 3 commits into from
Feb 24, 2020

Conversation

lammel
Copy link
Contributor

@lammel lammel commented Feb 19, 2020

This PR resolves issue #1509

A routing conflict is caused by radix tree node splits on common prefix of static routes together with a param route on the same level. An additional test has been added for this issue.

This patch actually ensures that routing will continue back up the route tree in that case.

@codecov
Copy link

codecov bot commented Feb 19, 2020

Codecov Report

Merging #1512 into master will not change coverage.
The diff coverage is 100%.

Impacted file tree graph

@@           Coverage Diff           @@
##           master    #1512   +/-   ##
=======================================
  Coverage   84.54%   84.54%           
=======================================
  Files          27       27           
  Lines        2097     2097           
=======================================
  Hits         1773     1773           
  Misses        211      211           
  Partials      113      113
Impacted Files Coverage Δ
router.go 97.13% <100%> (ø) ⬆️

Continue to review full report at Codecov.

Legend - Click here to learn more
Δ = absolute <relative> (impact), ø = not affected, ? = missing data
Powered by Codecov. Last update f4b5a90...f4e3d21. Read the comment docs.

@lammel
Copy link
Contributor Author

lammel commented Feb 19, 2020

This PR has an impact on the routing performance of ~15% in the worst case.
Numbers are taken using the available test benchmarks on my local system.

Before the patch (master, f4b5a90):

sh# /usr/bin/go test -benchmem -run=^$ github.com/labstack/echo/v4 -bench ^(BenchmarkRouterStaticRoutes|BenchmarkRouterGitHubAPI|BenchmarkRouterParseAPI|BenchmarkRouterGooglePlusAPI)$

goos: linux
goarch: amd64
pkg: github.com/labstack/echo/v4
BenchmarkRouterStaticRoutes-16     	   66543	     17475 ns/op	       0 B/op	       0 allocs/op
BenchmarkRouterGitHubAPI-16        	   31058	     38630 ns/op	       2 B/op	       0 allocs/op
BenchmarkRouterParseAPI-16         	  174482	      5884 ns/op	       0 B/op	       0 allocs/op
BenchmarkRouterGooglePlusAPI-16    	  123810	      9144 ns/op	       0 B/op	       0 allocs/op
PASS
ok  	github.com/labstack/echo/v4	5.274s

With this patch applied:

sh# /usr/bin/go test -benchmem -run=^$ github.com/labstack/echo/v4 -bench ^(BenchmarkRouterStaticRoutes|BenchmarkRouterGitHubAPI|BenchmarkRouterParseAPI|BenchmarkRouterGooglePlusAPI)$

goos: linux
goarch: amd64
pkg: github.com/labstack/echo/v4
BenchmarkRouterStaticRoutes-16     	   56984	     20068 ns/op	       1 B/op	       0 allocs/op
BenchmarkRouterGitHubAPI-16        	   26817	     44528 ns/op	       2 B/op	       0 allocs/op
BenchmarkRouterParseAPI-16         	  192571	      5751 ns/op	       0 B/op	       0 allocs/op
BenchmarkRouterGooglePlusAPI-16    	  100843	     11578 ns/op	       0 B/op	       0 allocs/op
PASS
ok  	github.com/labstack/echo/v4	5.476s

@lammel
Copy link
Contributor Author

lammel commented Feb 19, 2020

Managed to save some CPU cylces by avoiding backing up the tree for static only route trees (where no next param or any nodes are stored)

goos: linux
goarch: amd64
pkg: github.com/labstack/echo/v4
BenchmarkRouterStaticRoutes-16     	   63564	     18446 ns/op	       0 B/op	       0 allocs/op
BenchmarkRouterGitHubAPI-16        	   27559	     43419 ns/op	       2 B/op	       0 allocs/op
BenchmarkRouterParseAPI-16         	  194587	      5777 ns/op	       0 B/op	       0 allocs/op
BenchmarkRouterGooglePlusAPI-16    	  107343	     11036 ns/op	       0 B/op	       0 allocs/op
PASS
ok  	github.com/labstack/echo/v4	5.501s

@vishr vishr merged commit 5ddc3a6 into labstack:master Feb 24, 2020
@vishr
Copy link
Member

vishr commented Feb 24, 2020

@lammel Thanks for your work!

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