diff --git a/src/net/http/serve_test.go b/src/net/http/serve_test.go index c14d87dcf9..10651fff7c 100644 --- a/src/net/http/serve_test.go +++ b/src/net/http/serve_test.go @@ -608,8 +608,9 @@ func TestShouldRedirectConcurrency(t *testing.T) { mux.HandleFunc("/", func(w ResponseWriter, r *Request) {}) } -func BenchmarkServeMux(b *testing.B) { - +func BenchmarkServeMux(b *testing.B) { benchmarkServeMux(b, true) } +func BenchmarkServeMux_SkipServe(b *testing.B) { benchmarkServeMux(b, false) } +func benchmarkServeMux(b *testing.B, runHandler bool) { type test struct { path string code int @@ -641,9 +642,11 @@ func BenchmarkServeMux(b *testing.B) { for _, tt := range tests { *rw = httptest.ResponseRecorder{} h, pattern := mux.Handler(tt.req) - h.ServeHTTP(rw, tt.req) - if pattern != tt.path || rw.Code != tt.code { - b.Fatalf("got %d, %q, want %d, %q", rw.Code, pattern, tt.code, tt.path) + if runHandler { + h.ServeHTTP(rw, tt.req) + if pattern != tt.path || rw.Code != tt.code { + b.Fatalf("got %d, %q, want %d, %q", rw.Code, pattern, tt.code, tt.path) + } } } } diff --git a/src/net/http/server.go b/src/net/http/server.go index 0ac7c96de7..407546d6c9 100644 --- a/src/net/http/server.go +++ b/src/net/http/server.go @@ -2182,7 +2182,12 @@ func cleanPath(p string) string { // path.Clean removes trailing slash except for root; // put the trailing slash back if necessary. if p[len(p)-1] == '/' && np != "/" { - np += "/" + // Fast path for common case of p being the string we want: + if len(p) == len(np)+1 && strings.HasPrefix(p, np) { + np = p + } else { + np += "/" + } } return np }