@marcel91200 / caddy Mirror · read-only / commits / 68d50020
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 11, 16:00 UTC

refactor: use strings.Builder to improve performance (#7364)

* refactor: use strings.Builder to improve performance Signed-off-by: zjumathcode <pai314159@2980.com> * refactor: small builder improvements per review (WriteByte / split writes) also revert builder change in client_test.go refactor(logging): build IP mask output via join of parts (more efficient) --------- Signed-off-by: zjumathcode <pai314159@2980.com> Co-authored-by: Francis Lavoie <lavofr@gmail.com>

zjumathcode committed Feb 17, 2026 at 03:30 UTC 68d50020eef0d4c3398b878f17c8092ca5b58ca0
2 files changed +17 -12
modules/caddyhttp/routes.go
+12 -7
@@ -18,6 +18,7 @@ import (
18 "encoding/json"
19 "fmt"
20 "net/http"
21 + "strings"
22
23 "github.com/caddyserver/caddy/v2"
24 )
@@ -110,14 +111,16 @@ func (r Route) Empty() bool {
111 }
112
113 func (r Route) String() string {
113 - handlersRaw := "["
114 + var handlersRaw strings.Builder
115 + handlersRaw.WriteByte('[')
116 for _, hr := range r.HandlersRaw {
115 - handlersRaw += " " + string(hr)
117 + handlersRaw.WriteByte(' ')
118 + handlersRaw.WriteString(string(hr))
119 }
117 - handlersRaw += "]"
120 + handlersRaw.WriteByte(']')
121
122 return fmt.Sprintf(`{Group:"%s" MatcherSetsRaw:%s HandlersRaw:%s Terminal:%t}`,
120 - r.Group, r.MatcherSetsRaw, handlersRaw, r.Terminal)
123 + r.Group, r.MatcherSetsRaw, handlersRaw.String(), r.Terminal)
124 }
125
126 // Provision sets up both the matchers and handlers in the route.
@@ -440,13 +443,15 @@ func (ms *MatcherSets) FromInterface(matcherSets any) error {
443
444 // TODO: Is this used?
445 func (ms MatcherSets) String() string {
443 - result := "["
446 + var result strings.Builder
447 + result.WriteByte('[')
448 for _, matcherSet := range ms {
449 for _, matcher := range matcherSet {
446 - result += fmt.Sprintf(" %#v", matcher)
450 + result.WriteString(fmt.Sprintf(" %#v", matcher))
451 }
452 }
449 - return result + " ]"
453 + result.WriteByte(']')
454 + return result.String()
455 }
456
457 var routeGroupCtxKey = caddy.CtxKey("route_group")
modules/logging/filters.go
+5 -5
@@ -255,7 +255,7 @@ func (m IPMaskFilter) Filter(in zapcore.Field) zapcore.Field {
255 }
256
257 func (m IPMaskFilter) mask(s string) string {
258 - output := ""
258 + parts := make([]string, 0)
259 for value := range strings.SplitSeq(s, ",") {
260 value = strings.TrimSpace(value)
261 host, port, err := net.SplitHostPort(value)
@@ -264,7 +264,7 @@ func (m IPMaskFilter) mask(s string) string {
264 }
265 ipAddr := net.ParseIP(host)
266 if ipAddr == nil {
267 - output += value + ", "
267 + parts = append(parts, value)
268 continue
269 }
270 mask := m.v4Mask
@@ -273,13 +273,13 @@ func (m IPMaskFilter) mask(s string) string {
273 }
274 masked := ipAddr.Mask(mask)
275 if port == "" {
276 - output += masked.String() + ", "
276 + parts = append(parts, masked.String())
277 continue
278 }
279
280 - output += net.JoinHostPort(masked.String(), port) + ", "
280 + parts = append(parts, net.JoinHostPort(masked.String(), port))
281 }
282 - return strings.TrimSuffix(output, ", ")
282 + return strings.Join(parts, ", ")
283 }
284
285 type filterAction string