[Haskell-cafe] Re: [Haskell] installing streams library

Duncan Coutts duncan.coutts at worc.ox.ac.uk
Sun May 28 07:05:53 EDT 2006


On Sun, 2006-05-28 at 14:44 +0400, Bulat Ziganshin wrote:
> Hello Jeremy,
> 
> Sunday, May 28, 2006, 1:29:02 AM, you wrote:
> > Two alternative ideas are:
> 
> >  (1) accurately pre-calculate the size of the serialized structure and
> >      allocate the correct amount of memory from the start
> 
> it's good idea to have 'binarySize :: Binary a => a->Int' function,
> although using it will halve the speed, so for you it's not the best
> solution
> 
> >  (2) start with a 'guess' and realloc the memory if the initial guess
> >      is too small.
> 
> createMemBuf does exactly this :)

One of the areas where we found that Data.ByteString.Lazy was performing
better than the ordinary Data.ByteString is cases like this where we do
not know beforehand how big the buffer will be.

If you have to use a single contiguous buffer then it involves guessing
and possible reallocation. With a 'chunked' representation like
ByteString.Lazy it's not a problem as we just allocate another chunk and
start to fill that.

Obvious example include concat and getContents.

Would the same make sense for a MemBuf stream? Why does it need to be a
single large buffer? Couldn't it be a list of buffers?

Duncan



More information about the Haskell-Cafe mailing list