plugin/pkg/replacer: fix usage of sync.Pool to save an alloc (#7701)

This commit is contained in:
Charlie Vieth
2025-11-21 16:41:58 -05:00
committed by GitHub
parent d3e13fe05d
commit 854048b0ab

View File

@@ -257,12 +257,14 @@ func loadFormat(s string) replacer {
// bufPool stores pointers to scratch buffers. // bufPool stores pointers to scratch buffers.
var bufPool = sync.Pool{ var bufPool = sync.Pool{
New: func() any { New: func() any {
return make([]byte, 0, 256) b := make([]byte, 0, 256)
return &b
}, },
} }
func (r replacer) Replace(ctx context.Context, state request.Request, rr *dnstest.Recorder) string { func (r replacer) Replace(ctx context.Context, state request.Request, rr *dnstest.Recorder) string {
b := bufPool.Get().([]byte) p := bufPool.Get().(*[]byte)
b := *p
for _, s := range r { for _, s := range r {
switch s.typ { switch s.typ {
case typeLabel: case typeLabel:
@@ -278,7 +280,7 @@ func (r replacer) Replace(ctx context.Context, state request.Request, rr *dnstes
} }
} }
s := string(b) s := string(b)
//nolint:staticcheck *p = b[:0]
bufPool.Put(b[:0]) bufPool.Put(p)
return s return s
} }