Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(dot/network): memory improvement for network buffers #2233

Merged
merged 1 commit into from
Feb 4, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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 @@ -107,7 +107,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 @@ -181,16 +181,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