-
Notifications
You must be signed in to change notification settings - Fork 295
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
[Feature] zero-copy ArrayPoolBufferWriter<byte>.WriteTo(Stream) #614
Comments
Hey, thank you for the proposal! I'm not really sure I follow, could you not just do: stream.Write(writerSource.WrittenSpan); ? 🤔 |
Actually, if you're on .NET Standard 2.0, this approach will still do a copy (though it will use a pooled buffer).
writerSource.WrittenMemory.AsStream().CopyTo(stream);
_ = MemoryMarshal.TryGetArray(writerSource.WrittenMemory, out ArraySegment<byte> segment);
stream.Write(segment.Array!, segment.Offset, segment.Count); |
Doesn't this incur a buffer copy inside the |
Thanks a lot, the 2nd bullet (with |
This made me realize ArraySegment<byte> segment = writerSource.DangerousGetArray();
stream.Write(segment.Array!, segment.Offset, segment.Count); |
Overview
ArrayPoolBufferWriter<byte>
: copy-free write out to a streamCurrent behavior
Before the change, the only way to write out the underlying byte array to a stream
requires a buffer copy.
Proposed change
Add a new method to write the array to a given stream.
See also https://github.com/vask-msft/CommunityToolkit-dotnet/tree/vask/WriteToStream --- I'll be happy to work it into an accepted PR if the feature is approved.
API breakdown
Usage example
The example demonstrates writing into a target stream that is itself wrapping a buffer writer, but in my local
fork I am using it for another custom
Stream
.Breaking change?
No
Alternatives
Currently to write to a given stream from a source buffer one may convert the written memory to array, incurring a buffer copy:
Additional context
No response
Help us help you
Yes, I'd like to be assigned to work on this item
The text was updated successfully, but these errors were encountered: