reduce mutex contention in SetProtocols

This commit is contained in:
Marten Seemann 2021-10-22 15:51:34 +02:00
parent a1f426f3d0
commit 3f028c937b
2 changed files with 9 additions and 10 deletions

View File

@ -4,7 +4,7 @@ import (
"fmt"
"sync"
peer "github.com/libp2p/go-libp2p-core/peer"
"github.com/libp2p/go-libp2p-core/peer"
pstore "github.com/libp2p/go-libp2p-core/peerstore"
)
@ -43,15 +43,15 @@ func (pb *dsProtoBook) SetProtocols(p peer.ID, protos ...string) error {
return err
}
s := pb.segments.get(p)
s.Lock()
defer s.Unlock()
protomap := make(map[string]struct{}, len(protos))
for _, proto := range protos {
protomap[proto] = struct{}{}
}
s := pb.segments.get(p)
s.Lock()
defer s.Unlock()
return pb.meta.Put(p, "protocols", protomap)
}

View File

@ -3,7 +3,7 @@ package pstoremem
import (
"sync"
peer "github.com/libp2p/go-libp2p-core/peer"
"github.com/libp2p/go-libp2p-core/peer"
pstore "github.com/libp2p/go-libp2p-core/peerstore"
)
@ -71,16 +71,15 @@ func (pb *memoryProtoBook) SetProtocols(p peer.ID, protos ...string) error {
return err
}
s := pb.segments.get(p)
s.Lock()
defer s.Unlock()
newprotos := make(map[string]struct{}, len(protos))
for _, proto := range protos {
newprotos[pb.internProtocol(proto)] = struct{}{}
}
s := pb.segments.get(p)
s.Lock()
s.protocols[p] = newprotos
s.Unlock()
return nil
}