Binary question
Hal Daume III
hdaume@ISI.EDU
Wed, 12 Jun 2002 09:13:23 -0700 (PDT)
There are a few Binarys for GHC out there; I'm not sure which one you're
using, but the one I use (that compiles fine) you can grab from me at:
http://www.isi.edu/~hdaume/hnlp/Binary.hs (you'll also need
hnlp/FastMutInt.lhs)
> In the meantime i have a temporary version of binary but its
> performance is not what i would like. Let me paste some of it
>
> [snip]
This is certainly not the same as the one I'm using. I don't know if mine
is compatible with DrIFT (or whatever the appropriate capitalization is),
but it's really quite trivial to write your own instances. An instance
for FiniteMap is:
> instance (Ord a, Binary a, Binary b) => Binary (FiniteMap a b) where
> put_ bh = put_ bh . fmToList
> get bh = get bh >>= return . listToFM
The most unfortunate thing I found with this version of Binary is that
there's not as built in instance of Binary Double. My instance is:
> instance Binary Double where
> put_ bh d = put_ bh (doubleToInts d)
> get bh = get bh >>= return . intsToDouble
Where the appropriate (admittedly hack) definitions used in there are:
> doubleToInts d = runST (
> do arr <- newDoubleArray (1,2)
> writeDoubleArray arr 1 d
> i1 <- readIntArray arr 1
> i2 <- readIntArray arr 2
> return (i1,i2))
>
> intsToDouble (i1,i2) = runST (
> do arr <- newDoubleArray (1,2)
> writeIntArray arr 1 i1
> writeIntArray arr 2 i2
> readDoubleArray arr 1)
> My second question is how to optimize this piece of code. I suppose
> that the main performance bottleneck is the use of hPutChar. I did some
> profiling but some guidance will be really helpful.
I'll let someone else take that :)