A radix-tree based router. No-allocation lookups for extra speed! https://git.akyoto.dev/go/router
Go to file
2024-08-22 22:02:27 -05:00
tests Implement benchmarks 2024-08-22 22:02:27 -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 Implement benchmarks 2024-08-22 22:02:27 -05:00
Router.go initial commit 2024-08-22 21:46:21 -05:00
Tree.go initial commit 2024-08-22 21:46:21 -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

goos: linux
goarch: amd64
pkg: git.sharkk.net/Go/Router/tests
cpu: AMD Ryzen 9 7950X 16-Core Processor            
BenchmarkBlog/Len1-Param0-32            384992013                3.337 ns/op           0 B/op          0 allocs/op
BenchmarkBlog/Len1-Param1-32            199599014                6.021 ns/op           0 B/op          0 allocs/op
BenchmarkGithub/Len7-Param0-32          256332994                4.678 ns/op           0 B/op          0 allocs/op
BenchmarkGithub/Len7-Param1-32          269038417                4.455 ns/op           0 B/op          0 allocs/op
BenchmarkGithub/Len7-Param2-32          256228226                4.673 ns/op           0 B/op          0 allocs/op
PASS
ok      git.sharkk.net/Go/Router/tests  8.410s