on master help sign in
Add machine benchmark and fix others
cc4743cHarendra Kumar
asyncly.cabal+2
@@ -68,6 +68,7 @@ benchmark bench
68 ghc-options: -Wall -fwarn-identities -fwarn-incomplete-record-updates
68 ghc-options: -Wall -fwarn-identities -fwarn-incomplete-record-updates
69 -fwarn-incomplete-uni-patterns -fwarn-tabs
69 -fwarn-incomplete-uni-patterns -fwarn-tabs
70 -O2 -funbox-strict-fields
70 -O2 -funbox-strict-fields
71+ -fno-ignore-asserts
71 build-depends:
72 build-depends:
72 asyncly
73 asyncly
73 , atomic-primops
74 , atomic-primops
@@ -76,6 +77,7 @@ benchmark bench
76 , conduit-combinators
77 , conduit-combinators
77 , criterion
78 , criterion
78 , io-streams
79 , io-streams
80+ , machines
79 , mtl
81 , mtl
80 , pipes
82 , pipes
81 , simple-conduit
83 , simple-conduit
benchmark/Main.hs+23−12
@@ -24,6 +24,7 @@ import System.IO.Unsafe (unsafePerformIO)
24import Transient.Internals as T
24import Transient.Internals as T
25import Transient.Indeterminism as T
25import Transient.Indeterminism as T
26
26
27+import Data.Machine as M
27import Asyncly as A
28import Asyncly as A
28
29
29main :: IO ()
30main :: IO ()
@@ -31,11 +32,12 @@ main = do
31 defaultMain [
32 defaultMain [
32 bgroup "basic" [ bench "asyncly" $ nfIO asyncly_basic
33 bgroup "basic" [ bench "asyncly" $ nfIO asyncly_basic
33 , bench "transient" $ nfIO transient_basic
34 , bench "transient" $ nfIO transient_basic
35+ , bench "machines" $ nfIO machines_basic
34 , bench "stream" $ nfIO stream_basic
36 , bench "stream" $ nfIO stream_basic
35− , bench "iostreams" $ nfIO iostreams_basic
36− , bench "pipes" $ nfIO pipes_basic
37 , bench "conduit" $ nfIO conduit_basic
37 , bench "conduit" $ nfIO conduit_basic
38+ , bench "pipes" $ nfIO pipes_basic
38− -- , bench "simple-conduit" $ nfIO simple_conduit_basic
39+ , bench "simple-conduit" $ nfIO simple_conduit_basic
40+ , bench "iostreams" $ nfIO iostreams_basic
39 -- , bench "fusion" $ nfIO fusion_basic
41 -- , bench "fusion" $ nfIO fusion_basic
40
42
41 ]
43 ]
@@ -47,7 +49,7 @@ map f x = return $ f x
47
49
48{-# INLINABLE filter #-}
50{-# INLINABLE filter #-}
49filter :: (Monad m, Alternative m) => (a -> Bool) -> a -> m a
51filter :: (Monad m, Alternative m) => (a -> Bool) -> a -> m a
50−filter cond x = guard (cond x) >> return x
52+filter cond x = guard (not $ cond x) >> return x
51
53
52{-# NOINLINE count #-}
54{-# NOINLINE count #-}
53count :: IORef Int
55count :: IORef Int
@@ -57,8 +59,7 @@ drop :: (MonadIO m, Alternative m) => Int -> Int -> m Int
57drop num x = do
59drop num x = do
58
60
59 mn <- liftIO $ atomicModifyIORefCAS count $ \n ->
61 mn <- liftIO $ atomicModifyIORefCAS count $ \n ->
60− let n' = n + 1
61− in if n' < num then (n + 1, False) else (n + 1, True)
62+ if n < num then (n + 1, False) else (n, True)
62 guard mn
63 guard mn
63 return x
64 return x
64
65
@@ -73,13 +74,13 @@ tdrop = Main.drop
73
74
74transient_basic :: IO Int
75transient_basic :: IO Int
75transient_basic = T.keep' $ T.threads 0 $ do
76transient_basic = T.keep' $ T.threads 0 $ do
77+ liftIO $ writeIORef count 0
76− xs <- T.group 499000 $ do
78+ xs <- T.group 499001 $ do
77− liftIO $ writeIORef count 0
78− T.choose [1..1000000 :: Int]
79+ T.choose [1..1000001 :: Int]
79 >>= tfilter even
80 >>= tfilter even
80 >>= tmap (+1)
81 >>= tmap (+1)
81 >>= tdrop 1000
82 >>= tdrop 1000
82− >>= tmap (+1)
83+ >>= tmap (+1)
83 >>= tfilter (\x -> x `mod` 2 == 0)
84 >>= tfilter (\x -> x `mod` 2 == 0)
84
85
85 assert (Prelude.length xs == 499000) $
86 assert (Prelude.length xs == 499000) $
@@ -104,10 +105,20 @@ asyncly_basic = do
104 >>= adrop 1000
105 >>= adrop 1000
105 >>= amap (+1)
106 >>= amap (+1)
106 >>= afilter (\x -> x `mod` 2 == 0)
107 >>= afilter (\x -> x `mod` 2 == 0)
107−
108 assert (Prelude.length xs == 499000) $
108 assert (Prelude.length xs == 499000) $
109 return (Prelude.length xs)
109 return (Prelude.length xs)
110
110
111+machines_basic :: IO Int
112+machines_basic = do
113+ xs <- M.runT $ M.source [1..1000000]
114+ M.~> M.filtered even
115+ M.~> M.mapping (+1)
116+ M.~> M.dropping 1000
117+ M.~> M.mapping (+1)
118+ M.~> M.filtered (\x -> x `mod` 2 == 0)
119+ assert (Prelude.length xs == 499000) $
120+ return (Prelude.length (xs ::[Int]))
121+
111pipes_basic :: IO Int
122pipes_basic :: IO Int
112pipes_basic = do
123pipes_basic = do
113 xs <- P.toListM $ P.each [1..1000000]
124 xs <- P.toListM $ P.each [1..1000000]
@@ -159,7 +170,7 @@ simple_conduit_basic = do
159stream_basic :: IO Int
170stream_basic :: IO Int
160stream_basic = do
171stream_basic = do
161 let ns = [1..1000000] :: [Int]
172 let ns = [1..1000000] :: [Int]
162− xs <- Str.toList $ Str.each ns
173+ xs Str.:> _ <- Str.toList $ Str.each ns
163 & Str.filter even
174 & Str.filter even
164 & Str.map (+1)
175 & Str.map (+1)
165 & Str.drop 1000
176 & Str.drop 1000