[Haskell-cafe] Optimizing cellular automata & the beauty of
unlifted types
Felipe Lessa
felipe.lessa at gmail.com
Fri Dec 21 14:22:30 EST 2007
On Dec 21, 2007 3:00 PM, Justin Bailey <jgbailey at gmail.com> wrote:
> It really did help. I started with an implementation that used Ints,
> and this sped the program up by at least 2x. I think that's because of
> the bit-manipulation I'm doing. For example, Data.Bits defines the
> bitwise and operation on Ints as:
>
> (I# x#) .&. (I# y#) = I# (word2Int# (int2Word# x# `and#` int2Word# y#))
>
> Which you can see has 3 conversions in it. The I#
> deconstruction/construction is also suspicious to me, but I don't know
> if there are performance costs there or not. Regardless, using Word#
> directly lets me write (assuming w1# and w2# are already words):
>
> w1# `and#` w2#
>
> Maybe better rewrite rules in the Data.Bits library would eliminate
> unnecessary conversions and this wouldn't be necessary
Wouldn't it be sufficient to use Data.Word and rely on GHC unboxing
capabilities? Compiling
import Data.Bits
import Data.Word
main = do
a <- getLine
b <- getLine
print (read a .&. read b :: Word)
with GHC 6.6.1 gives me
(case GHC.Read.read @ GHC.Word.Word GHC.Word.$f40 a_afE
of wild1_a1tT { GHC.Word.W# x#_a1tV ->
case GHC.Read.read @ GHC.Word.Word GHC.Word.$f40 a87_a1ub
of wild11_a1tW { GHC.Word.W# y#_a1tY ->
GHC.Word.$w$dmshow1 (GHC.Prim.and# x#_a1tV y#_a1tY)
}
})
which of course does the right thing.
Cheers,
--
Felipe.
More information about the Haskell-Cafe
mailing list