Optimize the memory footprint under SOCKS5 when USERNAME/PASSWORD authentication is used #315
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Currently the Go
bytes.Buffer
struct is not used optimally, ref: https://github.com/cty123/tun2socks/blob/main/transport/socks5/socks5.go#L190bytes.Buffer{}
always returns an empty buffer with capacity = 0, and this capacity grows as data is written into the buffer. Under the hood,bytes.Buffer{}
has a priavtegrow(int)
function to allocate additional space to store the data.Therefore, during the operation to construct the AuthMsg payload, there are 2 things that are not optimal,
What we can do instead is that, since the total amount of payload size can be calculated before the allocation, we can compute it and allocate a block of memory that fits exactly to the payload data, such that we are not wasting any memory and can avoid resizing the buffer during the handling.
For example,