Working character by character in Haskell

Simon Marlow
Tue, 23 Oct 2001 14:54:02 +0100

> "Simon Marlow" <> writes:
> > To really match the C program, you need to use IOExts.hGetBuf and
> > IOExts.hPutBuf, and do the operations on raw characters in memory.
> > Using a UArray of Word8 would be better, but there aren't any
> > operations to do IO to/from a UArray yet (actually I've written
> > these, but they aren't in the tree yet).
> So why don't getContents / putStr / etc. deforest cleanly to calls to
> hGetBuf and hPutBuf?  I'm genuinely curious; my own experience in this
> direction is "The engineering is challenging".  These functions are so
> commonly used, though---and they're vastly easier to use, actually
> portable, etc.  The effort would surely be repaid.

I think you're right, in that the engineering would be challenging.  You
could conceivably factor getContents into two functions getBlocks :: IO
[Block] and a pure function to unpack the blocks into characters, which
would lend itself to deforestation, but that would conflict with our
attempts to re-use buffers when possible.  But the gains might outweigh
the slight performance loss from not re-using buffers as much - it's an
interesting idea, anyway.