A radix-tree based router. No-allocation lookups for extra speed! https://git.akyoto.dev/go/router
Go to file
2024-08-23 22:15:58 -05:00
tests Fix github uri bench, update comments and benchmark results 2024-08-23 22:15:58 -05:00
.gitignore Initial commit 2024-08-22 21:08:25 -05:00
Flow.go initial commit 2024-08-22 21:46:21 -05:00
go.mod initial commit 2024-08-22 21:46:21 -05:00
LICENSE Initial commit 2024-08-22 21:08:25 -05:00
Parameter.go initial commit 2024-08-22 21:46:21 -05:00
README.md Fix github uri bench, update comments and benchmark results 2024-08-23 22:15:58 -05:00
Router.go Fix github uri bench, update comments and benchmark results 2024-08-23 22:15:58 -05:00
Tree.go Fix github uri bench, update comments and benchmark results 2024-08-23 22:15:58 -05:00
TreeNode.go initial commit 2024-08-22 21:46:21 -05:00

Router

A radix-tree based no-allocation router in Go. All credit to Eduard Urbach for his incredible work.

Features

  • Efficient lookup
  • Generic data structure
  • Zero dependencies

Installation

go get git.sharkk.net/Go/Router

Usage

router := router.New[string]()

// Static routes
router.Add("GET", "/hello", "...")
router.Add("GET", "/world", "...")

// Parameter routes
router.Add("GET", "/users/:id", "...")
router.Add("GET", "/users/:id/comments", "...")

// Wildcard routes
router.Add("GET", "/images/*path", "...")

// Simple lookup
data, params := router.Lookup("GET", "/users/42")
fmt.Println(data, params)

// Efficient lookup
data := router.LookupNoAlloc("GET", "/users/42", func(key string, value string) {
	fmt.Println(key, value)
})

Benchmarks

cpu: AMD Ryzen 9 7950X 16-Core Processor

BenchmarkBlog/Len1-Params0-32         	268391120	          4.461 ns/op	       0 B/op	       0 allocs/op
BenchmarkBlog/Len1-Params0-NoAlloc-32 	383737024	          3.123 ns/op	       0 B/op	       0 allocs/op
BenchmarkBlog/Len1-Param1-32          	23690427	          47.85 ns/op	      32 B/op	       1 allocs/op
BenchmarkBlog/Len1-Param1-NoAlloc-32  	248275540	          4.913 ns/op	       0 B/op	       0 allocs/op

BenchmarkGithub/Len7-Params0-32         	148926122	      8.043 ns/op	       0 B/op	       0 allocs/op
BenchmarkGithub/Len7-Params0-NoAlloc-32 	168011829	      7.153 ns/op	       0 B/op	       0 allocs/op
BenchmarkGithub/Len7-Params1-32         	22188592	      47.25 ns/op	      32 B/op	       1 allocs/op
BenchmarkGithub/Len7-Params1-NoAlloc-32 	100000000	      11.29 ns/op	       0 B/op	       0 allocs/op
BenchmarkGithub/Len7-Params3-32         	10181496	      111.3 ns/op	      96 B/op	       2 allocs/op
BenchmarkGithub/Len7-Params3-NoAlloc-32 	46369563	      25.54 ns/op	       0 B/op	       0 allocs/op