mirror of
https://github.com/caddyserver/caddy.git
synced 2026-08-31 15:51:09 +00:00
Right-size three per-request allocations that previously grew from a
nil/empty backing array, causing repeated reallocation:
- CleanPath: pre-Grow the strings.Builder used when slash collapsing is
disabled, instead of letting it reallocate as runes are appended
- headers: presize the rewritten value slice in HeaderOps.ApplyTo's Set
loop instead of appending from nil
- reverseproxy: presize the weights slice in WeightedRoundRobinSelection
.Select
This commit also adds benchmarks exercising each path. Measured with benchstat
with count=20 and GOMAXPROCS=1, with p=0.000:
sec/op B/op allocs/op
CleanPathNoCollapse 1013.4n -> 828.6n (-18.23%) 120->64 (-46.67%) 4->1 (-75.00%)
HeaderOpsApplyToSet 1262.0n -> 955.8n (-24.27%) 255->152 (-40.39%) 9->6 (-33.33%)
WeightedRoundRobinSelect 215.8n -> 200.3n ( -7.20%) 112->96 (-14.29%) 2->2 (N/A)
24 lines
495 B
Go
24 lines
495 B
Go
package headers
|
|
|
|
import (
|
|
"net/http"
|
|
"testing"
|
|
|
|
"github.com/caddyserver/caddy/v2"
|
|
)
|
|
|
|
func BenchmarkHeaderOpsApplyToSet(b *testing.B) {
|
|
ops := &HeaderOps{
|
|
Set: http.Header{
|
|
"Content-Type": []string{"text/html; charset=utf-8"},
|
|
"Cache-Control": []string{"public", "max-age=3600", "immutable"},
|
|
"X-Custom-Header": []string{"value-one", "value-two"},
|
|
},
|
|
}
|
|
repl := caddy.NewReplacer()
|
|
b.ReportAllocs()
|
|
for b.Loop() {
|
|
hdr := make(http.Header)
|
|
ops.ApplyTo(hdr, repl)
|
|
}
|
|
}
|