Skip to content

Commit

Permalink
Cache generated ids to relieve pressure on the GC
Browse files Browse the repository at this point in the history
  • Loading branch information
Tom Wilkie committed Sep 8, 2015
1 parent 59b5bce commit fbe513e
Showing 1 changed file with 34 additions and 1 deletion.
35 changes: 34 additions & 1 deletion report/id.go
Original file line number Diff line number Diff line change
@@ -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.
Expand All @@ -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.
Expand Down

0 comments on commit fbe513e

Please sign in to comment.