[Haskell-cafe] two problems with Data.Binary and Data.ByteString

Don Stewart dons at galois.com
Wed Nov 5 14:10:01 EST 2008

> I had the same problem (stack overflow). The solution was to change
> the >>= operator in the Get monad. Currently it is:
>   m >>= k   = Get (\s -> let (a, s') = unGet m s
>                            in unGet (k a) s')
> but I changed it to:
>     m >>= k   = Get (\s -> case unGet m s of
>                              (a, s') -> unGet (k a) s')
> It seems that the bind operator is lazy and this caused the stack overflow.

Hmm. That's interesting. I'm not sure that doesn't change other things
we rely on though.
> I have also another problem. Every Int and Word is stored as 64-bit
> value and this expands the output file a lot. I have a lot of integers
> and most of them are < 128 but not all of them. I changed the
> serialization so that the Int and Word are serialized in a variable
> number of bytes. Without this change the binary serialization was even
> worse than the textual serialization that we had before. The file was
> almost full with zeros.

The motivation for this is to use zlib compress / decompress.

    writeFile "f" . compress . encode $ foo
> I just haven't time to prepare a patch and to send it for review but
> if other people have the same problem I will do it.

Patches welcome. You shouldn't need to patch a library like this, it
should be able to do what you need.

-- Don

More information about the Haskell-Cafe mailing list