mirror of
https://github.com/libp2p/go-libp2p-peerstore.git
synced 2026-08-21 15:23:28 +08:00
feat: improve protocol interning
This used to specify a max size/count but that was only because this logic was shared between all peerstores (including the on-disk one). Now that: 1. This only applies to the in-memory one. 2. We never actually GC these. We can just intern indefinitely.
This commit is contained in:
@@ -8,11 +8,6 @@ import (
|
|||||||
pstore "github.com/libp2p/go-libp2p-peerstore"
|
pstore "github.com/libp2p/go-libp2p-peerstore"
|
||||||
)
|
)
|
||||||
|
|
||||||
const (
|
|
||||||
maxInternedProtocols = 512
|
|
||||||
maxInternedProtocolSize = 256
|
|
||||||
)
|
|
||||||
|
|
||||||
type protoSegment struct {
|
type protoSegment struct {
|
||||||
sync.RWMutex
|
sync.RWMutex
|
||||||
protocols map[peer.ID]map[string]struct{}
|
protocols map[peer.ID]map[string]struct{}
|
||||||
@@ -35,7 +30,7 @@ var _ pstore.ProtoBook = (*memoryProtoBook)(nil)
|
|||||||
|
|
||||||
func NewProtoBook() pstore.ProtoBook {
|
func NewProtoBook() pstore.ProtoBook {
|
||||||
return &memoryProtoBook{
|
return &memoryProtoBook{
|
||||||
interned: make(map[string]string, maxInternedProtocols),
|
interned: make(map[string]string, 256),
|
||||||
segments: func() (ret protoSegments) {
|
segments: func() (ret protoSegments) {
|
||||||
for i := range ret {
|
for i := range ret {
|
||||||
ret[i] = &protoSegment{
|
ret[i] = &protoSegment{
|
||||||
@@ -48,10 +43,6 @@ func NewProtoBook() pstore.ProtoBook {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func (pb *memoryProtoBook) internProtocol(proto string) string {
|
func (pb *memoryProtoBook) internProtocol(proto string) string {
|
||||||
if len(proto) > maxInternedProtocolSize {
|
|
||||||
return proto
|
|
||||||
}
|
|
||||||
|
|
||||||
// check if it is interned with the read lock
|
// check if it is interned with the read lock
|
||||||
pb.lk.RLock()
|
pb.lk.RLock()
|
||||||
interned, ok := pb.interned[proto]
|
interned, ok := pb.interned[proto]
|
||||||
@@ -71,11 +62,6 @@ func (pb *memoryProtoBook) internProtocol(proto string) string {
|
|||||||
return interned
|
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
|
pb.interned[proto] = proto
|
||||||
return proto
|
return proto
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user