Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add benchmarks #285

Merged
merged 3 commits into from
Mar 29, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 5 additions & 3 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,7 @@ jobs:
- name: Install dependencies
# If we had an exact cache hit, the dependencies will be up to date.
if: steps.cache.outputs.cache-hit != 'true'
run: cabal build all --enable-tests --only-dependencies
run: cabal build all --enable-tests --enable-benchmarks --only-dependencies

# Cache dependencies already here, so that we do not have to rebuild them should the subsequent steps fail.
- name: Save cached dependencies
Expand All @@ -95,9 +95,11 @@ jobs:
key: ${{ steps.cache.outputs.cache-primary-key }}

- name: Cabal build packages
run: cabal build all --enable-tests -fdev
run: cabal build all --enable-tests --enable-benchmarks -fdev
- name: Cabal test
run: cabal test all --enable-tests --test-show-details=Always
- name: Cabal bench
run: cabal bench all

build-stack:
runs-on: ubuntu-latest
Expand Down Expand Up @@ -145,7 +147,7 @@ jobs:
- uses: actions/checkout@v4
- uses: tfausak/cabal-gild-setup-action@v2
with:
version: 1.0.2.4
version: 1.3.0.1
- run: ./cabal_check.sh

success:
Expand Down
8 changes: 8 additions & 0 deletions rhine/bench/Main.hs
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
-- criterion
import Criterion.Main

-- rhine
import WordCount

main :: IO ()
main = defaultMain [WordCount.benchmarks]
20 changes: 20 additions & 0 deletions rhine/bench/Test.hs
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
import WordCount

-- tasty
import Test.Tasty

-- tasty-hunit
import Test.Tasty.HUnit (testCase, (@?=))

-- | The number of words in Project Gutenberg's edition of Shakespeare's complete works.
wordCount :: Int
wordCount = 966503

main :: IO ()
main =
defaultMain $
testGroup
"WordCount"
[ testCase "rhine" $ rhineWordCount >>= (@?= wordCount)
, testCase "dunai" $ dunaiWordCount >>= (@?= wordCount)
]
148 changes: 148 additions & 0 deletions rhine/bench/WordCount.hs
Original file line number Diff line number Diff line change
@@ -0,0 +1,148 @@
{-# LANGUAGE Arrows #-}
{-# LANGUAGE ScopedTypeVariables #-}

-- | Count the number of words in the complete works of Shakespeare.
module WordCount where

-- base
import Control.Exception
import Data.IORef (modifyIORef', newIORef, readIORef)
import Data.Monoid (Sum (..))
import GHC.IO.Handle hiding (hGetContents)
import System.IO (IOMode (ReadMode), openFile, stdin, withFile)
import System.IO.Error (isEOFError)
import Prelude hiding (getContents, getLine, words)

-- text
import Data.Text (words)
import Data.Text.IO (getLine)
import Data.Text.Lazy qualified as Lazy
import Data.Text.Lazy.IO (hGetContents)

-- criterion
import Criterion.Main

-- dunai
import Data.MonadicStreamFunction qualified as Dunai

-- rhine

import Control.Monad.Trans.MSF.Except qualified as Dunai
import FRP.Rhine
import FRP.Rhine.Clock.Except (
DelayIOError,
ExceptClock (..),
delayIOError,
)
import Paths_rhine

-- * Top level benchmarks

benchmarks :: Benchmark
benchmarks =
bgroup
"WordCount"
[ bench "rhine" $ nfIO rhineWordCount
, bench "dunai" $ nfIO dunaiWordCount
, bgroup
"Text"
[ bench "IORef" $ nfIO textWordCount
, bench "no IORef" $ nfIO textWordCountNoIORef
, bench "Lazy" $ nfIO textLazy
]
]

-- * Benchmark helpers

-- | The path to Shakespeare's complete works
testFile :: IO FilePath
testFile = getDataFileName "bench/pg100.txt"

-- | Provide Shakespeare's complete works on stdin
withInput :: IO b -> IO b
withInput action = do
inputFileName <- testFile
withFile inputFileName ReadMode $ \stdinFile -> do
hDuplicateTo stdinFile stdin
action

-- * Frameworks specific implementations of word count

-- | Idiomatic Rhine implementation with a single clock
rhineWordCount :: IO Int
rhineWordCount = do
Left (Right count) <- withInput $ runExceptT $ flow $ wc @@ delayIOError (ExceptClock StdinClock) Left
return count
where
wc :: ClSF (ExceptT (Either IOError Int) IO) (DelayIOError (ExceptClock StdinClock IOError) (Either IOError Int)) () ()
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.
-}
dunaiWordCount :: IO Int
dunaiWordCount = do
Left (Right nWords) <- withInput $ runExceptT $ Dunai.reactimate wc
return nWords
where
wc = proc () -> do
lineOrEOF <- Dunai.constM $ liftIO $ Control.Exception.try getLine -< ()
nWords <- Dunai.mappendS -< either (const 0) (Sum . length . words) lineOrEOF
case lineOrEOF of
Right _ -> returnA -< ()
Left e ->
if isEOFError e
then Dunai.throwS -< Right $ getSum nWords
else Dunai.throwS -< Left e

-- ** Reference implementations in Haskell

{- | The fastest line-based word count implementation that I could think of.

This is what 'rhineWordCount' would reduce to roughly, if all possible optimizations kick in,
except for the way the IORef is handled.
-}
textWordCount :: IO Int
textWordCount = do
wcOut <- newIORef (0 :: Int)
catch (withInput $ go wcOut) $ \(e :: IOError) ->
if isEOFError e
then return ()
else throwIO e
readIORef wcOut
where
go wcOut = do
line <- getLine
modifyIORef' wcOut (+ length (words line))
go wcOut

{- | The fastest line-based word count implementation that I could think of, not using IORefs.

This is what 'rhineWordCount' would reduce to roughly, if all possible optimizations kick in.
It is a bit slower than the version with IORef.
-}
textWordCountNoIORef :: IO Int
textWordCountNoIORef = do
withInput $ go 0
where
step n = do
line <- getLine
return $ Right $ n + length (words line)
go n = do
n' <- catch (step n) $
\(e :: IOError) ->
if isEOFError e
then return $ Left n
else throwIO e
either return go n'

-- | Just for fun the probably most readable but not the fastest way to count the number of words.
textLazy :: IO Int
textLazy = do
inputFileName <- testFile
handle <- openFile inputFileName ReadMode
length . Lazy.words <$> hGetContents handle
Loading
Loading