Proposal: add ByteString support to unix:System.Posix.IO API

wren ng thornton wren at
Thu Mar 3 03:24:04 CET 2011

On 3/2/11 1:58 AM, Antoine Latter wrote:
> On Tue, Mar 1, 2011 at 9:23 PM, wren ng thornton<wren at>  wrote:
>> On 3/1/11 1:35 AM, Bryan O'Sullivan wrote:
>>> I want to see four entry points for writing:
>>> fdWrite :: Strict.ByteString ->    IO Int
>>> fdWriteAll :: Strict.ByteString ->    IO ()
>>> fdWritev :: [Strict.ByteString] ->    IO Int -- turn the list into an iovec,
>>> then call writev
>>> fdWritevAll :: [Strict.ByteString] ->    IO ()
>> Using writev requires the length of the list in order to get a count of
>> chunks, which forces us to hold the whole list/lazy-bytestring in memory at
>> once and also adds O(n) time for traversing it. Also it'd require converting
>> each of the ByteString structs into iovec structs (whereas using write
>> allows this to be unpacked into the call frames for write).
>> What's the benefit of doing this? Is writev that much more efficient than
>> Haskell code with the same semantics[1]?
> The benefit of using 'writev' over multiple calls to 'write' is the
> 'writev' is frequently a single kernel call - avoiding multiple
> context switches.

Ah, of course.

> Whether or not it is worth it to hang on to the entire bytestring to
> get this advantage probably depends on the circumstance.

In that case I'd suggest having both writev and iterated write versions 
of handling lazy/lists-of bytestrings. I think the increased API size 
would be worth it to cover both situations.

Live well,

More information about the Libraries mailing list