diff options
author | 2018-10-29 15:13:39 +0000 | |
---|---|---|
committer | 2018-10-29 08:13:39 -0700 | |
commit | e6d02a3fd25831fc68e5c6f2bf9b779098ef8b98 (patch) | |
tree | 305823ab8d1ba4e0e156209367cc4d201cd59c87 /plugin/cache/cache.go | |
parent | 29f42053643325bf0c23ae8da47362ea1a7650e0 (diff) | |
download | coredns-e6d02a3fd25831fc68e5c6f2bf9b779098ef8b98.tar.gz coredns-e6d02a3fd25831fc68e5c6f2bf9b779098ef8b98.tar.zst coredns-e6d02a3fd25831fc68e5c6f2bf9b779098ef8b98.zip |
cache: some optimizations (#2247)
Remove some optimization and lowercasing of the qname (in the end
miekg/dns should provide a fast and OK function for it).
* remove the make([]byte, 2) allocation in the key()
* use already lowercased qname in hash key calculation.
% benchcmp old.txt new.txt
benchmark old ns/op new ns/op delta
BenchmarkCacheResponse-4 9599 8735 -9.00%
Signed-off-by: Miek Gieben <miek@miek.nl>
Diffstat (limited to 'plugin/cache/cache.go')
-rw-r--r-- | plugin/cache/cache.go | 26 |
1 files changed, 8 insertions, 18 deletions
diff --git a/plugin/cache/cache.go b/plugin/cache/cache.go index 9ff43dac2..cd0fda505 100644 --- a/plugin/cache/cache.go +++ b/plugin/cache/cache.go @@ -2,7 +2,6 @@ package cache import ( - "encoding/binary" "hash/fnv" "net" "time" @@ -61,10 +60,10 @@ func New() *Cache { } } -// Return key under which we store the item, -1 will be returned if we don't store the -// message. +// key returns key under which we store the item, -1 will be returned if we don't store the message. // Currently we do not cache Truncated, errors zone transfers or dynamic update messages. -func key(m *dns.Msg, t response.Type, do bool) (bool, uint64) { +// qname holds the already lowercased qname. +func key(qname string, m *dns.Msg, t response.Type, do bool) (bool, uint64) { // We don't store truncated responses. if m.Truncated { return false, 0 @@ -74,7 +73,7 @@ func key(m *dns.Msg, t response.Type, do bool) (bool, uint64) { return false, 0 } - return true, hash(m.Question[0].Name, m.Question[0].Qtype, do) + return true, hash(qname, m.Question[0].Qtype, do) } var one = []byte("1") @@ -89,18 +88,9 @@ func hash(qname string, qtype uint16, do bool) uint64 { h.Write(zero) } - b := make([]byte, 2) - binary.BigEndian.PutUint16(b, qtype) - h.Write(b) - - for i := range qname { - c := qname[i] - if c >= 'A' && c <= 'Z' { - c += 'a' - 'A' - } - h.Write([]byte{c}) - } - + h.Write([]byte{byte(qtype >> 8)}) + h.Write([]byte{byte(qtype)}) + h.Write([]byte(qname)) return h.Sum64() } @@ -167,7 +157,7 @@ func (w *ResponseWriter) WriteMsg(res *dns.Msg) error { } // key returns empty string for anything we don't want to cache. - hasKey, key := key(res, mt, do) + hasKey, key := key(w.state.Name(), res, mt, do) msgTTL := dnsutil.MinimalTTL(res, mt) var duration time.Duration |