haskell / mwc-random

A very fast Haskell library for generating high quality pseudo-random numbers.
http://hackage.haskell.org/package/mwc-random
BSD 2-Clause "Simplified" License
54 stars 25 forks source link

Potential optimization for Word32 #62

Closed OlivierSohn closed 6 years ago

OlivierSohn commented 6 years ago

I assume it will be faster this way. I couldn't benchmark it however, as the benchmark didn't compile and I didn't know how to fix the errors (I believe the compile errors are preexistent to this commit, note that I use stack to compile).

Shimuuar commented 6 years ago

I think GHC is clever enough to compile it down to same code. But this patch simplifies compiler job at very least. I'll merge it tomorrow

OlivierSohn commented 6 years ago

@Shimuuar In such a case, I always say to myself "let's look at the generated core" and then I do something else :) But in that particular case, even looking at core may be "not enough" as the optimization might happen at a later stage... Oh well...

Shimuuar commented 6 years ago

Actually I expect that fromIntegral will be rewritten to id so everything will happen on core level. But I didn't check

OlivierSohn commented 6 years ago

I think you're right, it might be due to the 3rd RULE found here ?


{-# RULES
"fromIntegral/Word8->Word32"   fromIntegral = \(W8# x#) -> W32# x#
"fromIntegral/Word16->Word32"  fromIntegral = \(W16# x#) -> W32# x#
"fromIntegral/Word32->Word32"  fromIntegral = id :: Word32 -> Word32
"fromIntegral/Word32->Integer" fromIntegral = toInteger :: Word32 -> Integer
"fromIntegral/a->Word32"       fromIntegral = \x -> case fromIntegral x of W# x# -> W32# (narrow32Word# x#)
"fromIntegral/Word32->a"       fromIntegral = \(W32# x#) -> fromIntegral (W# x#)
  #-}
Shimuuar commented 6 years ago

Yep!