Skip to content

Commit

Permalink
feat: intern agent versions
Browse files Browse the repository at this point in the history
This also starts taking up a bunch of memory after a while.
  • Loading branch information
Stebalien committed May 27, 2019
1 parent a862d91 commit 5197d8f
Showing 1 changed file with 15 additions and 2 deletions.
17 changes: 15 additions & 2 deletions p2p/host/peerstore/pstoremem/metadata.go
Expand Up @@ -7,6 +7,11 @@ import (
pstore "github.com/libp2p/go-libp2p-peerstore"
)

var internKeys = map[string]bool{
"AgentVersion": true,
"ProtocolVersion": true,
}

type metakey struct {
id peer.ID
key string
Expand All @@ -15,8 +20,9 @@ type metakey struct {
type memoryPeerMetadata struct {
// store other data, like versions
//ds ds.ThreadSafeDatastore
ds map[metakey]interface{}
dslock sync.RWMutex
ds map[metakey]interface{}
dslock sync.RWMutex
interned map[string]interface{}
}

var _ pstore.PeerMetadata = (*memoryPeerMetadata)(nil)
Expand All @@ -30,6 +36,13 @@ func NewPeerMetadata() pstore.PeerMetadata {
func (ps *memoryPeerMetadata) Put(p peer.ID, key string, val interface{}) error {
ps.dslock.Lock()
defer ps.dslock.Unlock()
if vals, ok := val.(string); ok && internKeys[key] {
if interned, ok := ps.interned[vals]; ok {
val = interned
} else {
ps.interned[vals] = val
}
}
ps.ds[metakey{p, key}] = val
return nil
}
Expand Down

0 comments on commit 5197d8f

Please sign in to comment.