Skip to content

Commit

Permalink
reader benchmark
Browse files Browse the repository at this point in the history
  • Loading branch information
janmasrovira committed Feb 9, 2024
1 parent 1ad530b commit 7a5e58b
Show file tree
Hide file tree
Showing 6 changed files with 117 additions and 6 deletions.
17 changes: 17 additions & 0 deletions bench2/Benchmark/Effect.hs
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
module Benchmark.Effect where

import Benchmark.Effect.Output qualified as Output
import Benchmark.Effect.Reader qualified as Reader
import Benchmark.Effect.ReaderH qualified as ReaderH
import Benchmark.Effect.State qualified as State
import Test.Tasty.Bench

bm :: Benchmark
bm =
bgroup
"Effect"
[ Output.bm,
State.bm,
ReaderH.bm,
Reader.bm
]
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
module Benchmark.Output where
module Benchmark.Effect.Output where

import Juvix.Prelude
import Juvix.Prelude.Effects (Eff, (:>))
Expand Down
50 changes: 50 additions & 0 deletions bench2/Benchmark/Effect/Reader.hs
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
module Benchmark.Effect.Reader where

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

bm :: Benchmark
bm =
bgroup
"Reader (First order)"
[ bench "Eff Reader (Static)" $ nf countEff k,
bench "Sem Reader" $ nf countSem k,
bench "Raw Reader" $ nf countRaw k
]

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

c :: Natural
c = 5

countRaw :: Natural -> Natural
countRaw = sum' . go []
where
i :: Natural = 5
go :: [Natural] -> Natural -> [Natural]
go acc = \case
0 -> acc
m -> go (i : acc) (pred m)

countEff :: Natural -> Natural
countEff = sum' . E.runPureEff . E.runReader c . go []
where
go :: (E.Reader Natural :> r) => [Natural] -> Natural -> Eff r [Natural]
go acc = \case
0 -> return acc
n -> do
i <- E.ask
go (i : acc) (pred n)

countSem :: Natural -> Natural
countSem = sum' . run . runReader c . go []
where
go :: (Member (Reader Natural) r) => [Natural] -> Natural -> Sem r [Natural]
go acc = \case
0 -> return acc
n -> do
i <- ask
go (i : acc) (pred n)
46 changes: 46 additions & 0 deletions bench2/Benchmark/Effect/ReaderH.hs
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
module Benchmark.Effect.ReaderH where

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

bm :: Benchmark
bm =
bgroup
"Reader (Higher order)"
[ bench "Eff Reader (Static)" $ nf countEff k,
bench "Sem Reader" $ nf countSem k,
bench "Raw Reader" $ nf countRaw k
]

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

countRaw :: Natural -> Natural
countRaw = sum' . go []
where
go :: [Natural] -> Natural -> [Natural]
go acc = \case
0 -> acc
m -> go (m : acc) (pred m)

countEff :: Natural -> Natural
countEff x = sum' . E.runPureEff . E.runReader x $ go []
where
go :: (E.Reader Natural :> r) => [Natural] -> Eff r [Natural]
go acc = do
n <- E.ask
case n of
0 -> return acc
m -> E.local @Natural pred (go (m : acc))

countSem :: Natural -> Natural
countSem x = sum . run . runReader x $ go []
where
go :: (Members '[Reader Natural] r) => [Natural] -> Sem r [Natural]
go acc = do
n :: Natural <- ask
case n of
0 -> return acc
m -> local @Natural pred (go (m : acc))
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
module Benchmark.State where
module Benchmark.Effect.State where

import Juvix.Prelude
import Juvix.Prelude.Effects (Eff, (:>))
Expand Down
6 changes: 2 additions & 4 deletions bench2/Main.hs
Original file line number Diff line number Diff line change
@@ -1,13 +1,11 @@
module Main where

import Benchmark.Output qualified as Output
import Benchmark.State qualified as State
import Benchmark.Effect qualified as Effect
import Juvix.Prelude
import Test.Tasty.Bench

main :: IO ()
main =
defaultMain
[ Output.bm,
State.bm
[ Effect.bm
]

0 comments on commit 7a5e58b

Please sign in to comment.