[Haskell-cafe] No Derived Read for Unboxed Arrays

SevenThunders mattcbro at earthlink.net
Thu Jan 25 19:55:44 EST 2007

Neil Mitchell wrote:
> I found that Read was maybe 30 times slower than the slowest binary
> serialisation method I could possibly think of. If performance matters
> to you, and the array is more than a few elements long, switching away
> from Read/Show should be the first step - before going for unboxed
> arrays.
> (But of course, having Read/Show defined for UArray may well be
> useful, and sounds a good idea)
> Thanks
> Neil
> _______________________________________________
> Haskell-Cafe mailing list
> Haskell-Cafe at haskell.org
> http://www.haskell.org/mailman/listinfo/haskell-cafe

No doubt any kind of binary serialization would be a lot faster.  In my
case, however,  I just wanted it to work out of the box.  I need to read in
about 5-10 arrays of only 1000 entries or so, saved in files.  I suspect
even the ascii parser could do that within a few seconds.

I thought maybe a simple hack of the existing Read instantiation for Array
would work, but I must be either too lazy or stupid to get it right the
first time. My problem is I'm not really familiar with how these parsing
routines work.

instance  (Ix a, Read a, Read b) => Read (UArray a b)  where
    readsPrec p = readParen (p > 9)
           (\r -> [(array b as, u) | ("array",s) <- lex r,
                                     (b,t)       <- reads s,
                                     (as,u)      <- reads t   ])

This fails to compile with the error:

    Could not deduce (IArray UArray b)
      from the context (Read (UArray a b), Ix a, Read a, Read b)
      arising from use of `array' at TunePerf.hs:23:20-29
    Possible fix:
      add (IArray UArray b) to the class or instance method `readsPrec'
      or add an instance declaration for (IArray UArray b)
    In the expression: array b as
    In the expression: (array b as, u)
    In the expression:
        [(array b as, u) |
             ("array", s) <- lex r, (b, t) <- reads s, (as, u) <- reads t]

Is there a simple hack to get this going?  I might just end up reading it
into a boxed array and then do the conversions. 

View this message in context: http://www.nabble.com/No-Derived-Read-for-Unboxed-Arrays-tf3119003.html#a8644431
Sent from the Haskell - Haskell-Cafe mailing list archive at Nabble.com.

More information about the Haskell-Cafe mailing list