-
Notifications
You must be signed in to change notification settings - Fork 16
use io.CopyBuffer with explicitly allocated buffers #69
Conversation
io.Copy uses a 32KB buffer, which pprof indicates result in significant memory usage.
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.
The 4096B buffer LGTM as in practice the max MTU for Ethernet networks is 1500B. I think we disable Nagle (I could be mistaken), so we can do with smaller buffers. But my assumptions could be wrong.
However, I think we would benefit from using a sync.Pool here to further curtail memory thrashing and GC pressure. WDYT?
Yeah, we could use a sync.Pool, steb made the same comment. Does it ever return memory? I am worried about hoarding memory when we scale up. |
reading the docs, it explicitly mentioned that pooled objects may be gc'ed which is nice. |
Yeah, it holds weak references, so unused elements are elegible for collection by the GC :-) |
Added a pool. |
relay.go
Outdated
@@ -85,6 +87,11 @@ func NewRelay(ctx context.Context, h host.Host, upgrader *tptu.Upgrader, opts .. | |||
incoming: make(chan *Conn), | |||
relays: make(map[peer.ID]struct{}), | |||
liveHops: make(map[peer.ID]map[peer.ID]int), | |||
bufPool: sync.Pool{ | |||
New: func() interface{} { | |||
return make([]byte, 4096) |
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.
So, welcome to go. Converting a []byte
to an interface{}
allocates. Let's just use https://github.com/libp2p/go-buffer-pool (https://godoc.org/github.com/libp2p/go-buffer-pool#Get)
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.
Oh wow, that's nuts.
Yeah, I'll use go-buffer-pool.
io.Copy
uses a 32KB buffer, which as pprof indicates results in significant memory usage.This changes to using
io.CopyBuffer
directly with 4KB buffers.