Add option to reuse buffers for interleaved frames#599
Add option to reuse buffers for interleaved frames#599kevmo314 wants to merge 1 commit intobluenviron:mainfrom
Conversation
|
@aler9 Do you have any thoughts/feedback on this change? |
|
Hello @kevmo314, while this is an improvement with respect to the current situation, it's not a complete solution. There are at least 3 methods to manage memory:
The library uses method 1 and this PR adds support for method 2, but in general users are constrained to pick one of the two methods, which is not an optimal solution, since method 1 is not efficient but method 2 prevents buffers from being shared between different routines. If we take a look at the standard Go library, that can serve as reference, most methods that require memory actually allow users to pass their own memory pointers: type Reader interface {
Read(p []byte) (n int, err error)
}Therefore, a good solution consists into doing the same here, for instance by allowing users to define a callback that provides buffers: type Server struct {
...
// function used to get byte buffers
GetBuffer func(n int) []byte
}This behavior needs to be implemented in both server and client, with both TCP and UDP transport protocols. |
Reusing buffers reduces garbage collection memory pressure. If the user opts in, the option
BufferReuseEnablewill reuse the payload buffer for incoming frames.Fixes #581.