on master help sign in
Benchmark all variants of the State effect
commitc49ee8793f96af7b7a57d6bebb6b06f8790f41fb authorAndrzej Rybczak <andrzej@rybczak.net> dateFri 11 Jun 2021, 14:37 (5 years ago) parentc7a3cf7
2 files changed+63−15
bench/Countdown.hs+55−11 bench/Main.hs+8−4
bench/Countdown.hs+55−11
@@ -3,30 +3,37 @@ module Countdown where
33
44import Data.Functor.Identity
55
6−import qualified Control.Monad.Reader as M
7−import qualified Control.Monad.State as M
6+-- eff
7+#ifdef VERSION_eff
8+import qualified Control.Effect as L
9+#endif
810
11+-- effective
912import qualified Effective as E
1013import qualified Effective.Reader as E
1114import qualified Effective.State as E
15+import qualified Effective.State.MVar as ME
1216import qualified Effective.State.Dynamic as DE
1317
18+-- freer-simple
1419#ifdef VERSION_freer_simple
1520import qualified Control.Monad.Freer as FS
1621import qualified Control.Monad.Freer.Reader as FS
1722import qualified Control.Monad.Freer.State as FS
1823#endif
1924
20−#ifdef VERSION_eff
21−import qualified Control.Effect as L
22−#endif
23−
25+-- fused-effects
2426#ifdef VERSION_fused_effects
2527import qualified Control.Algebra as FE
2628import qualified Control.Carrier.Reader as FE
2729import qualified Control.Carrier.State.Strict as FE
2830#endif
2931
32+-- mtl
33+import qualified Control.Monad.Reader as M
34+import qualified Control.Monad.State as M
35+
36+-- polysemy
3037#ifdef VERSION_polysemy
3138import qualified Polysemy as P
3239import qualified Polysemy.Reader as P
@@ -66,7 +73,7 @@ countdownMtlDeep n = runIdentity
6673 runR = flip M.runReaderT ()
6774
6875----------------------------------------
69−-- effective (static)
76+-- effective (pure)
7077
7178programEffectiveStatic :: E.State Integer E.:> es => E.Eff es Integer
7279programEffectiveStatic = do
@@ -90,6 +97,31 @@ countdownEffectiveStaticDeep n = E.runEff
9097 where
9198 runR = E.runReader ()
9299
100+----------------------------------------
101+-- effective (mvar)
102+
103+programEffectiveMVar :: ME.State Integer E.:> es => E.Eff es Integer
104+programEffectiveMVar = do
105+ n <- ME.get @Integer
106+ if n <= 0
107+ then pure n
108+ else do
109+ ME.put (n - 1)
110+ programEffectiveMVar
111+{-# NOINLINE programEffectiveMVar #-}
112+
113+countdownEffectiveMVar :: Integer -> IO (Integer, Integer)
114+countdownEffectiveMVar n = E.runEff . ME.runState n $ programEffectiveMVar
115+
116+countdownEffectiveMVarDeep :: Integer -> IO (Integer, Integer)
117+countdownEffectiveMVarDeep n = E.runEff
118+ . runR . runR . runR . runR . runR . runR . runR . runR . runR . runR
119+ . ME.runState n
120+ . runR . runR . runR . runR . runR . runR . runR . runR . runR . runR
121+ $ programEffectiveMVar
122+ where
123+ runR = E.runReader ()
124+
93125----------------------------------------
94126-- effective (dynamic)
95127
@@ -103,11 +135,14 @@ programEffectiveDynamic = do
103135 programEffectiveDynamic
104136{-# NOINLINE programEffectiveDynamic #-}
105137
106−countdownEffectiveDynamic :: Integer -> IO (Integer, Integer)
107−countdownEffectiveDynamic n = E.runEff . DE.runState n $ programEffectiveDynamic
138+countdownEffectiveDynPure :: Integer -> IO (Integer, Integer)
139+countdownEffectiveDynPure n = E.runEff . DE.runState n $ programEffectiveDynamic
140+
141+countdownEffectiveDynMVar :: Integer -> IO (Integer, Integer)
142+countdownEffectiveDynMVar n = E.runEff . DE.runStateMVar n $ programEffectiveDynamic
108143
109−countdownEffectiveDynamicDeep :: Integer -> IO (Integer, Integer)
110−countdownEffectiveDynamicDeep n = E.runEff
144+countdownEffectiveDynPureDeep :: Integer -> IO (Integer, Integer)
145+countdownEffectiveDynPureDeep n = E.runEff
111146 . runR . runR . runR . runR . runR . runR . runR . runR . runR . runR
112147 . DE.runState n
113148 . runR . runR . runR . runR . runR . runR . runR . runR . runR . runR
@@ -115,6 +150,15 @@ countdownEffectiveDynamicDeep n = E.runEff
115150 where
116151 runR = E.runReader ()
117152
153+countdownEffectiveDynMVarDeep :: Integer -> IO (Integer, Integer)
154+countdownEffectiveDynMVarDeep n = E.runEff
155+ . runR . runR . runR . runR . runR . runR . runR . runR . runR . runR
156+ . DE.runStateMVar n
157+ . runR . runR . runR . runR . runR . runR . runR . runR . runR . runR
158+ $ programEffectiveDynamic
159+ where
160+ runR = E.runReader ()
161+
118162----------------------------------------
119163-- fused-effects
120164
bench/Main.hs+8−4
@@ -10,8 +10,10 @@ main = defaultMain
1010 [ bgroup "countdown"
1111 [ bgroup "reference" $ mkCountdown nf countdownRef
1212 , bgroup "shallow"
13− [ bgroup "effective (S)" $ mkCountdown nfAppIO countdownEffectiveStatic
14− , bgroup "effective (D)" $ mkCountdown nfAppIO countdownEffectiveDynamic
13+ [ bgroup "effective (SP)" $ mkCountdown nfAppIO countdownEffectiveStatic
14+ , bgroup "effective (SM)" $ mkCountdown nfAppIO countdownEffectiveMVar
15+ , bgroup "effective (DP)" $ mkCountdown nfAppIO countdownEffectiveDynPure
16+ , bgroup "effective (DM)" $ mkCountdown nfAppIO countdownEffectiveDynMVar
1517#ifdef VERSION_freer_simple
1618 , bgroup "freer-simple" $ mkCountdown nf countdownFreerSimple
1719#endif
@@ -27,8 +29,10 @@ main = defaultMain
2729#endif
2830 ]
2931 , bgroup "deep"
30− [ bgroup "effective (S)" $ mkCountdown nfAppIO countdownEffectiveStaticDeep
31− , bgroup "effective (D)" $ mkCountdown nfAppIO countdownEffectiveDynamicDeep
32+ [ bgroup "effective (SP)" $ mkCountdown nfAppIO countdownEffectiveStaticDeep
33+ , bgroup "effective (SM)" $ mkCountdown nfAppIO countdownEffectiveMVarDeep
34+ , bgroup "effective (DP)" $ mkCountdown nfAppIO countdownEffectiveDynPureDeep
35+ , bgroup "effective (DM)" $ mkCountdown nfAppIO countdownEffectiveDynMVarDeep
3236#ifdef VERSION_eff
3337 , bgroup "eff" $ mkCountdown nf countdownEffDeep
3438#endif