Skip to content

Commit

Permalink
benchmark embed IO
Browse files Browse the repository at this point in the history
  • Loading branch information
janmasrovira committed Feb 12, 2024
1 parent c73f7b2 commit 69ce498
Show file tree
Hide file tree
Showing 2 changed files with 51 additions and 0 deletions.
2 changes: 2 additions & 0 deletions bench2/Benchmark/Effect.hs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
module Benchmark.Effect where

import Benchmark.Effect.EmbedIO qualified as EmbedIO
import Benchmark.Effect.Output qualified as Output
import Benchmark.Effect.Reader qualified as Reader
import Benchmark.Effect.ReaderH qualified as ReaderH
Expand All @@ -13,5 +14,6 @@ bm =
[ Output.bm,
State.bm,
ReaderH.bm,
EmbedIO.bm,
Reader.bm
]
49 changes: 49 additions & 0 deletions bench2/Benchmark/Effect/EmbedIO.hs
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
module Benchmark.Effect.EmbedIO where

import Juvix.Prelude
import Juvix.Prelude.Effects (Eff)
import Juvix.Prelude.Effects qualified as E
import Test.Tasty.Bench

bm :: Benchmark
bm =
bgroup
"Embed IO"
[ bench "Raw IO" $ nfAppIO countRaw k,
bench "Eff RIO" $ nfAppIO countEff k,
bench "Sem Embed IO" $ nfAppIO countSem k
]

k :: Natural
k = 2 ^ (23 :: Natural)

c :: Char
c = 'x'

countRaw :: Natural -> IO ()
countRaw = countHelper

countHelper :: forall m. (MonadMask m, MonadIO m) => Natural -> m ()
countHelper n =
withSystemTempFile "tmp" $ \_ h -> go h n
where
go :: Handle -> Natural -> m ()
go h = \case
0 -> return ()
a -> liftIO (hPutChar h c) >> go h (pred a)

countSem :: Natural -> IO ()
countSem n = withSystemTempFile "tmp" $ \_ h -> runM (go h n)
where
go :: Handle -> Natural -> Sem '[Embed IO] ()
go h = \case
0 -> return ()
a -> liftIO (hPutChar h c) >> go h (pred a)

countEff :: Natural -> IO ()
countEff n = withSystemTempFile "tmp" $ \_ h -> E.runEff (go h n)
where
go :: Handle -> Natural -> Eff '[E.IOE] ()
go h = \case
0 -> return ()
a -> liftIO (hPutChar h c) >> go h (pred a)

0 comments on commit 69ce498

Please sign in to comment.