[Haskell-cafe] forLoop + strict State monad is much faster than foldl'

Daniel Fischer daniel.is.fischer at googlemail.com
Wed Apr 30 12:35:49 UTC 2014


On Wednesday 30 April 2014, 13:42:36, Joachim Breitner wrote:
> Hi,
> 
> Am Dienstag, den 29.04.2014, 18:16 -0700 schrieb John Lato:
> > So this is interesting.  The forLoop code gets compiled into a nice
> > loop in core over unboxed Ints.  The foldl' function OTOH still goes
> > via a list.  I expect it's not foldl' itself that's slow, rather that
> > it doesn't fuse with the list producers in current GHCs.  This may be
> > improved in the future.  Especially as the vectorized foldl' does
> > fuse.
> 
> This should have improved in GHC 7.9, where I have fixed
> https://ghc.haskell.org/trac/ghc/ticket/7994 (making foldl a good
> consumer).

Confirmed, foldl' (+) 0 [1 .. 5000000 :: Int] gets the very nice core

Rec {
Main.$wgo [Occ=LoopBreaker]
  :: GHC.Prim.Int# -> GHC.Prim.Int# -> GHC.Prim.Int#
[GblId, Arity=2, Caf=NoCafRefs, Str=DmdType <S,1*U><L,U>]
Main.$wgo =
  \ (w :: GHC.Prim.Int#) (ww :: GHC.Prim.Int#) ->
    case w of wild {
      __DEFAULT -> Main.$wgo (GHC.Prim.+# wild 1) (GHC.Prim.+# ww wild);
      5000000 -> GHC.Prim.+# ww 5000000
    }
end Rec }

with 7.9.20140425. The core for Integer is naturally not quite as nice, but 
still the list is eliminated.

Thanks, Joachim.

Cheers,
Daniel


More information about the Haskell-Cafe mailing list