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

Niklas Hambüchen mail at nh2.me
Wed Apr 30 14:13:58 UTC 2014

On 30/04/14 12:41, Joachim Breitner wrote:
> Did you look at the GHC Core generated? When starting to investigate
> performance issues, reading Core simply is required – otherwise it’s
> like search for a lost plain while refusing to look under the water
> surface.

Of course. A short look with ghc-core on

  import Data.List
  main = print (foldl' (*) 0 [0..100000::Int])

reveals that the original list traversal is still present:

  $wlgo =
    \ (ww_s1HD :: Int#) (w_s1HF :: [Int]) ->
      case w_s1HF of _ {
        [] -> ww_s1HD;
        : x_asn xs_aso ->
          case x_asn of _ { I# y_asz ->
          $wlgo (*# ww_s1HD y_asz) xs_aso

There is not much point investigating further until collecting the
responses from this list - as it turned out, you already fixed the
problem. Thanks!

For performance-critical code, it seems sensible for me to stick to my
`forLoop` for now, as it works quite well with the existing GHCs and
much less brain seems to be necessary to get that compile down to fast code.

More information about the Haskell-Cafe mailing list