Created
October 24, 2013 15:14
-
-
Save Tarrasch/7139032 to your computer and use it in GitHub Desktop.
reifyStack() seems to not be idempotent when called from same context.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
{-# LANGUAGE MagicHash #-} | |
module Main where | |
import GHC.IO (reifyStack, MyArray(..)) | |
import GHC.Prim | |
import GHC.Exts | |
import Unsafe.Coerce | |
import Data.Primitive.ByteArray | |
import Data.Primitive.Types | |
import System.Mem | |
import Data.Char | |
import Data.Int | |
import Control.Monad (forM_) | |
reifyStack' :: IO (ByteArray) | |
reifyStack' = do | |
myba <- reifyStack | |
return $ unsafeCoerce myba | |
mapByteArray :: Prim a => (a -> b) -> ByteArray -> [b] | |
mapByteArray f ba = map (f ∘ get) [0..n - 1] | |
where n = sizeofByteArray ba | |
get i = indexByteArray ba i | |
instance Show Addr where | |
show = show ∘ (unsafeCoerce :: Addr -> Int) | |
printStackTrace :: ByteArray -> IO() | |
printStackTrace ba = do | |
forM_ [0..n-1] (λi -> putStrLn $ " " ++ show (get i)) | |
where n = sizeofByteArray ba | |
get :: Int -> Addr | |
get i = indexByteArray ba i | |
----- Main below vvvvvvvvv | |
-- | |
-- | |
main :: IO() | |
main = do print 1 | |
a | |
print 2 | |
a, b :: IO() | |
a = do print 10 | |
b | |
print 20 | |
b = do print 100 | |
putStrLn $ "Running time 1" | |
reifyStack' >>= printStackTrace | |
putStrLn $ "Running time 2" | |
reifyStack' >>= printStackTrace | |
putStrLn $ "Running time 3" | |
reifyStack' >>= printStackTrace | |
print 200 | |
{- | |
Output: | |
$ ./inplace/bin/ghc-stage2 -O2 --make my/ReifyMini.hs && ./my/ReifyMini | |
[1 of 1] Compiling Main ( my/ReifyMini.hs, my/ReifyMini.o ) | |
Linking my/ReifyMini ... | |
1 | |
10 | |
100 | |
Running time 1 | |
4490520 | |
4279904 | |
4279904 | |
4279904 | |
4279904 | |
4279904 | |
8301784 | |
Running time 2 | |
4490520 | |
4479888 | |
0 | |
140719980249088 | |
140719980277313 | |
5334584 | |
140719980277328 | |
Running time 3 | |
4490520 | |
4279904 | |
4279904 | |
4279904 | |
11166241 | |
11256345 | |
11256521 | |
200 | |
20 | |
-} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment