[Haskell-cafe] ANNOUNCE: binary 0.4: high performance, pure binary parsing and serialisation

Don Stewart dons at galois.com
Sat Oct 6 11:21:55 EDT 2007


Binary: high performance, pure binary encoding, decoding and serialisation for Haskell
     ---------------------------------------------------------------------- 

The Binary Strike Team is pleased to announce release 0.4 of Data.Binary, the
pure, efficient binary serialisation library for Haskell, now available from
Hackage:
    
 tarball:    http://hackage.haskell.org/cgi-bin/hackage-scripts/package/binary/0.4
 darcs:      darcs get http://darcs.haskell.org/binary

The 'binary' package provides efficient serialisation of Haskell values
to and from lazy ByteStrings, and a low level layer for high performance
decoding and decoding binary data. ByteStrings constructed this way may then be
written to disk, written to the network, or further processed (e.g.  stored in
memory directly, or compressed in memory with zlib or bzlib).

*Very* high performance can be expected, with throughput over 1G/sec observed
in practice (good enough for most networking scenarios, we suspect).

Encoding and decoding are achieved by the functions:

    encode :: Binary a => a -> ByteString
    decode :: Binary a => ByteString -> a

which mirror the read/show functions. Convenience functions for serialising to
disk are also provided:

    encodeFile :: Binary a => FilePath -> a -> IO ()
    decodeFile :: Binary a => FilePath -> IO a

To serialise your Haskell data, all you need do is write an instance of
Binary for your type. For example, suppose in an interpreter we had the
data type:

    import Data.Binary
    import Control.Monad

    data Exp = IntE Int
             | OpE  String Exp Exp

We can serialise this to bytestring form with the following instance:

    instance Binary Exp where
        put (IntE i)          = putWord8 0 >> put i
        put (OpE s e1 e2)     = putWord8 1 >> put s >> put e1 >> put e2
        get = do tag <- getWord8
                 case tag of
                    0 -> liftM  IntE get
                    1 -> liftM3 OpE  get get get

The binary library has been heavily tuned for performance, particularly for
writing speed.  On average, Data.Binary is 10x faster than NewBinary, and 
has the advantage of a pure interface, and bytestring return values.

Binary was developed by a team of 8 during the Haskell Hackathon, Hac
07, in January 2007, and this maintainence release has taken place during the
second hackathon.

Binary is portable, using the foreign function interface and cpp, and has
been tested with Hugs and GHC.

Happy hacking!

-- Don


More information about the Haskell-Cafe mailing list