on master help sign in
rename the runner APIs
17b30baHarendra Kumar
benchmark/Main.hs+21−20
@@ -3,17 +3,18 @@ module Main where
3
3
4import Criterion.Main (defaultMain, bgroup, bench, nfIO)
4import Criterion.Main (defaultMain, bgroup, bench, nfIO)
5import Control.Exception (assert)
5import Control.Exception (assert)
6+import Control.Monad.IO.Class (MonadIO (liftIO))
6--
7--
7-- import Fusion as F hiding ((&))
8-- import Fusion as F hiding ((&))
8import Data.Function ((&))
9import Data.Function ((&))
9
10
10−import Data.Conduit as C
11+import qualified Data.Conduit as C
11−import Data.Conduit.Combinators as C
12+import qualified Data.Conduit.Combinators as C
12−import Pipes as P
13+import qualified Pipes as P
13import qualified Pipes.Prelude as P
14import qualified Pipes.Prelude as P
14import qualified Streaming.Prelude as Str
15import qualified Streaming.Prelude as Str
15import qualified System.IO.Streams as IOS
16import qualified System.IO.Streams as IOS
16−import Conduit.Simple as S
17+import qualified Conduit.Simple as S
17
18
18-- Transient
19-- Transient
19import Control.Monad (guard)
20import Control.Monad (guard)
@@ -21,11 +22,11 @@ import Control.Applicative (Alternative)
21import Data.IORef (IORef, newIORef, writeIORef)
22import Data.IORef (IORef, newIORef, writeIORef)
22import Data.Atomics (atomicModifyIORefCAS)
23import Data.Atomics (atomicModifyIORefCAS)
23import System.IO.Unsafe (unsafePerformIO)
24import System.IO.Unsafe (unsafePerformIO)
24−import Transient.Internals as T
25+import qualified Transient.Internals as T
25−import Transient.Indeterminism as T
26+import qualified Transient.Indeterminism as T
26
27
27−import Data.Machine as M
28+import qualified Data.Machine as M
28−import Asyncly as A
29+import qualified Asyncly as A
29
30
30main :: IO ()
31main :: IO ()
31main = do
32main = do
@@ -63,13 +64,13 @@ drop num x = do
63 guard mn
64 guard mn
64 return x
65 return x
65
66
66−tmap :: (a -> Int) -> a -> TransIO Int
67+tmap :: (a -> Int) -> a -> T.TransIO Int
67tmap = Main.map
68tmap = Main.map
68
69
69−tfilter :: (a -> Bool) -> a -> TransIO a
70+tfilter :: (a -> Bool) -> a -> T.TransIO a
70tfilter = Main.filter
71tfilter = Main.filter
71
72
72−tdrop :: Int -> Int -> TransIO Int
73+tdrop :: Int -> Int -> T.TransIO Int
73tdrop = Main.drop
74tdrop = Main.drop
74
75
75transient_basic :: IO Int
76transient_basic :: IO Int
@@ -86,19 +87,19 @@ transient_basic = T.keep' $ T.threads 0 $ do
86 assert (Prelude.length xs == 499000) $
87 assert (Prelude.length xs == 499000) $
87 T.exit (Prelude.length xs)
88 T.exit (Prelude.length xs)
88
89
89−amap :: (a -> Int) -> a -> AsynclyT IO Int
90+amap :: (a -> Int) -> a -> A.AsynclyT IO Int
90amap = Main.map
91amap = Main.map
91
92
92−afilter :: (a -> Bool) -> a -> AsynclyT IO a
93+afilter :: (a -> Bool) -> a -> A.AsynclyT IO a
93afilter = Main.filter
94afilter = Main.filter
94
95
95−adrop :: Int -> Int -> AsynclyT IO Int
96+adrop :: Int -> Int -> A.AsynclyT IO Int
96adrop = Main.drop
97adrop = Main.drop
97
98
98asyncly_basic :: IO Int
99asyncly_basic :: IO Int
99asyncly_basic = do
100asyncly_basic = do
100 writeIORef count 0
101 writeIORef count 0
101− xs <- wait $ do
102+ xs <- A.toList $ do
102 A.each [1..1000000 :: Int]
103 A.each [1..1000000 :: Int]
103 >>= afilter even
104 >>= afilter even
104 >>= amap (+1)
105 >>= amap (+1)
@@ -122,11 +123,11 @@ machines_basic = do
122pipes_basic :: IO Int
123pipes_basic :: IO Int
123pipes_basic = do
124pipes_basic = do
124 xs <- P.toListM $ P.each [1..1000000]
125 xs <- P.toListM $ P.each [1..1000000]
125− >-> P.filter even
126+ P.>-> P.filter even
126− >-> P.map (+1)
127+ P.>-> P.map (+1)
127− >-> P.drop 1000
128+ P.>-> P.drop 1000
128− >-> P.map (+1)
129+ P.>-> P.map (+1)
129− >-> P.filter (\x -> x `mod` 2 == 0)
130+ P.>-> P.filter (\x -> x `mod` 2 == 0)
130 assert (Prelude.length xs == 499000) $
131 assert (Prelude.length xs == 499000) $
131 return (Prelude.length (xs ::[Int]))
132 return (Prelude.length (xs ::[Int]))
132
133
src/Asyncly.hs+2−2
@@ -125,8 +125,8 @@ module Asyncly
125 , AsyncT
125 , AsyncT
126
126
127 -- * Run
127 -- * Run
128− , wait
128+ , runAsyncly
129− , wait_
129+ , toList
130-- , threads
130-- , threads
131 , each
131 , each
132
132
src/Asyncly/RunAsync.hs+8−9
@@ -14,8 +14,8 @@
14-- Portability : GHC
14-- Portability : GHC
15--
15--
16module Asyncly.RunAsync
16module Asyncly.RunAsync
17− ( wait
17+ ( runAsyncly
18− , wait_
18+ , toList
19 , AsynclyT
19 , AsynclyT
20-- , threads
20-- , threads
21 , each
21 , each
@@ -76,9 +76,9 @@ getContext lref = do
76
76
77-- | Run an 'AsynclyT m' computation, wait for it to finish and discard the
77-- | Run an 'AsynclyT m' computation, wait for it to finish and discard the
78-- results.
78-- results.
79−{-# INLINABLE wait_ #-}
79+{-# INLINABLE runAsyncly #-}
80−wait_ :: forall m a. (MonadAsync m, MonadCatch m) => AsynclyT m a -> m ()
80+runAsyncly :: (MonadAsync m, MonadCatch m) => AsynclyT m a -> m ()
81−wait_ (AsynclyT m) = do
81+runAsyncly (AsynclyT m) = do
82 ctx <- liftIO $ getContext Nothing
82 ctx <- liftIO $ getContext Nothing
83 _ <- runStateT (run m) ctx
83 _ <- runStateT (run m) ctx
84 return ()
84 return ()
@@ -90,11 +90,11 @@ wait_ (AsynclyT m) = do
90
90
91data Step a r = Stop | Done a | Yield a r
91data Step a r = Stop | Done a | Yield a r
92
92
93−{-# INLINABLE wait #-}
94-- | Run an 'AsynclyT m' computation and collect the results generated by each
93-- | Run an 'AsynclyT m' computation and collect the results generated by each
95-- thread of the computation in a list.
94-- thread of the computation in a list.
95+{-# INLINABLE toList #-}
96−wait :: forall m a. (MonadAsync m, MonadCatch m) => AsynclyT m a -> m [a]
96+toList :: (MonadAsync m, MonadCatch m) => AsynclyT m a -> m [a]
97−wait (AsynclyT m) = liftIO (getContext Nothing) >>= run m
97+toList (AsynclyT m) = liftIO (getContext Nothing) >>= run m
98
98
99 where
99 where
100
100
@@ -111,7 +111,6 @@ wait (AsynclyT m) = liftIO (getContext Nothing) >>= run m
111 Stop -> return []
111 Stop -> return []
112 Done x -> return (x : [])
112 Done x -> return (x : [])
113
113
114−-- scatter
115{-# INLINABLE each #-}
114{-# INLINABLE each #-}
116each :: MonadAsync m => [a] -> AsynclyT m a
115each :: MonadAsync m => [a] -> AsynclyT m a
117each xs = foldr (<|>) empty $ map return xs
116each xs = foldr (<|>) empty $ map return xs