-
Notifications
You must be signed in to change notification settings - Fork 17.9k
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
proposal: encoding: add interface BinaryWriter to reduce pressure on GC #72915
Comments
CC @dsnet |
I don’t see how this provides any greater functionality over the Looking at the linked examples… I’m unsure how that code is bringing any advantage to GC pressure over just using But I don’t want to get too in the weeds on that specific implementation, because my central consideration is what behavior does this provide that is not covered by good |
@puellanivis thanks for your comment. The main idea of With |
But then the |
I don’t believe I understand what you mean here. Can you demonstrate some code that would “double reallocate” here, that could not easily be solved ahead of the code with a Like:
Like, equally in your example, one could use |
isn't this just the |
You are testing a completely different case, you have to call an interface with slice in a way that the call is not devirtualized. |
The global |
thanks, missed that |
:blink: :blink: It never occurred to me that one could totally use it like that? 🤦♀ Like, to write a non-data stream thing to an |
Also this is a pattern that we already expose in few APIs like: https://pkg.go.dev/golang.org/x/tools/go/ssa#Function.WriteTo |
Also, from I think the interface might be a good idea in general… even if I’m not 100% sold on the “reduce GC pressure” argument. |
Proposal Details
The main problem with the existing https://pkg.go.dev/encoding#BinaryMarshaler - is that it puts a huge pressure on the garbage collector, as there is no way to reuse byte buffers.
In 1.24 https://pkg.go.dev/encoding#BinaryAppender was introduced, which partially solves this problem (though it doesn't help much in cases of reallocation)
It would be great to have Marshaler interface which allows you to keep the control over the underlying buffer.
As a working example:
EDF - is a binary format in the network stack of Ergo Framework https://github.com/ergo-services/ergo
It uses:
https://github.com/ergo-services/ergo/blob/master/net/edf/edf.go#L53
This approach allows to reuse all the binary buffers
The text was updated successfully, but these errors were encountered: