Skip to content

Commit

Permalink
Add free SN wordcount benchmark (slightly slower)
Browse files Browse the repository at this point in the history
  • Loading branch information
turion committed Apr 10, 2024
1 parent 3492c68 commit edce964
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 1 deletion.
15 changes: 15 additions & 0 deletions rhine/bench/WordCount.hs
Original file line number Diff line number Diff line change
Expand Up @@ -28,12 +28,15 @@ import Data.MonadicStreamFunction qualified as Dunai
-- rhine

import Control.Monad.Trans.MSF.Except qualified as Dunai
import Data.Profunctor (Profunctor (lmap))
import FRP.Rhine
import FRP.Rhine.Clock.Except (
DelayIOError,
ExceptClock (..),
delayIOError,
)
import FRP.Rhine.Rhine.Free qualified as Free
import FRP.Rhine.SN.Free (At (..))
import Paths_rhine

-- * Top level benchmarks
Expand All @@ -43,6 +46,7 @@ benchmarks =
bgroup
"WordCount"
[ bench "rhine" $ nfIO rhineWordCount
, bench "rhine (free sn)" $ nfIO rhineFreeWordCount
, bench "dunai" $ nfIO dunaiWordCount
, bgroup
"Text"
Expand Down Expand Up @@ -85,6 +89,17 @@ rhineWordCount = do
words <- mappendS -< either (const 0) (Sum . length . words) lineOrStop
throwOn' -< (either isEOFError (const False) lineOrStop, Right $ getSum words)

rhineFreeWordCount :: IO Int
rhineFreeWordCount = do
Left (Right count) <- withInput $ runExceptT $ Free.flow $ lmap Present $ wc Free.@@ wordCountClock
return count
where
wc :: ClSF (ExceptT (Either IOError Int) IO) WordCountClock () ()
wc = proc _ -> do
lineOrStop <- tagS -< ()
words <- mappendS -< either (const 0) (Sum . length . words) lineOrStop
throwOn' -< (either isEOFError (const False) lineOrStop, Right $ getSum words)

{- | Idiomatic dunai implementation.
Compared to Rhine, this doesn't have the overhead of clocks and exception handling.
Expand Down
2 changes: 1 addition & 1 deletion rhine/rhine.cabal
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,7 @@ common opts
text >=1.2 && <2.1,
transformers >=0.5,
vector-sized >=1.4,
profunctors ^>= 5.6.2,

if flag(dev)
ghc-options: -Werror
Expand Down Expand Up @@ -151,7 +152,6 @@ library
transformers >=0.5,
free-category ^>= 0.0.4.5,
sop-core ^>= 0.5.0.2,
profunctors ^>= 5.6.2,

-- Directories containing source files.
hs-source-dirs: src
Expand Down

0 comments on commit edce964

Please sign in to comment.