The following code (suggested by Reid Barton at Criterion causing memory consumption to explode, no CAFs in sight)
has a benchmark time which scales proportionally with num
when compiled with O0
optimization. However using O3
optimization seems to result in a benchmark time which is independent of num
. Where in the core is the result being cached, and what can I do to prevent it from being cached?
The code is :
{-# OPTIONS_GHC -fno-cse #-}
{-# LANGUAGE BangPatterns #-}
module Main where
import Criterion.Main
import Data.List
num :: Int
num = 100000000
lst :: a -> [Int]
lst _ = [1,2..num]
myadd :: Int -> Int -> Int
myadd !x !y = let !result = x + y in
result
mysum = foldl' myadd 0
main :: IO ()
main = defaultMain [
bgroup "summation"
[bench "mysum" $ whnf (mysum . lst) ()]
]
and the core is :
main7
main7 = unpackCString# "mysum"#
main8
main8 = unpackCString# "summation"#
Rec {
$wlgo
$wlgo =
\ ww_s6vW w_s6vT ->
case w_s6vT of _ {
[] -> ww_s6vW;
: x_a4dz xs_a4dA ->
case x_a4dz of _ { I# ipv_s4d4 ->
$wlgo (+# ww_s6vW ipv_s4d4) xs_a4dA
}
}
end Rec }
lst1
lst1 = efdtInt 1 2 100000000
lvl_r6yu
lvl_r6yu = case $wlgo 0 lst1 of ww_s6w5 { __DEFAULT -> I# ww_s6w5 }
Rec {
main_$s$wa
main_$s$wa =
\ sc_s6xB sc1_s6xC sc2_s6xD ->
case tagToEnum# (<=# sc1_s6xC 0) of _ {
False ->
case seq# lvl_r6yu sc2_s6xD of _ { (# ipv_a4BO, ipv1_a4BP #) ->
main_$s$wa sc_s6xB (-# sc1_s6xC 1) ipv_a4BO
};
True -> (# sc2_s6xD, () #)
}
end Rec }
main6
main6 =
\ w_s6w9 w1_s6wa ->
case w_s6w9 of _ { I64# ww1_s6wd ->
main_$s$wa () ww1_s6wd w1_s6wa
}
main5
main5 = Benchmark main7 (main6 `cast` ...)
main4
main4 = : main5 ([])
main3
main3 = BenchGroup main8 main4
main2
main2 = : main3 ([])
main1
main1 = \ eta_B1 -> defaultMain2 defaultConfig main2 eta_B1
main9
main9 = \ eta_B1 -> runMainIO1 (main1 `cast` ...) eta_B1
lst
lst = \ @ a_a40V _ -> lst1
main
main = main1 `cast` ...
myadd
myadd =
\ x_a3Io y_a3Ip ->
case x_a3Io of _ { I# ipv_s4d1 ->
case y_a3Ip of _ { I# ipv1_s4d4 -> I# (+# ipv_s4d1 ipv1_s4d4) }
}
mysum
mysum =
\ w_s6w2 ->
case $wlgo 0 w_s6w2 of ww_s6w5 { __DEFAULT -> I# ww_s6w5 }
num
num = I# 100000000
main
main = main9 `cast` ...
where I tagged -ddump-simpl -fforce-recomp -O3 -dsuppress-all
to the end of the ghc --make -no-link ...
command invoked
by cabal build
. I am using criterion 1.1.0.0 and GHC version 7.8.3.
The result is being cached in your
lvl_r6yu
. You can see thatlst1
is[0..num]
lifted out to the top level, and from$wlgo 0 lst1
it can be seen that the result of the summation is lifted out too.It's easier to see what's happening if we add the top level definition
foo = mysum . lst
, and then look at the core forfoo
. You can see there thatfoo
is a constant function returning the result of the summation.If we add
{-# OPTIONS -fno-full-laziness #-}
, then subexpressions will not be lifted, and therefore the benchmark will work as intended.It is a good idea in general when using
criterion
to control evaluation through the arguments supplied towhnf
. In our case:This works fine regardless of optimization or lifting.