Moved from https://github.com/dotnet/corefxlab/issues/2031
Foreign buffers are buffers not allocated with the Pipe MemoryPool.
Use-cases (by @Drawaes @mgravell @ReubenBond)
With the current API, these buffers are copied into buffers owned by the Pipe MemoryPool:
Challenges:
CC @davidfowl
One factor to consider is that the use case I have most of the time will not be a "foreign" buffer it will be from the same pool just a different pipe. So it should be fine in the RIO case.
Also we managed to sneak in some changes to pipelines that will allow a buffer to identify it's pool in the final API reviews before lockdown.
From https://github.com/dotnet/runtime/issues/32894:
This API would allow a user to insert their own buffer into a PipeWriter's stream.
This allows a PipeWriter to be truly zero-copy for such a scenario, while currently they must copy their buffer into the PipeWriter's internal buffer. An example might be a block of pre-serialized data that a user might reuse throughout a stream's lifetime. The transport implementation could then take the resulting sequence of the PipeWriter's own buffers and the user's buffers and perform a gathered write to the socket.
class PipeWriter { // Allocate a reusable block that is guaranteed to be zero-copy if passed to AppendBuffer(). public virtual IMemoryOwner<byte> GetReusableMemory(int length); // If the buffer is compatible with the pipe writer's needs, use the buffer directly. Otherwise, do an implicit GetSpan()+Copy()+Advance(). // ValueTask completes once the buffer is no longer in use by the PipeWriter. // Unlike WriteAsync, does not flush. public virtual ValueTask AppendBufferAsync(ReadOnlyMemory<byte> buffer); }
Most helpful comment
One factor to consider is that the use case I have most of the time will not be a "foreign" buffer it will be from the same pool just a different pipe. So it should be fine in the RIO case.
Also we managed to sneak in some changes to pipelines that will allow a buffer to identify it's pool in the final API reviews before lockdown.