Skip to content

Commit

Permalink
Add benchmarks for the scientific parser
Browse files Browse the repository at this point in the history
  • Loading branch information
rnjtranjan committed Jul 6, 2023
1 parent d655671 commit 1cd21ad
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 1 deletion.
24 changes: 24 additions & 0 deletions benchmark/Streamly/Benchmark/Data/Parser.hs
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ import Prelude hiding

import qualified Control.Applicative as AP
import qualified Data.Foldable as F
import qualified Data.Scientific as Scientific
import qualified Data.Traversable as TR
import qualified Streamly.FileSystem.Handle as Handle
import qualified Streamly.Internal.Data.Array as Array
Expand All @@ -49,6 +50,7 @@ import qualified Streamly.Data.Stream as Stream
import qualified Streamly.Internal.Data.Producer as Producer
import qualified Streamly.Internal.Data.Producer.Source as Source
import qualified Streamly.Internal.Data.Stream.StreamD as Stream
import qualified Streamly.Internal.Data.Unicode.Parser.Extra as Parser

import Gauge hiding (env)
import Streamly.Benchmark.Common
Expand Down Expand Up @@ -628,6 +630,27 @@ choice value =
(PR.choice (replicate value (PR.satisfy (< 0))) AP.<|> PR.satisfy (> 0))
-}

{-# INLINE sourceUnfoldrMc #-}
sourceUnfoldrMc :: Monad m => Int -> Int -> Stream m Char
sourceUnfoldrMc value n = Stream.unfoldrM step n
where
step cnt =
if cnt > n + value
then return Nothing
else return (Just ('1', cnt + 1))

-- | Takes a fold method, and uses it with a default source.
{-# INLINE benchIOSinkc #-}
benchIOSinkc
:: NFData b
=> Int -> String -> (Stream IO Char -> IO b) -> Benchmark
benchIOSinkc value name f =
bench name $ nfIO $ randomRIO (1,1) >>= f . sourceUnfoldrMc value

{-# INLINE scientific #-}
scientific :: Monad m => Stream m Char -> m (Either ParseError Scientific.Scientific)
scientific = Stream.parse Parser.scientific

-------------------------------------------------------------------------------
-- Parsing with unfolds
-------------------------------------------------------------------------------
Expand Down Expand Up @@ -831,6 +854,7 @@ o_n_heap_serial value =
, benchIOSink value "manyAlt" manyAlt
, benchIOSink value "someAlt" someAlt
, benchIOSink value "listEqBy" (listEqBy value)
, benchIOSinkc value "scientific" scientific
]

-- accumulate results in a list in IO
Expand Down
3 changes: 2 additions & 1 deletion benchmark/streamly-benchmarks.cabal
Original file line number Diff line number Diff line change
Expand Up @@ -187,7 +187,8 @@ common bench-depends
, process >= 1.4 && < 1.7
, directory >= 1.2.2 && < 1.4
, filepath >= 1.4.1 && < 1.5
, ghc-prim >= 0.4 && < 0.11
, ghc-prim >= 0.4 && < 0.10
, scientific >= 0.0 && < 0.4

if flag(use-streamly-core)
build-depends: streamly-core
Expand Down

0 comments on commit 1cd21ad

Please sign in to comment.