To avoid CAF (resource sharing), I tried converting to function
with dummy argument, but no success (noCafB
).
I've read How to make a CAF not a CAF in Haskell? so tried noCafC
and noCafD
. When compiled with
-O0
, then functions with dummy argument did evaluated every time.
However, with -O2
, it seems that GHC converts those functions to
CAF. Is this intended behaviour (GHC's optimization)?
module Main where
import Debug.Trace
cafA :: [Integer]
cafA = trace "hi" (map (+1) $ [1..])
noCafB :: a -> [Integer]
noCafB _ = trace "hi" (map (+1) $ [1..])
noCafC :: a -> [Integer]
noCafC _ = trace "hi" (map (+1) $ [1..])
{-# NOINLINE noCafC #-}
noCafD :: a -> [Integer]
noCafD _ = trace "hi" (map (+1) $ myEnumFrom 0 1)
{-# NOINLINE noCafD #-}
myEnumFrom :: a -> Integer -> [Integer]
myEnumFrom _ n = enumFrom n
{-# NOINLINE myEnumFrom #-}
main :: IO ()
main = do
putStrLn "cafA"
print $ (cafA !! 1 + cafA !! 2)
putStrLn "noCafB"
print $ (noCafB 0 !! 1 + noCafB 0 !! 2)
putStrLn "noCafC"
print $ (noCafC 0 !! 1 + noCafC 0 !! 2)
putStrLn "noCafD"
print $ (noCafD 0 !! 1 + noCafD 0 !! 2)
Result with -O2
$ stack ghc -- --version
The Glorious Glasgow Haskell Compilation System, version 7.10.3
$ stack ghc -- -O2 cafTest.hs
[1 of 1] Compiling Main ( cafTest.hs, cafTest.o )
Linking cafTest ...
$ ./cafTest
cafA
hi
7
noCafB
7
noCafC
7
noCafD
hi
7
Result with -O0
$ stack ghc -- -O0 cafTest.hs
[1 of 1] Compiling Main ( cafTest.hs, cafTest.o )
Linking cafTest ...
$ ./cafTest
cafA
hi
7
noCafB
hi
hi
7
noCafC
hi
hi
7
noCafD
hi
hi
7
I've also tried without trace
but results was same. under -O2
, I found that the result of incInt
function is shared by inspecting profiling output. Why this behaviour?
incIntOrg :: [Integer]
incInt = map (+1) [1..]
incInt :: a -> [Integer] -- results IS shared. should it be?
incInt _ = map (+1) $ myEnum 0 1
{-# NOINLINE incInt #-}
myEnum :: a -> Integer -> [Integer]
myEnum _ n = enumFrom n
{-# NOINLINE myEnum #-}
main :: IO ()
main = do
print (incInt 0 !! 9999999)
print (incInt 0 !! 9999999)
print (incInt 0 !! 9999999)
Any comments will be appreciated deeply. Thanks.