[Haskell] Re: [Haskell-cafe] ANNOUNCE: enumerator, an alternative iteratee package

Felipe Lessa felipe.lessa at gmail.com
Sat Aug 21 08:24:32 EDT 2010


On Sat, Aug 21, 2010 at 5:40 AM, Magnus Therning <magnus at therning.org> wrote:
> It changes the timing.  The iteratee will receive the data sooner (when it's
> available rather than when the buffer is full).  This means it can fail
> *sooner*, in wall-clock time.

I still fail to see how this works.  So I went to see the sources.

In [1] we can see how hGet and hGetNonBlocking are defined.  The only
difference is that the former uses hGetBuf, and the latter uses
hGetBufNonBlocking.

[1] http://hackage.haskell.org/packages/archive/bytestring/0.9.1.7/doc/html/src/Data-ByteString.html#line-1908

hGetBuf's main loop is bufRead [2], while hGetBufNonBlocking's main
loop is bufReadNonBlocking [3].  Both are very similar.  The main
differences are RawIO.read vs RawIO.readNonBlocking [4], and
Buffered.fillReadBuffer vs Buffered.fillReadBuffer0 [5].  Reading
RawIO's documentation [4], we see that RawIO.read blocks only if there
is no data available.  So it doesn't wait for the buffer to be fully
filled, it just "returns the available data".  Unfortunately,
BufferedIO's documentation [5] doesn't specify if
Buffered.fillReadBuffer should return the available data without
blocking.  However, it does specify that that it should be "blocking
if the are no bytes available".

[2] http://hackage.haskell.org/packages/archive/base/4.2.0.1/doc/html/src/GHC-IO-Handle-Text.html#line-820
[3] http://hackage.haskell.org/packages/archive/base/4.2.0.1/doc/html/src/GHC-IO-Handle-Text.html#bufReadNonBlocking
[4] http://hackage.haskell.org/packages/archive/base/4.2.0.1/doc/html/src/GHC-IO-Device.html#RawIO
[5] http://hackage.haskell.org/packages/archive/base/4.2.0.1/doc/html/src/GHC-IO-BufferedIO.html#BufferedIO

So, assuming that the semantics of BufferedIO are the same as RawIO's,
*both* are non-blocking whenever data is already available.  None of
them wait until the buffer is full.  The difference lies in whether
they block if there is no data available.  However, when there isn't
data the enumarator *always* wants to block.  So using non-blocking IO
doesn't give anything, only complicates the code.

Am I misreading the docs/source somewhere?  =)

Cheers!

-- 
Felipe.


More information about the Haskell-Cafe mailing list