mirror of
https://github.com/coredns/coredns.git
synced 2025-11-01 02:33:14 -04:00
plugin/cache: unroll minTTL loop (#1773)
This allocates memory because of the append, just unroll the loop. Also add benchmark. Before: goos: linux goarch: amd64 pkg: github.com/coredns/coredns/plugin/cache BenchmarkCacheResponse-4 100000 11910 ns/op BenchmarkMinMsgTTL-4 1000000 1494 ns/op PASS After: goos: linux goarch: amd64 pkg: github.com/coredns/coredns/plugin/cache BenchmarkCacheResponse-4 100000 12016 ns/op BenchmarkMinMsgTTL-4 2000000 668 ns/op PASS
This commit is contained in:
29
plugin/cache/item.go
vendored
29
plugin/cache/item.go
vendored
@@ -99,7 +99,32 @@ func minMsgTTL(m *dns.Msg, mt response.Type) time.Duration {
|
||||
}
|
||||
|
||||
minTTL := maxTTL
|
||||
for _, r := range append(append(m.Answer, m.Ns...), m.Extra...) {
|
||||
for _, r := range m.Answer {
|
||||
switch mt {
|
||||
case response.NameError, response.NoData:
|
||||
if r.Header().Rrtype == dns.TypeSOA {
|
||||
minTTL = time.Duration(r.(*dns.SOA).Minttl) * time.Second
|
||||
}
|
||||
case response.NoError, response.Delegation:
|
||||
if r.Header().Ttl < uint32(minTTL.Seconds()) {
|
||||
minTTL = time.Duration(r.Header().Ttl) * time.Second
|
||||
}
|
||||
}
|
||||
}
|
||||
for _, r := range m.Ns {
|
||||
switch mt {
|
||||
case response.NameError, response.NoData:
|
||||
if r.Header().Rrtype == dns.TypeSOA {
|
||||
minTTL = time.Duration(r.(*dns.SOA).Minttl) * time.Second
|
||||
}
|
||||
case response.NoError, response.Delegation:
|
||||
if r.Header().Ttl < uint32(minTTL.Seconds()) {
|
||||
minTTL = time.Duration(r.Header().Ttl) * time.Second
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
for _, r := range m.Extra {
|
||||
if r.Header().Rrtype == dns.TypeOPT {
|
||||
// OPT records use TTL field for extended rcode and flags
|
||||
continue
|
||||
@@ -107,7 +132,7 @@ func minMsgTTL(m *dns.Msg, mt response.Type) time.Duration {
|
||||
switch mt {
|
||||
case response.NameError, response.NoData:
|
||||
if r.Header().Rrtype == dns.TypeSOA {
|
||||
return time.Duration(r.(*dns.SOA).Minttl) * time.Second
|
||||
minTTL = time.Duration(r.(*dns.SOA).Minttl) * time.Second
|
||||
}
|
||||
case response.NoError, response.Delegation:
|
||||
if r.Header().Ttl < uint32(minTTL.Seconds()) {
|
||||
|
||||
Reference in New Issue
Block a user