refactor: use strings.Builder to improve performance

Signed-off-by: zjumathcode <pai314159@2980.com>
This commit is contained in:
zjumathcode 2025-11-25 14:54:52 +08:00
parent 67a9e0657e
commit 269d616a79
3 changed files with 18 additions and 15 deletions

View file

@ -244,15 +244,15 @@ func DisabledTest(t *testing.T) {
sendFcgi(0, fcgiParams, []byte("c4ca4238a0b923820dcc509a6f75849b=1&7b8b965ad4bca0e41ab51de7b31363a1=n"), nil, nil)
log.Println("test:", "post data (more than 60KB)")
data := ""
var data strings.Builder
for i := 0x00; i < 0xff; i++ {
v0 := strings.Repeat(fmt.Sprint(i), 256)
h := md5.New()
_, _ = io.WriteString(h, v0)
k0 := fmt.Sprintf("%x", h.Sum(nil))
data += k0 + "=" + url.QueryEscape(v0) + "&"
data.WriteString(k0 + "=" + url.QueryEscape(v0) + "&")
}
sendFcgi(0, fcgiParams, []byte(data), nil, nil)
sendFcgi(0, fcgiParams, []byte(data.String()), nil, nil)
log.Println("test:", "post form (use url.Values)")
p0 := make(map[string]string, 1)

View file

@ -18,6 +18,7 @@ import (
"encoding/json"
"fmt"
"net/http"
"strings"
"github.com/caddyserver/caddy/v2"
)
@ -110,14 +111,15 @@ func (r Route) Empty() bool {
}
func (r Route) String() string {
handlersRaw := "["
var handlersRaw strings.Builder
handlersRaw.WriteString("[")
for _, hr := range r.HandlersRaw {
handlersRaw += " " + string(hr)
handlersRaw.WriteString(" " + string(hr))
}
handlersRaw += "]"
handlersRaw.WriteString("]")
return fmt.Sprintf(`{Group:"%s" MatcherSetsRaw:%s HandlersRaw:%s Terminal:%t}`,
r.Group, r.MatcherSetsRaw, handlersRaw, r.Terminal)
r.Group, r.MatcherSetsRaw, handlersRaw.String(), r.Terminal)
}
// Provision sets up both the matchers and handlers in the route.
@ -440,13 +442,14 @@ func (ms *MatcherSets) FromInterface(matcherSets any) error {
// TODO: Is this used?
func (ms MatcherSets) String() string {
result := "["
var result strings.Builder
result.WriteString("[")
for _, matcherSet := range ms {
for _, matcher := range matcherSet {
result += fmt.Sprintf(" %#v", matcher)
result.WriteString(fmt.Sprintf(" %#v", matcher))
}
}
return result + " ]"
return result.String() + " ]"
}
var routeGroupCtxKey = caddy.CtxKey("route_group")

View file

@ -255,7 +255,7 @@ func (m IPMaskFilter) Filter(in zapcore.Field) zapcore.Field {
}
func (m IPMaskFilter) mask(s string) string {
output := ""
var output strings.Builder
for value := range strings.SplitSeq(s, ",") {
value = strings.TrimSpace(value)
host, port, err := net.SplitHostPort(value)
@ -264,7 +264,7 @@ func (m IPMaskFilter) mask(s string) string {
}
ipAddr := net.ParseIP(host)
if ipAddr == nil {
output += value + ", "
output.WriteString(value + ", ")
continue
}
mask := m.v4Mask
@ -273,13 +273,13 @@ func (m IPMaskFilter) mask(s string) string {
}
masked := ipAddr.Mask(mask)
if port == "" {
output += masked.String() + ", "
output.WriteString(masked.String() + ", ")
continue
}
output += net.JoinHostPort(masked.String(), port) + ", "
output.WriteString(net.JoinHostPort(masked.String(), port) + ", ")
}
return strings.TrimSuffix(output, ", ")
return strings.TrimSuffix(output.String(), ", ")
}
type filterAction string