Array performance is killing me

Previous Topic Next Topic
 
classic Classic list List threaded Threaded
2 messages Options
Reply | Threaded
Open this post in threaded view
|

Array performance is killing me

Joel Reymont
Folks,

I managed to reduce my memory consumption but I have a performance  
problem now. This is a profiling report from my app. -O was not  
supplied when compiling the library and the relevant modules are at  
http://wagerlabs.com/array.tgz.

COST CENTRE                    MODULE               %time %alloc
byteArrayFromPtr               Script.Array          39.1   43.9
readBits                       Script.Array          23.6   25.5
emptyByteArray                 Script.Array           7.6    6.0
endian#2                       Script.Endian          4.4    4.2
bytearray                      Script.Pickle          3.9    3.4
copyMArray                     Script.Array           2.8    3.4
sequ                           Script.Pickle          2.7    2.0
withByteArray                  Script.Array           2.3    2.1
num#2                          Script.Pickle          1.3    1.7
wstr#5                         Script.Endian          1.1    0.6
byteSize                       Script.Pickle          1.1    0.5

I run into trouble with packets of ~50K that consist of a header (a  
few bytes) and compressed data. The compressed data has to be  
uncompressed and then parsed again. Overall, this takes a copy from  
array (I use hGetArray) into Ptr Word8 to be supplied to ZLib's  
compress and then another copy from Ptr Word8 into a byte array for  
parsing.

readBits is supposed to extract numbers from a byte array and can be  
found in Array.hs. uncompress is in ZLib.hs and the endian bits in  
Endian.hs. The code should compile without problems.

Please help me optimize this!

        Thanks, Joel

--
http://wagerlabs.com/





_______________________________________________
Haskell-Cafe mailing list
[hidden email]
http://www.haskell.org/mailman/listinfo/haskell-cafe
Reply | Threaded
Open this post in threaded view
|

Re: Array performance is killing me

Joel Reymont
Converting to Ptr Word8 and storables took about an hour and gave me  
a more favorable profile.

COST CENTRE                    MODULE               %time %alloc
reverse_                       Script.Endian         14.4   13.4
sequ                           Script.Pickle         14.3    9.3
read                           Script.PokerClient    10.9    9.1
storable                       Script.Pickle         10.8   10.7
timestamp                      Script.Trace           7.1    3.8
dispatch                       Script.Engine          6.3    4.3
unstuff                        Script.PokerClient     4.4    2.9
appU_wstr                      Script.Endian          4.4    3.2
trace_                         Script.Trace           3.9    1.2
readQ                          Script.Queue           3.3   13.0
lift                           Script.Pickle          2.9    5.3
writeQ                         Script.Queue           2.8    2.7
wrap                           Script.Pickle          2.2    1.2
$!!                            Script.DeepSeq         1.8    0.5
puTableInfo                    Script.PicklePlus      1.7    2.6
puCommand                      Script.PickleCmd       1.1    0.1
fetch                          Script.Engine          0.9   11.6
post_                          Script.Engine          0.2    1.7

Any suggestions on how to optimize reverseBytes below? Would  
specializing it help?

isBigEndian :: Bool
isBigEndian = $(lift $ (1::CChar) /= (unsafePerformIO
                                      $ with (1::CInt)
                                      $ peekByteOff `flip` 0) ) :: Bool

reverse_ :: (Storable a, Endian a) => a -> a
reverse_ a =
     if isBigEndian
        then reverseBytes a
        else a

--- Endian conversion

class (Bits a, Integral a, Num a) => Endian a where
     reverseBytes :: a -> a

instance Endian Word16 where
     reverseBytes v = (v `shiftR` 8) + ((v .&. 0xFF) `shiftL` 8)

instance Endian Int16 where
     reverseBytes v = (v `shiftR` 8) + ((v .&. 0xFF) `shiftL` 8)

instance Endian Word32 where
     reverseBytes v = ( v                 `shiftR` 24) +
                 ((v .&. 0x000000FF) `shiftL` 24) +
                 ((v .&. 0x0000FF00) `shiftL` 8) +
                 ((v .&. 0x00FF0000) `shiftR` 8)

instance Endian Int32 where
     reverseBytes v = ( v                 `shiftR` 24) +
                 ((v .&. 0x000000FF) `shiftL` 24) +
                 ((v .&. 0x0000FF00) `shiftL` 8) +
                 ((v .&. 0x00FF0000) `shiftR` 8)

instance Endian Word64 where
     reverseBytes v = ( v                         `shiftR` 56) +
                 ((v .&. 0x00000000000000FF) `shiftL` 56) +
                 ((v .&. 0x00FF000000000000) `shiftR` 40) +
                 ((v .&. 0x000000000000FF00) `shiftL` 40) +
                 ((v .&. 0x0000FF0000000000) `shiftR` 24) +
                 ((v .&. 0x0000000000FF0000) `shiftL` 24) +
                 ((v .&. 0x000000FF00000000) `shiftR` 8) +
                 ((v .&. 0x00000000FF000000) `shiftL` 8)

--
http://wagerlabs.com/





_______________________________________________
Haskell-Cafe mailing list
[hidden email]
http://www.haskell.org/mailman/listinfo/haskell-cafe