A radix-tree based router. No-allocation lookups for extra speed! https://git.akyoto.dev/go/router
Go to file
2024-09-06 08:17:10 -05:00
tests Reimplement generics 2024-09-06 08:16:32 -05:00
.gitignore Initial commit 2024-08-22 21:08:25 -05:00
flow.go rename files 2024-09-05 13:04:17 -05:00
go.mod initial commit 2024-08-22 21:46:21 -05:00
LICENSE Update license 2024-09-05 13:11:17 -05:00
parameter.go rename files 2024-09-05 13:04:17 -05:00
README.md Fix readme example 2024-09-06 08:17:10 -05:00
router.go Reimplement generics 2024-09-06 08:16:32 -05:00
tree.go Reimplement generics 2024-09-06 08:16:32 -05:00
treeNode.go Reimplement generics 2024-09-06 08:16:32 -05:00

Router

A radix-tree based no-allocation router in Go. All credit to Eduard Urbach for his incredible work. This router sports a fancy PATRICIA tree structure for efficient string lookups. It also has zero dependencies!

The router has a generic data structure, so any kind of handler can be used.

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

License

Licensed under MIT. Take a look!

Credit

All of the code here is built by Eduard Urbach.