@marcel91200 / caddy Mirror · read-only / commits / 53ececda
Fork 0

Fast and extensible multi-platform HTTP/1-2-3 web server with automatic HTTPS

Mirrored from https://github.com/Slazee/caddy.git

Synced Nov 14, 05:15 UTC

caddyhttp: performance improvement in HeaderRE Matcher (#4143)

Below is the report using `benchstat` and cmd: `go test -run=BenchmarkHeaderREMatcher -bench=BenchmarkHeaderREMatcher -benchmem -count=10` ``` name old time/op new time/op delta HeaderREMatcher-16 869ns ± 1% 658ns ± 0% -24.29% (p=0.000 n=10+10) name old alloc/op new alloc/op delta HeaderREMatcher-16 144B ± 0% 112B ± 0% -22.22% (p=0.000 n=10+10) name old allocs/op new allocs/op delta HeaderREMatcher-16 7.00 ± 0% 5.00 ± 0% -28.57% (p=0.000 n=10+10) ```

Calvin Xiao committed May 3, 2021 at 00:35 UTC 53ececda21f77d000ee893bc981d6ec5c33872f8
2 files changed +29 -2
modules/caddyhttp/matchers.go
+3 -2
@@ -24,6 +24,7 @@ import (
24 "path/filepath"
25 "regexp"
26 "sort"
27 + "strconv"
28 "strings"
29
30 "github.com/caddyserver/caddy/v2"
@@ -935,14 +936,14 @@ func (mre *MatchRegexp) Match(input string, repl *caddy.Replacer) bool {
936
937 // save all capture groups, first by index
938 for i, match := range matches {
938 - key := fmt.Sprintf("%s.%d", mre.phPrefix, i)
939 + key := mre.phPrefix + "." + strconv.Itoa(i)
940 repl.Set(key, match)
941 }
942
943 // then by name
944 for i, name := range mre.compiled.SubexpNames() {
945 if i != 0 && name != "" {
945 - key := fmt.Sprintf("%s.%s", mre.phPrefix, name)
946 + key := mre.phPrefix + "." + name
947 repl.Set(key, matches[i])
948 }
949 }
modules/caddyhttp/matchers_test.go
+26
@@ -692,6 +692,32 @@ func TestHeaderREMatcher(t *testing.T) {
692 }
693 }
694
695 +func BenchmarkHeaderREMatcher(b *testing.B) {
696 +
697 + i := 0
698 + match := MatchHeaderRE{"Field": &MatchRegexp{Pattern: "^foo(.*)$", Name: "name"}}
699 + input := http.Header{"Field": []string{"foobar"}}
700 + var host string
701 + err := match.Provision(caddy.Context{})
702 + if err != nil {
703 + b.Errorf("Test %d %v: Provisioning: %v", i, match, err)
704 + }
705 + err = match.Validate()
706 + if err != nil {
707 + b.Errorf("Test %d %v: Validating: %v", i, match, err)
708 + }
709 +
710 + // set up the fake request and its Replacer
711 + req := &http.Request{Header: input, URL: new(url.URL), Host: host}
712 + repl := caddy.NewReplacer()
713 + ctx := context.WithValue(req.Context(), caddy.ReplacerCtxKey, repl)
714 + req = req.WithContext(ctx)
715 + addHTTPVarsToReplacer(repl, req, httptest.NewRecorder())
716 + for run := 0; run < b.N; run++ {
717 + match.Match(req)
718 + }
719 +}
720 +
721 func TestVarREMatcher(t *testing.T) {
722 for i, tc := range []struct {
723 desc string