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

Add option to reuse buffers for interleaved frames #599

Open
wants to merge 1 commit into
base: main
Choose a base branch
from

Conversation

kevmo314
Copy link
Contributor

@kevmo314 kevmo314 commented Aug 6, 2024

Reusing buffers reduces garbage collection memory pressure. If the user opts in, the option BufferReuseEnable will reuse the payload buffer for incoming frames.

Fixes #581.

@kevmo314
Copy link
Contributor Author

@aler9 Do you have any thoughts/feedback on this change?

@aler9
Copy link
Member

aler9 commented Aug 25, 2024

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:

  1. by using unique buffers
  2. by using a shared buffer
  3. by using a buffer pool

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

reuse buffers instead of allocating new ones during each read
2 participants