Skip to content

Commit 81f0e98

Browse files
committed
Use a sync.Pool instead of sizedBufferPool
1 parent aae6c81 commit 81f0e98

File tree

5 files changed

+14
-174
lines changed

5 files changed

+14
-174
lines changed

dot/network/inbound.go

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,8 +15,9 @@ func (s *Service) readStream(stream libp2pnetwork.Stream, decoder messageDecoder
1515
s.streamManager.logNewStream(stream)
1616

1717
peer := stream.Conn().RemotePeer()
18-
msgBytes := s.bufPool.get()
19-
defer s.bufPool.put(msgBytes)
18+
buffer := s.bufPool.Get().(*[]byte)
19+
defer s.bufPool.Put(buffer)
20+
msgBytes := *buffer
2021

2122
for {
2223
n, err := readStream(stream, msgBytes[:])

dot/network/notifications.go

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -427,11 +427,11 @@ func (s *Service) readHandshake(stream libp2pnetwork.Stream, decoder HandshakeDe
427427
hsC := make(chan *handshakeReader)
428428

429429
go func() {
430-
msgBytes := s.bufPool.get()
431-
defer func() {
432-
s.bufPool.put(msgBytes)
433-
close(hsC)
434-
}()
430+
defer close(hsC)
431+
432+
buffer := s.bufPool.Get().(*[]byte)
433+
defer s.bufPool.Put(buffer)
434+
msgBytes := *buffer
435435

436436
tot, err := readStream(stream, msgBytes[:])
437437
if err != nil {

dot/network/pool.go

Lines changed: 0 additions & 43 deletions
This file was deleted.

dot/network/pool_test.go

Lines changed: 0 additions & 114 deletions
This file was deleted.

dot/network/service.go

Lines changed: 6 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -63,7 +63,7 @@ type Service struct {
6363
host *host
6464
mdns *mdns
6565
gossip *gossip
66-
bufPool *sizedBufferPool
66+
bufPool *sync.Pool
6767
streamManager *streamManager
6868

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

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

149145
network := &Service{
150146
ctx: ctx,

0 commit comments

Comments
 (0)