Skip to content

Commit 1295b74

Browse files
kennygrantbradfitz
authored andcommitted
net/http: improve speed of default mux
The DefaultServeMux included in net/http uses a map to store routes, but iterates all keys for every request to allow longer paths. This change checks the map for an exact match first. To check performance was better, BenchmarkServeMux has been added - this adds >100 routes and checks the matches. Exact matches are faster and more predictable on this benchmark and on most existing package benchmarks. https://perf.golang.org/search?q=upload:20170312.1 ServeMux-4 2.02ms ± 2% 0.04ms ± 2% −98.08% (p=0.004 n=5+6) https://perf.golang.org/search?q=upload:20170312.2 ReadRequestChrome-4 184MB/s ± 8% 186MB/s ± 1% ~ ReadRequestCurl-4 45.0MB/s ± 1% 46.2MB/s ± 1% +2.71% Read...Apachebench-4 45.8MB/s ±13% 48.7MB/s ± 1% ~ ReadRequestSiege-4 63.6MB/s ± 5% 69.2MB/s ± 1% +8.75% ReadRequestWrk-4 30.9MB/s ± 9% 34.4MB/s ± 2% +11.25% Change-Id: I8afafcb956f07197419d545a9f1c03ecaa307384 Reviewed-on: https://go-review.googlesource.com/38057 Reviewed-by: Brad Fitzpatrick <[email protected]> Run-TryBot: Brad Fitzpatrick <[email protected]> TryBot-Result: Gobot Gobot <[email protected]>
1 parent b5f81ea commit 1295b74

File tree

2 files changed

+50
-2
lines changed

2 files changed

+50
-2
lines changed

src/net/http/serve_test.go

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -460,6 +460,47 @@ func TestMuxRedirectLeadingSlashes(t *testing.T) {
460460
}
461461
}
462462

463+
func BenchmarkServeMux(b *testing.B) {
464+
465+
type test struct {
466+
path string
467+
code int
468+
req *Request
469+
}
470+
471+
// Build example handlers and requests
472+
var tests []test
473+
endpoints := []string{"search", "dir", "file", "change", "count", "s"}
474+
for _, e := range endpoints {
475+
for i := 200; i < 230; i++ {
476+
p := fmt.Sprintf("/%s/%d/", e, i)
477+
tests = append(tests, test{
478+
path: p,
479+
code: i,
480+
req: &Request{Method: "GET", Host: "localhost", URL: &url.URL{Path: p}},
481+
})
482+
}
483+
}
484+
mux := NewServeMux()
485+
for _, tt := range tests {
486+
mux.Handle(tt.path, serve(tt.code))
487+
}
488+
489+
rw := httptest.NewRecorder()
490+
b.ReportAllocs()
491+
b.ResetTimer()
492+
for i := 0; i < b.N; i++ {
493+
for _, tt := range tests {
494+
*rw = httptest.ResponseRecorder{}
495+
h, pattern := mux.Handler(tt.req)
496+
h.ServeHTTP(rw, tt.req)
497+
if pattern != tt.path || rw.Code != tt.code {
498+
b.Fatalf("got %d, %q, want %d, %q", rw.Code, pattern, tt.code, tt.path)
499+
}
500+
}
501+
}
502+
}
503+
463504
func TestServerTimeouts(t *testing.T) {
464505
setParallel(t)
465506
defer afterTest(t)

src/net/http/server.go

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2167,9 +2167,16 @@ func cleanPath(p string) string {
21672167
return np
21682168
}
21692169

2170-
// Find a handler on a handler map given a path string
2171-
// Most-specific (longest) pattern wins
2170+
// Find a handler on a handler map given a path string.
2171+
// Most-specific (longest) pattern wins.
21722172
func (mux *ServeMux) match(path string) (h Handler, pattern string) {
2173+
// Check for exact match first.
2174+
v, ok := mux.m[path]
2175+
if ok {
2176+
return v.h, v.pattern
2177+
}
2178+
2179+
// Check for longest valid match.
21732180
var n = 0
21742181
for k, v := range mux.m {
21752182
if !pathMatch(k, path) {

0 commit comments

Comments
 (0)