There is opportunities to skip synchronization events, allocations and maybe even do some zero copies (if WriteTo and ReadFrom are called concurrently).
If ReadFrom is called this would allow Read to acquire a reference to the Reader passed in and pass the buffer slice to the reader directly (skipping a possible allocation that would have been required to copy data from Reader to PipeWriter).
If WriteTo is called this would allow Write to acquire a reference to the Writer passed in and pass the buffer slice to the reader directly (skipping a possible allocation that would have required to copy data from PipeReader to Writer).
If ReadFrom and WriteTo are called this would allow to call io.Copy (or equivalent reusing a buffer) to maybe do a zero copy between the Writer and Reader.
The implementation would need to account for the fact that ReadFrom can be called multiple times and WriteTo need to waits until the pipe is closed, but nothing that cannot be done.
The extra channels required to pass the Writer and Reader around might be much in term of extra allocations, I guess a sync based implementation (instead of channels) would be better (as it would just embed all synchronization primitives into the pipe struct).
There is opportunities to skip synchronization events, allocations and maybe even do some zero copies (if
WriteToandReadFromare called concurrently).If
ReadFromis called this would allowReadto acquire a reference to theReaderpassed in and pass the buffer slice to the reader directly (skipping a possible allocation that would have been required to copy data fromReadertoPipeWriter).If
WriteTois called this would allowWriteto acquire a reference to theWriterpassed in and pass the buffer slice to the reader directly (skipping a possible allocation that would have required to copy data fromPipeReadertoWriter).If
ReadFromandWriteToare called this would allow to callio.Copy(or equivalent reusing a buffer) to maybe do a zero copy between theWriterandReader.The implementation would need to account for the fact that
ReadFromcan be called multiple times andWriteToneed to waits until the pipe is closed, but nothing that cannot be done.The extra channels required to pass the
WriterandReaderaround might be much in term of extra allocations, I guess asyncbased implementation (instead of channels) would be better (as it would just embed all synchronization primitives into thepipestruct).