From fbe513eb47c27e7807ebab385579b7f42d52a400 Mon Sep 17 00:00:00 2001 From: Tom Wilkie Date: Tue, 8 Sep 2015 17:51:11 +0000 Subject: [PATCH] Cache generated ids to relieve pressure on the GC --- report/id.go | 35 ++++++++++++++++++++++++++++++++++- 1 file changed, 34 insertions(+), 1 deletion(-) diff --git a/report/id.go b/report/id.go index 5f676850cd..11c31b492c 100644 --- a/report/id.go +++ b/report/id.go @@ -1,8 +1,13 @@ package report import ( + "hash" + "hash/fnv" "net" "strings" + "sync" + + cache "github.com/hashicorp/golang-lru" ) // TheInternet is used as a node ID to indicate a remote IP. @@ -21,9 +26,37 @@ const ( EdgeDelim = "|" ) +var ( + idCache, _ = cache.New(1024) + hashers = sync.Pool{ + New: func() interface{} { + return fnv.New64a() + }, + } +) + +func lookupID(_1, _2, _3 string, f func() string) string { + h := hashers.Get().(hash.Hash64) + h.Write([]byte(_1)) + h.Write([]byte(_2)) + h.Write([]byte(_3)) + sum := h.Sum64() + var result string + if id, ok := idCache.Get(sum); ok { + result = id.(string) + } + result = f() + idCache.Add(sum, result) + h.Reset() + hashers.Put(h) + return result +} + // MakeEndpointNodeID produces an endpoint node ID from its composite parts. func MakeEndpointNodeID(hostID, address, port string) string { - return MakeAddressNodeID(hostID, address) + ScopeDelim + port + return lookupID(hostID, address, port, func() string { + return MakeAddressNodeID(hostID, address) + ScopeDelim + port + }) } // MakeAddressNodeID produces an address node ID from its composite parts.