mirror of
https://github.com/coredns/coredns.git
synced 2025-11-22 11:52:17 -05:00
This commit removes superfluous allocations of the Answer, Ns, and Extra
slices when copying a cached a dns.Msg. The allocations are superfluous
because we immediately overwrite the newly copied slices with
filterRRSlice. It also updates filterRRSlice to pre-calculate the size
of the slice being copied into.
Benchmark results:
goos: darwin
goarch: arm64
pkg: github.com/coredns/coredns/plugin/cache
cpu: Apple M4 Pro
│ base.10.txt │ new.10.txt │
│ sec/op │ sec/op vs base │
CacheResponse-14 471.1n ± 0% 462.9n ± 2% -1.74% (p=0.009 n=10)
│ base.10.txt │ new.10.txt │
│ B/op │ B/op vs base │
CacheResponse-14 672.0 ± 0% 656.0 ± 0% -2.38% (p=0.000 n=10)
│ base.10.txt │ new.10.txt │
│ allocs/op │ allocs/op vs base │
CacheResponse-14 13.00 ± 0% 12.00 ± 0% -7.69% (p=0.000 n=10)
Signed-off-by: Charlie Vieth <charlie.vieth@gmail.com>
31 lines
608 B
Go
31 lines
608 B
Go
package cache
|
|
|
|
import "github.com/miekg/dns"
|
|
|
|
// filterRRSlice filters out OPT RRs, and sets all RR TTLs to ttl.
|
|
// If dup is true the RRs in rrs are _copied_ before adjusting their
|
|
// TTL and the slice of copied RRs is returned.
|
|
func filterRRSlice(rrs []dns.RR, ttl uint32, dup bool) []dns.RR {
|
|
n := 0
|
|
for _, r := range rrs {
|
|
if r.Header().Rrtype != dns.TypeOPT {
|
|
n++
|
|
}
|
|
}
|
|
rs := make([]dns.RR, n)
|
|
j := 0
|
|
for _, r := range rrs {
|
|
if r.Header().Rrtype == dns.TypeOPT {
|
|
continue
|
|
}
|
|
if dup {
|
|
rs[j] = dns.Copy(r)
|
|
} else {
|
|
rs[j] = r
|
|
}
|
|
rs[j].Header().Ttl = ttl
|
|
j++
|
|
}
|
|
return rs
|
|
}
|