Cetin Sert
Cetin Sert

Reputation: 4600

Haskell: what is the fastest way to pipe data from handle to a handle?

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.

Upvotes: 1

Views: 668

Answers (2)

dflemstr
dflemstr

Reputation: 26167

I assume that you are dealing with sockets, because of your tags.

If this is the case, use the sendfile package to perform the operation with sendFile. Note that you have to use the Haskell networking library and real sockets; it doesn't work with handles.

If you are dealing with abstract handles, you need to do what @thomas-m-dubuisson suggested and copy the handles explicitly, because you need some kind of buffering to make the operation efficient. You shouldn't use lazy byte strings, however, because hGetSome will not read enough to make lazy byte strings more efficient.

almostForever $ Data.ByteString.hGetSome h1 nr >>= Data.ByteString.hPutStr h2

Upvotes: 1

Thomas M. DuBuisson
Thomas M. DuBuisson

Reputation: 64740

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.

Upvotes: 1

Related Questions