[Haskell-cafe] How can I avoid buffered reads?

Bardur Arantsson spam at scientician.net
Wed Nov 28 21:03:39 CET 2012


On 11/28/2012 08:38 PM, Leon Smith wrote:
> I have some code that reads (infrequently) small amounts of data from
> /dev/urandom,  and because this is pretty infrequent,  I simply open the
> handle and close it every time I need some random bytes.
> 
> The problem is that I recently discovered that,  thanks to buffering within
> GHC,   I was actually reading 8096 bytes when I only need 16 bytes,  and
> thus wasting entropy.   Moreover  calling hSetBuffering  handle NoBuffering
> did not change this behavior.
> 
> I'm not sure if this behavior is a bug or a feature,  but in any case it's
> unacceptable for dealing with /dev/urandom.   Probably the simplest way to
> fix this is to write a little C helper function that will read from
> /dev/urandom for me,  so that I have precise control over the system calls
> involved.     But I'm curious if GHC can manage this use case correctly;
> I've just started digging into the GHC.IO code myself.
> 

Use openFd, fdReadBuf and closeFd from the System.Posix.IO.ByteString
module in the 'unix' package.

Those correspond directly to system calls and are thus unbuffered.




More information about the Haskell-Cafe mailing list