Skip to content

Commit

Permalink
Merge pull request #79 from libp2p/feat/better-intern
Browse files Browse the repository at this point in the history
improve interning
  • Loading branch information
Stebalien committed May 28, 2019
2 parents 075e2a2 + 5197d8f commit fd88712
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 17 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
16 changes: 1 addition & 15 deletions p2p/host/peerstore/pstoremem/protobook.go
Expand Up @@ -8,11 +8,6 @@ import (
pstore "github.com/libp2p/go-libp2p-peerstore"
)

const (
maxInternedProtocols = 512
maxInternedProtocolSize = 256
)

type protoSegment struct {
sync.RWMutex
protocols map[peer.ID]map[string]struct{}
Expand All @@ -35,7 +30,7 @@ var _ pstore.ProtoBook = (*memoryProtoBook)(nil)

func NewProtoBook() pstore.ProtoBook {
return &memoryProtoBook{
interned: make(map[string]string, maxInternedProtocols),
interned: make(map[string]string, 256),
segments: func() (ret protoSegments) {
for i := range ret {
ret[i] = &protoSegment{
Expand All @@ -48,10 +43,6 @@ func NewProtoBook() pstore.ProtoBook {
}

func (pb *memoryProtoBook) internProtocol(proto string) string {
if len(proto) > maxInternedProtocolSize {
return proto
}

// check if it is interned with the read lock
pb.lk.RLock()
interned, ok := pb.interned[proto]
Expand All @@ -71,11 +62,6 @@ func (pb *memoryProtoBook) internProtocol(proto string) string {
return interned
}

// if we've filled the table, throw it away and start over
if len(pb.interned) >= maxInternedProtocols {
pb.interned = make(map[string]string, maxInternedProtocols)
}

pb.interned[proto] = proto
return proto
}
Expand Down

0 comments on commit fd88712

Please sign in to comment.