Skip to content

Commit

Permalink
Use a sync.Pool instead of sizedBufferPool
Browse files Browse the repository at this point in the history
  • Loading branch information
qdm12 committed Jan 27, 2022
1 parent be668c1 commit 012e4cf
Show file tree
Hide file tree
Showing 5 changed files with 14 additions and 174 deletions.
5 changes: 3 additions & 2 deletions dot/network/inbound.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,9 @@ func (s *Service) readStream(stream libp2pnetwork.Stream, decoder messageDecoder
s.streamManager.logNewStream(stream)

peer := stream.Conn().RemotePeer()
msgBytes := s.bufPool.get()
defer s.bufPool.put(msgBytes)
buffer := s.bufPool.Get().(*[]byte)
defer s.bufPool.Put(buffer)
msgBytes := *buffer

for {
n, err := readStream(stream, msgBytes[:])
Expand Down
10 changes: 5 additions & 5 deletions dot/network/notifications.go
Original file line number Diff line number Diff line change
Expand Up @@ -427,11 +427,11 @@ func (s *Service) readHandshake(stream libp2pnetwork.Stream, decoder HandshakeDe
hsC := make(chan *handshakeReader)

go func() {
msgBytes := s.bufPool.get()
defer func() {
s.bufPool.put(msgBytes)
close(hsC)
}()
defer close(hsC)

buffer := s.bufPool.Get().(*[]byte)
defer s.bufPool.Put(buffer)
msgBytes := *buffer

tot, err := readStream(stream, msgBytes[:])
if err != nil {
Expand Down
43 changes: 0 additions & 43 deletions dot/network/pool.go

This file was deleted.

114 changes: 0 additions & 114 deletions dot/network/pool_test.go

This file was deleted.

16 changes: 6 additions & 10 deletions dot/network/service.go
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ type Service struct {
host *host
mdns *mdns
gossip *gossip
bufPool *sizedBufferPool
bufPool *sync.Pool
streamManager *streamManager

notificationsProtocols map[byte]*notificationsProtocol // map of sub-protocol msg ID to protocol info
Expand Down Expand Up @@ -135,16 +135,12 @@ func NewService(cfg *Config) (*Service, error) {
return nil, err
}

// pre-allocate pool of buffers used to read from streams.
// initially allocate as many buffers as likely necessary which is the number of inbound streams we will have,
// which should equal the average number of peers times the number of notifications protocols, which is currently 3.
preAllocateInPool := cfg.MinPeers * 3
poolSize := cfg.MaxPeers * 3
if cfg.noPreAllocate { // testing
preAllocateInPool = 0
poolSize = cfg.MinPeers * 3
bufPool := &sync.Pool{
New: func() interface{} {
b := make([]byte, maxMessageSize)
return &b
},
}
bufPool := newSizedBufferPool(preAllocateInPool, poolSize)

network := &Service{
ctx: ctx,
Expand Down

0 comments on commit 012e4cf

Please sign in to comment.