Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
perf: clone request URL struct directly for path manipulation
Save 4 allocations worth 200B per request cycle with redirect. The rewrite operation takes before O(600ns) vs after O(200ns). ``` $ go test -benchmem -benchtime 5000000x -bench BenchmarkStrictSlash goos: linux goarch: amd64 pkg: github.com/gorilla/mux cpu: Intel(R) Core(TM) i7-8550U CPU @ 1.80GHz BenchmarkStrictSlashClone-8 5000000 183.9 ns/op 64 B/op 4 allocs/op BenchmarkStrictSlashParse-8 5000000 559.8 ns/op 264 B/op 8 allocs/op PASS ok github.com/gorilla/mux 3.740s $ go test -benchmem -benchtime 5000000x -bench BenchmarkStrictSlash goos: linux goarch: amd64 pkg: github.com/gorilla/mux cpu: Intel(R) Core(TM) i7-8550U CPU @ 1.80GHz BenchmarkStrictSlashClone-8 5000000 180.4 ns/op 64 B/op 4 allocs/op BenchmarkStrictSlashParse-8 5000000 573.5 ns/op 264 B/op 8 allocs/op PASS ok github.com/gorilla/mux 3.788s $ go test -benchmem -benchtime 5000000x -bench BenchmarkStrictSlash goos: linux goarch: amd64 pkg: github.com/gorilla/mux cpu: Intel(R) Core(TM) i7-8550U CPU @ 1.80GHz BenchmarkStrictSlashClone-8 5000000 175.8 ns/op 64 B/op 4 allocs/op BenchmarkStrictSlashParse-8 5000000 569.4 ns/op 264 B/op 8 allocs/op PASS ok github.com/gorilla/mux 3.744s ``` ``` func BenchmarkStrictSlashClone(b *testing.B) { req := newRequest(http.MethodGet, "http://localhost/x") b.ResetTimer() for i := 0; i < b.N; i++ { _ = replaceURLPath(req.URL, req.URL.Path+"/") } } func BenchmarkStrictSlashParse(b *testing.B) { req := newRequest(http.MethodGet, "http://localhost/x") b.ResetTimer() for i := 0; i < b.N; i++ { u, _ := url.Parse(req.URL.String()) u.Path += "/" _ = u.String() } } ``` Signed-off-by: Jakob Ackermann <[email protected]>
- Loading branch information