on master help sign in
Move parseMany benchamrks from FileSystem.Handle to Data.Parser
b04187bAdithya Kumar
benchmark/Streamly/Benchmark/Data/Parser.hs+32−3
@@ -5,6 +5,7 @@
5-- License : BSD-3-Clause
5-- License : BSD-3-Clause
6-- Maintainer : streamly@composewell.com
6-- Maintainer : streamly@composewell.com
7
7
8+{-# LANGUAGE CPP #-}
8{-# LANGUAGE FlexibleContexts #-}
9{-# LANGUAGE FlexibleContexts #-}
9
10
10module Main
11module Main
@@ -18,6 +19,13 @@ import Data.Foldable (asum)
18import Data.Functor (($>))
19import Data.Functor (($>))
19import Data.Maybe (fromMaybe)
20import Data.Maybe (fromMaybe)
20import Data.Monoid (Sum(..))
21import Data.Monoid (Sum(..))
22+import GHC.Magic (inline)
23+#if __GLASGOW_HASKELL__ >= 802
24+import GHC.Magic (noinline)
25+#else
26+#define noinline
27+#endif
28+import System.IO (Handle)
21import System.Random (randomRIO)
29import System.Random (randomRIO)
22import Prelude
30import Prelude
23 hiding (any, all, take, sequence, sequence_, sequenceA, takeWhile)
31 hiding (any, all, take, sequence, sequence_, sequenceA, takeWhile)
@@ -25,14 +33,16 @@ import Prelude
25import qualified Data.Traversable as TR
33import qualified Data.Traversable as TR
26import qualified Data.Foldable as F
34import qualified Data.Foldable as F
27import qualified Control.Applicative as AP
35import qualified Control.Applicative as AP
36+import qualified Streamly.FileSystem.Handle as Handle
28import qualified Streamly.Prelude as S
37import qualified Streamly.Prelude as S
29import qualified Streamly.Internal.Data.Fold as FL
38import qualified Streamly.Internal.Data.Fold as FL
30import qualified Streamly.Internal.Data.Parser as PR
39import qualified Streamly.Internal.Data.Parser as PR
31import qualified Streamly.Internal.Data.Stream.IsStream as IP
40import qualified Streamly.Internal.Data.Stream.IsStream as IP
32
41
33−import Gauge
42+import Gauge hiding (env)
34import Streamly.Prelude (SerialT)
43import Streamly.Prelude (SerialT)
35import Streamly.Benchmark.Common
44import Streamly.Benchmark.Common
45+import Streamly.Benchmark.Common.Handle
36
46
37-------------------------------------------------------------------------------
47-------------------------------------------------------------------------------
38-- Utilities
48-- Utilities
@@ -218,6 +228,13 @@ longestAllAny value =
218 (PR.drainWhile (<= value))
228 (PR.drainWhile (<= value))
219 )
229 )
220
230
231+parseManyChunksOfSum :: Int -> Handle -> IO Int
232+parseManyChunksOfSum n inh =
233+ S.length
234+ $ IP.parseMany
235+ (PR.fromFold $ FL.takeLE n FL.sum)
236+ (S.unfold Handle.read inh)
237+
221-------------------------------------------------------------------------------
238-------------------------------------------------------------------------------
222-- Parsers in which -fspec-constr-recursive=16 is problematic
239-- Parsers in which -fspec-constr-recursive=16 is problematic
223-------------------------------------------------------------------------------
240-------------------------------------------------------------------------------
@@ -313,6 +330,14 @@ o_1_space_serial value =
313 , benchIOSink value "parseIterate" parseIterate
330 , benchIOSink value "parseIterate" parseIterate
314 ]
331 ]
315
332
333+o_1_space_filesystem :: BenchEnv -> [Benchmark]
334+o_1_space_filesystem env =
335+ [ mkBench ("S.parseMany (FL.take " ++ show (bigSize env) ++ " FL.sum)") env
336+ $ \inh _ -> noinline parseManyChunksOfSum (bigSize env) inh
337+ , mkBench "S.parseMany (FL.take 1 FL.sum)" env
338+ $ \inh _ -> inline parseManyChunksOfSum 1 inh
339+ ]
340+
316o_n_heap_serial :: Int -> [Benchmark]
341o_n_heap_serial :: Int -> [Benchmark]
317o_n_heap_serial value =
342o_n_heap_serial value =
318 [
343 [
@@ -344,11 +369,15 @@ o_n_heap_serial value =
344main :: IO ()
369main :: IO ()
345main = do
370main = do
346 (value, cfg, benches) <- parseCLIOpts defaultStreamSize
371 (value, cfg, benches) <- parseCLIOpts defaultStreamSize
372+ env <- mkHandleBenchEnv
347− value `seq` runMode (mode cfg) cfg benches (allBenchmarks value)
373+ value `seq` runMode (mode cfg) cfg benches (allBenchmarks env value)
348
374
349 where
375 where
350
376
351− allBenchmarks value =
377+ allBenchmarks env value =
352 [ bgroup (o_1_space_prefix moduleName) (o_1_space_serial value)
378 [ bgroup (o_1_space_prefix moduleName) (o_1_space_serial value)
379+ , bgroup
380+ (o_1_space_prefix moduleName ++ "/filesystem")
381+ (o_1_space_filesystem env)
353 , bgroup (o_n_heap_prefix moduleName) (o_n_heap_serial value)
382 , bgroup (o_n_heap_prefix moduleName) (o_n_heap_serial value)
354 ]
383 ]
benchmark/Streamly/Benchmark/FileSystem/Handle/Read.hs−14
@@ -36,7 +36,6 @@ import qualified Streamly.FileSystem.Handle as FH
36import qualified Streamly.Internal.Data.Array.Foreign as A
36import qualified Streamly.Internal.Data.Array.Foreign as A
37import qualified Streamly.Internal.Data.Array.Foreign.Types as AT
37import qualified Streamly.Internal.Data.Array.Foreign.Types as AT
38import qualified Streamly.Internal.Data.Fold as FL
38import qualified Streamly.Internal.Data.Fold as FL
39−import qualified Streamly.Internal.Data.Parser as PR
40import qualified Streamly.Internal.Data.Stream.IsStream as IP
39import qualified Streamly.Internal.Data.Stream.IsStream as IP
41import qualified Streamly.Internal.FileSystem.Handle as IFH
40import qualified Streamly.Internal.FileSystem.Handle as IFH
42import qualified Streamly.Internal.Data.Array.Stream.Foreign as AS
41import qualified Streamly.Internal.Data.Array.Stream.Foreign as AS
@@ -318,11 +317,6 @@ foldManyChunksOfSum :: Int -> Handle -> IO Int
318foldManyChunksOfSum n inh =
317foldManyChunksOfSum n inh =
319 S.length $ IP.foldMany (FL.takeLE n FL.sum) (S.unfold FH.read inh)
318 S.length $ IP.foldMany (FL.takeLE n FL.sum) (S.unfold FH.read inh)
320
319
321−parseManyChunksOfSum :: Int -> Handle -> IO Int
322−parseManyChunksOfSum n inh =
323− S.length
324− $ IP.parseMany (PR.fromFold $ FL.takeLE n FL.sum) (S.unfold FH.read inh)
325−
326-- XXX investigate why we need an INLINE in this case (GHC)
320-- XXX investigate why we need an INLINE in this case (GHC)
327-- Even though allocations remain the same in both cases inlining improves time
321-- Even though allocations remain the same in both cases inlining improves time
328-- by 4x.
322-- by 4x.
@@ -373,14 +367,6 @@ o_1_space_reduce_read_grouped env =
373 "S.foldMany (FL.take 1 FL.sum)"
367 "S.foldMany (FL.take 1 FL.sum)"
374 env
368 env
375 $ \inh _ -> inline foldManyChunksOfSum 1 inh
369 $ \inh _ -> inline foldManyChunksOfSum 1 inh
376− , mkBench
377− ("S.parseMany (FL.take " ++ show (bigSize env) ++ " FL.sum)")
378− env
379− $ \inh _ -> noinline parseManyChunksOfSum (bigSize env) inh
380− , mkBench
381− "S.parseMany (FL.take 1 FL.sum)"
382− env
383− $ \inh _ -> inline parseManyChunksOfSum 1 inh
384
370
385 -- folding chunks to arrays
371 -- folding chunks to arrays
386 , mkBenchSmall "S.chunksOf 1" env $ \inh _ ->
372 , mkBenchSmall "S.chunksOf 1" env $ \inh _ ->