-
Notifications
You must be signed in to change notification settings - Fork 122
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
chore(dot/network): use sync.Pool for network message buffers #1600
Changes from all commits
5a38145
117ebb8
2fca3d8
09e8ca9
328dcd6
040cdc4
3f5a7b6
4eceebf
0680d7c
1ca19c6
4d747f2
e648cf1
e7abd0f
19034b8
0c71acd
823b5f8
66ebd2b
5c33f4a
2955598
5634551
ecfa3c7
e666056
a4199e9
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,57 @@ | ||
// Copyright 2019 ChainSafe Systems (ON) Corp. | ||
// This file is part of gossamer. | ||
// | ||
// The gossamer library is free software: you can redistribute it and/or modify | ||
// it under the terms of the GNU Lesser General Public License as published by | ||
// the Free Software Foundation, either version 3 of the License, or | ||
// (at your option) any later version. | ||
// | ||
// The gossamer library is distributed in the hope that it will be useful, | ||
// but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
// GNU Lesser General Public License for more details. | ||
// | ||
// You should have received a copy of the GNU Lesser General Public License | ||
// along with the gossamer library. If not, see <http://www.gnu.org/licenses/>. | ||
|
||
package network | ||
|
||
// sizedBufferPool is a pool of buffers used for reading from streams | ||
type sizedBufferPool struct { | ||
c chan *[maxMessageSize]byte | ||
} | ||
|
||
func newSizedBufferPool(min, max int) (bp *sizedBufferPool) { | ||
bufferCh := make(chan *[maxMessageSize]byte, max) | ||
|
||
for i := 0; i < min; i++ { | ||
buf := [maxMessageSize]byte{} | ||
bufferCh <- &buf | ||
} | ||
|
||
return &sizedBufferPool{ | ||
c: bufferCh, | ||
} | ||
} | ||
|
||
// get gets a buffer from the sizedBufferPool, or creates a new one if none are | ||
// available in the pool. Buffers have a pre-allocated capacity. | ||
func (bp *sizedBufferPool) get() [maxMessageSize]byte { | ||
var buff *[maxMessageSize]byte | ||
select { | ||
case buff = <-bp.c: | ||
// reuse existing buffer | ||
default: | ||
// create new buffer | ||
buff = &[maxMessageSize]byte{} | ||
} | ||
return *buff | ||
} | ||
|
||
// put returns the given buffer to the sizedBufferPool. | ||
func (bp *sizedBufferPool) put(b *[maxMessageSize]byte) { | ||
select { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. should we clear the buffer when it comes back to the pool? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. no it doesn't make a difference since the buffer contents will be overwritten when we read into it |
||
case bp.c <- b: | ||
default: // Discard the buffer if the pool is full. | ||
} | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Rename it to
preAllocate
Because this is difficult to read
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
we want to pre-allocate by default so if this is changed to
preAllocate
then it will be false by default, but we won't know if it was set to false or if it defaulted to false, hence why it'snoPreAllocate