Skip to content

Instantly share code, notes, and snippets.

@carymrobbins
Last active July 29, 2019 16:46

Revisions

  1. carymrobbins revised this gist Jul 29, 2019. 1 changed file with 28 additions and 0 deletions.
    28 changes: 28 additions & 0 deletions NoInlineWhere.hs
    Original file line number Diff line number Diff line change
    @@ -6,6 +6,10 @@ main = do
    thing 2
    thing 3

    thing' 1
    thing' 2
    thing' 3

    thing :: Int -> IO ()
    thing n = print $ n + cachedExpr
    where
    @@ -15,20 +19,44 @@ thing n = print $ n + cachedExpr
    putStrLn "computing cachedExpr..."
    pure 5


    thing' :: Int -> IO ()
    thing' n = print $ n + cachedExpr'

    {-# NOINLINE cachedExpr' #-}
    cachedExpr' :: Int
    cachedExpr' = unsafePerformIO $ do
    putStrLn "computing cachedExpr'..."
    pure 5

    {-
    % ghc -O0 NoInlineWhere.hs -o noinlinewhere && ./noinlinewhere
    [1 of 1] Compiling Main ( NoInlineWhere.hs, NoInlineWhere.o )
    Linking noinlinewhere ...
    computing cachedExpr...
    6
    computing cachedExpr...
    7
    computing cachedExpr...
    8
    computing cachedExpr'...
    6
    7
    8
    % rm noinlinewhere NoInlineWhere.{hi,o}
    % ghc -O2 NoInlineWhere.hs -o noinlinewhere && ./noinlinewhere
    [1 of 1] Compiling Main ( NoInlineWhere.hs, NoInlineWhere.o )
    Linking noinlinewhere ...
    computing cachedExpr...
    6
    7
    8
    computing cachedExpr'...
    6
    7
    8
    -}
  2. carymrobbins created this gist Jul 29, 2019.
    34 changes: 34 additions & 0 deletions NoInlineWhere.hs
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,34 @@
    import System.IO.Unsafe

    main :: IO ()
    main = do
    thing 1
    thing 2
    thing 3

    thing :: Int -> IO ()
    thing n = print $ n + cachedExpr
    where
    {-# NOINLINE cachedExpr #-}
    cachedExpr :: Int
    cachedExpr = unsafePerformIO $ do
    putStrLn "computing cachedExpr..."
    pure 5

    {-
    % ghc -O0 NoInlineWhere.hs -o noinlinewhere && ./noinlinewhere
    computing cachedExpr...
    6
    computing cachedExpr...
    7
    computing cachedExpr...
    8
    % ghc -O2 NoInlineWhere.hs -o noinlinewhere && ./noinlinewhere
    computing cachedExpr...
    6
    7
    8
    -}