From f48927253fa183f81eda684da508ba9226b87e9f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Tomasz=20K=C5=82ak?= Date: Sat, 23 Jan 2016 17:42:00 +0100 Subject: [PATCH 1/2] Named groups replaced with conditional wrapping of regexps --- bench_test.go | 13 +++++++++++ regexp.go | 65 ++++++++++++++++++++++++--------------------------- 2 files changed, 44 insertions(+), 34 deletions(-) diff --git a/bench_test.go b/bench_test.go index c5f97b2..04409ab 100644 --- a/bench_test.go +++ b/bench_test.go @@ -19,3 +19,16 @@ func BenchmarkMux(b *testing.B) { router.ServeHTTP(nil, request) } } + +func BenchmarkMuxAlternativeInRegexp(b *testing.B) { + router := new(Router) + handler := func(w http.ResponseWriter, r *http.Request) {} + router.HandleFunc("/v1/{v1:(a|b)}", handler) + + requestA, _ := http.NewRequest("GET", "/v1/a", nil) + requestB, _ := http.NewRequest("GET", "/v1/b", nil) + for i := 0; i < b.N; i++ { + router.ServeHTTP(nil, requestA) + router.ServeHTTP(nil, requestB) + } +} diff --git a/regexp.go b/regexp.go index 06728dd..3c3a31b 100644 --- a/regexp.go +++ b/regexp.go @@ -73,14 +73,17 @@ func newRouteRegexp(tpl string, matchHost, matchPrefix, matchQuery, strictSlash tpl[idxs[i]:end]) } // Build the regexp pattern. - varIdx := i / 2 - fmt.Fprintf(pattern, "%s(?P<%s>%s)", regexp.QuoteMeta(raw), varGroupName(varIdx), patt) + if patt[0] == '(' && patt[len(patt)-1] == ')' { + fmt.Fprintf(pattern, "%s%s", regexp.QuoteMeta(raw), patt) + } else { + fmt.Fprintf(pattern, "%s(%s)", regexp.QuoteMeta(raw), patt) + } // Build the reverse template. fmt.Fprintf(reverse, "%s%%s", raw) // Append variable name and compiled pattern. - varsN[varIdx] = name - varsR[varIdx], err = regexp.Compile(fmt.Sprintf("^%s$", patt)) + varsN[i/2] = name + varsR[i/2], err = regexp.Compile(fmt.Sprintf("^%s$", patt)) if err != nil { return nil, err } @@ -246,30 +249,17 @@ type routeRegexpGroup struct { func (v *routeRegexpGroup) setMatch(req *http.Request, m *RouteMatch, r *Route) { // Store host variables. if v.host != nil { - hostVars := v.host.regexp.FindStringSubmatch(getHost(req)) - if hostVars != nil { - subexpNames := v.host.regexp.SubexpNames() - varName := 0 - for i, name := range subexpNames[1:] { - if name != "" && name == varGroupName(varName) { - m.Vars[v.host.varsN[varName]] = hostVars[i+1] - varName++ - } - } + host := getHost(req) + matches := v.host.regexp.FindStringSubmatchIndex(host) + if len(matches) > 0 { + extractVars(host, matches, v.host.varsN, m.Vars) } } // Store path variables. if v.path != nil { - pathVars := v.path.regexp.FindStringSubmatch(req.URL.Path) - if pathVars != nil { - subexpNames := v.path.regexp.SubexpNames() - varName := 0 - for i, name := range subexpNames[1:] { - if name != "" && name == varGroupName(varName) { - m.Vars[v.path.varsN[varName]] = pathVars[i+1] - varName++ - } - } + matches := v.path.regexp.FindStringSubmatchIndex(req.URL.Path) + if len(matches) > 0 { + extractVars(req.URL.Path, matches, v.path.varsN, m.Vars) // Check if we should redirect. if v.path.strictSlash { p1 := strings.HasSuffix(req.URL.Path, "/") @@ -288,16 +278,10 @@ func (v *routeRegexpGroup) setMatch(req *http.Request, m *RouteMatch, r *Route) } // Store query string variables. for _, q := range v.queries { - queryVars := q.regexp.FindStringSubmatch(q.getUrlQuery(req)) - if queryVars != nil { - subexpNames := q.regexp.SubexpNames() - varName := 0 - for i, name := range subexpNames[1:] { - if name != "" && name == varGroupName(varName) { - m.Vars[q.varsN[varName]] = queryVars[i+1] - varName++ - } - } + queryUrl := q.getUrlQuery(req) + matches := q.regexp.FindStringSubmatchIndex(queryUrl) + if len(matches) > 0 { + extractVars(queryUrl, matches, q.varsN, m.Vars) } } } @@ -315,3 +299,16 @@ func getHost(r *http.Request) string { return host } + +func extractVars(input string, matches []int, names []string, output map[string]string) { + matchesCount := 0 + prevEnd := -1 + for i := 2; i < len(matches) && matchesCount < len(names); i += 2 { + if prevEnd < matches[i+1] { + value := input[matches[i]:matches[i+1]] + output[names[matchesCount]] = value + prevEnd = matches[i+1] + matchesCount++ + } + } +} From 78fb8eb962166e2ed581b9e30619fc353d2758b3 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Tomasz=20K=C5=82ak?= Date: Sat, 23 Jan 2016 18:09:52 +0100 Subject: [PATCH 2/2] Added benchmark with deep path --- bench_test.go | 15 +++++++++++++++ 1 file changed, 15 insertions(+) diff --git a/bench_test.go b/bench_test.go index 04409ab..946289b 100644 --- a/bench_test.go +++ b/bench_test.go @@ -6,6 +6,7 @@ package mux import ( "net/http" + "net/http/httptest" "testing" ) @@ -32,3 +33,17 @@ func BenchmarkMuxAlternativeInRegexp(b *testing.B) { router.ServeHTTP(nil, requestB) } } + +func BenchmarkManyPathVariables(b *testing.B) { + router := new(Router) + handler := func(w http.ResponseWriter, r *http.Request) {} + router.HandleFunc("/v1/{v1}/{v2}/{v3}/{v4}/{v5}", handler) + + matchingRequest, _ := http.NewRequest("GET", "/v1/1/2/3/4/5", nil) + notMatchingRequest, _ := http.NewRequest("GET", "/v1/1/2/3/4", nil) + recorder := httptest.NewRecorder() + for i := 0; i < b.N; i++ { + router.ServeHTTP(nil, matchingRequest) + router.ServeHTTP(recorder, notMatchingRequest) + } +}