Word foldl'没有优化和Int foldl'

Kev*_*vin 14 optimization haskell ghc

import Data.List

test :: Int -> Int
test n = foldl' (+) 0 [1..n]

main :: IO ()
main = do
  print $ test $ 10^8
Run Code Online (Sandbox Code Playgroud)

GHC优化了上面的代码,以至于垃圾收集器甚至不需要做任何事情:

$ ghc -rtsopts -O2 testInt && ./testInt +RTS -s
[1 of 1] Compiling Main             ( testInt.hs, testInt.o )
Linking testInt ...
5000000050000000
          51,752 bytes allocated in the heap
           3,480 bytes copied during GC
          44,384 bytes maximum residency (1 sample(s))
          17,056 bytes maximum slop
               1 MB total memory in use (0 MB lost due to fragmentation)

                                     Tot time (elapsed)  Avg pause  Max pause
  Gen  0         0 colls,     0 par    0.000s   0.000s     0.0000s    0.0000s
  Gen  1         1 colls,     0 par    0.000s   0.000s     0.0001s    0.0001s

  INIT    time    0.000s  (  0.000s elapsed)
  MUT     time    0.101s  (  0.101s elapsed)
  GC      time    0.000s  (  0.000s elapsed)
  EXIT    time    0.000s  (  0.000s elapsed)
  Total   time    0.103s  (  0.102s elapsed)

  %GC     time       0.1%  (0.1% elapsed)

  Alloc rate    511,162 bytes per MUT second

  Productivity  99.8% of total user, 100.9% of total elapsed
Run Code Online (Sandbox Code Playgroud)

但是,如果我更改testto 的类型test :: Word -> Word,则会产生大量垃圾,代码运行速度会慢40倍.

ghc -rtsopts -O2 testWord && ./testWord +RTS -s
[1 of 1] Compiling Main             ( testWord.hs, testWord.o )
Linking testWord ...
5000000050000000
  11,200,051,784 bytes allocated in the heap
       1,055,520 bytes copied during GC
          44,384 bytes maximum residency (2 sample(s))
          21,152 bytes maximum slop
               1 MB total memory in use (0 MB lost due to fragmentation)

                                     Tot time (elapsed)  Avg pause  Max pause
  Gen  0     21700 colls,     0 par    0.077s   0.073s     0.0000s    0.0000s
  Gen  1         2 colls,     0 par    0.000s   0.000s     0.0001s    0.0001s

  INIT    time    0.000s  (  0.000s elapsed)
  MUT     time    4.551s  (  4.556s elapsed)
  GC      time    0.077s  (  0.073s elapsed)
  EXIT    time    0.000s  (  0.000s elapsed)
  Total   time    4.630s  (  4.630s elapsed)

  %GC     time       1.7%  (1.6% elapsed)

  Alloc rate    2,460,957,186 bytes per MUT second

  Productivity  98.3% of total user, 98.3% of total elapsed
Run Code Online (Sandbox Code Playgroud)

为什么会这样?我预计性能几乎相同?(我在x86_64 GNU/Linux上使用GHC版本8.0.1)

编辑:我提交了一个错误:https://ghc.haskell.org/trac/ghc/ticket/12354#ticket

Tho*_*son 10

这可能主要是(但不是唯一的)由于Int而不是Word存在的重写规则.我之所以这么说,是因为如果我们使用-fno-enable-rewrite-rules这个Int案例,我们会得到一个更接近但却不那么糟糕的时间Word.

% ghc -O2 so.hs -fforce-recomp -fno-enable-rewrite-rules && time ./so
[1 of 1] Compiling Main             ( so.hs, so.o )
Linking so ...
5000000050000000
./so  1.45s user 0.03s system 99% cpu 1.489 total
Run Code Online (Sandbox Code Playgroud)

如果我们使用-ddump-rule-rewrites和转换这些规则来转储重写规则,那么我们会看到一个规则在Int案例中触发而不是Word案例:

 Rule: fold/build
 Before: GHC.Base.foldr
 ...
Run Code Online (Sandbox Code Playgroud)

该特定规则在Base 4.9 GHC.Base第823行(NB我实际上自己使用GHC 7.10)并没有Int明确提及.我很好奇为什么它没有开火Word,但现在没有时间进一步调查.

  • 我没有调查过,但是我把我的赌注押在'Enum Word`实例上,与'Enum Int`实例不同,防止枚举与`foldr`融合. (4认同)