How can I define
pipe :: Handle -> Handle -> IO ()
in the most efficient way in Haskell (platform 2011-04 or ghc 7.4)?
Update 1: How to write a minimal-overhead proxy to localhost:3389 in Haskell?
Update 2: Using GNU/Linux system call `splice` for zero-copy Socket to Socket data transfers in Haskell
Uses the GNU/Linux system call splice
for zero-copy data piping between two network sockets, also makes room for a portable socket-to-socket splice
substitute written in Haskell, which uses (mallocBytes
, hGetBufSome
and hPutBuf
) a one-time allocated user space buffer throughout the data transfer loop avoiding tiny allocations that lead to pressure on the garbage collector with repeated calls to recv
, sendAll
from the bytestring
package.
I don't think you'll find any non-FFI solution that significantly beats either:
almostForever $ Data.ByteString.hGetSome h1 nr >>= Data.ByteString.hPutStr h2
Or perhaps you'd gain a little using lazy byte strings:
Data.ByteString.Lazy.hGetContents h1 >>= Data.ByteString.Lazy.hPut h2
If you have time then run a benchmark of these. If you don't have time, then just do one and don't worry so much about performance unless it's actually an issue.