@@ -1,100 +1,82 @@
3−-- Copyright : (c) 2020 Composewell Technologies
5−-- License : BSD-3-Clause
6−-- Maintainer : streamly@composewell.com
8{-# LANGUAGE CPP #-}
1{-# LANGUAGE CPP #-}
9−{-# LANGUAGE FlexibleContexts #-}
11−import Control.DeepSeq (NFData(..))
12−import System.Random (randomRIO)
14−import qualified Streamly.Benchmark.Data.ArrayOps as Ops
3+#include "Streamly/Benchmark/Data/Array/CommonImports.hs"
16−#ifdef DATA_ARRAY
5+import Control.DeepSeq (deepseq)
17−import qualified Streamly.Internal.Data.Array as A
19−import qualified Streamly.Data.Array.Foreign as A
23import qualified Streamly.Internal.Data.Array as IA
7import qualified Streamly.Internal.Data.Array as IA
25−import qualified Streamly.Prelude as S
8+import qualified Streamly.Internal.Data.Array as A
28−import Streamly.Benchmark.Common hiding (benchPureSrc)
11+#include "Streamly/Benchmark/Data/Array/Common.hs"
29−import qualified Streamly.Benchmark.Prelude as P
31−import Control.DeepSeq (deepseq)
33-------------------------------------------------------------------------------
13-------------------------------------------------------------------------------
34−--
14+-- Benchmark helpers
35-------------------------------------------------------------------------------
15-------------------------------------------------------------------------------
37−{-# INLINE benchIO #-}
38−benchIO :: NFData b => String -> (Int -> IO a) -> (a -> b) -> Benchmark
39−benchIO name src f = bench name $ nfIO $
40− randomRIO (1,1) >>= src >>= return . f
42-- Drain a source that generates an array in the IO monad
17-- Drain a source that generates an array in the IO monad
43{-# INLINE benchIOSrc #-}
18{-# INLINE benchIOSrc #-}
44−benchIOSrc :: NFData a => String -> (Int -> IO (Ops.Stream a)) -> Benchmark
19+benchIOSrc :: NFData a => String -> (Int -> IO (Stream a)) -> Benchmark
45benchIOSrc name src = benchIO name src id
20benchIOSrc name src = benchIO name src id
47−{-# INLINE benchPureSink #-}
22+-------------------------------------------------------------------------------
48−benchPureSink :: NFData b => Int -> String -> (Ops.Stream Int -> b) -> Benchmark
23+-- Bench Ops
49−benchPureSink value name f = benchIO name (Ops.sourceIntFromTo value) f
24+-------------------------------------------------------------------------------
26+{-# INLINE sourceIntFromToFromList #-}
27+sourceIntFromToFromList :: MonadIO m => Int -> Int -> m (Stream Int)
28+sourceIntFromToFromList value n = P.return $ A.fromListN value $ [n..n + value]
31+{-# INLINE readInstance #-}
32+readInstance :: P.String -> Stream Int
37+ _ -> P.error "readInstance: no parse"
51−{-# INLINE benchIO' #-}
39+#ifdef DEVBUILD
52−benchIO' :: NFData b => String -> (Int -> IO a) -> (a -> IO b) -> Benchmark
40+{-
53−benchIO' name src f = bench name $ nfIO $
41+{-# INLINE foldableFoldl' #-}
54− randomRIO (1,1) >>= src >>= f
42+foldableFoldl' :: Stream Int -> Int
43+foldableFoldl' = F.foldl' (+) 0
45+{-# INLINE foldableSum #-}
46+foldableSum :: Stream Int -> Int
51+{-# INLINE sourceIntFromToFromStream #-}
52+sourceIntFromToFromStream :: MonadIO m => Int -> Int -> m (Stream Int)
53+sourceIntFromToFromStream value n = S.fold A.write $ S.enumerateFromTo n (n + value)
56−{-# INLINE benchIOSink #-}
55+-------------------------------------------------------------------------------
57−benchIOSink :: NFData b => Int -> String -> (Ops.Stream Int -> IO b) -> Benchmark
56+-- Bench groups
58−benchIOSink value name f = benchIO' name (Ops.sourceIntFromTo value) f
57+-------------------------------------------------------------------------------
60o_1_space_generation :: Int -> [Benchmark]
59o_1_space_generation :: Int -> [Benchmark]
61o_1_space_generation value =
60o_1_space_generation value =
63 "generation"
62 "generation"
64− [ benchIOSrc "writeN . intFromTo" (Ops.sourceIntFromTo value)
65−#ifndef DATA_SMALLARRAY
66− , benchIOSrc "write . intFromTo" (Ops.sourceIntFromToFromStream value)
63+ [ benchIOSrc "write . intFromTo" (sourceIntFromToFromStream value)
69− "fromList . intFromTo"
70− (Ops.sourceIntFromToFromList value)
71− , benchIOSrc "writeN . unfoldr" (Ops.sourceUnfoldr value)
72− , benchIOSrc "writeN . fromList" (Ops.sourceFromList value)
73 , let testStr = mkListString value
64 , let testStr = mkListString value
74− in testStr `deepseq` (bench "read" $ nf Ops.readInstance testStr)
65+ in testStr `deepseq` (bench "read" $ nf readInstance testStr)
75− , benchPureSink value "show" Ops.showInstance
79o_1_space_elimination :: Int -> [Benchmark]
69o_1_space_elimination :: Int -> [Benchmark]
80o_1_space_elimination value =
70o_1_space_elimination value =
81 [ bgroup "elimination"
71 [ bgroup "elimination"
82− [ benchPureSink value "id" id
83− , benchPureSink value "==" Ops.eqInstance
84− , benchPureSink value "/=" Ops.eqInstanceNotEq
85− , benchPureSink value "<" Ops.ordInstance
86− , benchPureSink value "min" Ops.ordInstanceMin
87− , benchIOSink value "foldl'" Ops.pureFoldl'
88− , benchIOSink value "read" Ops.unfoldReadDrain
89− , benchIOSink value "toStreamRev" Ops.toStreamRevDrain
90− , benchFold "writeLastN.1"
72+ [ benchFold "writeLastN.1"
91 (S.fold (IA.writeLastN 1)) (P.sourceUnfoldrM value)
73 (S.fold (IA.writeLastN 1)) (P.sourceUnfoldrM value)
92 , benchFold "writeLastN.10"
74 , benchFold "writeLastN.10"
93 (S.fold (IA.writeLastN 10)) (P.sourceUnfoldrM value)
75 (S.fold (IA.writeLastN 10)) (P.sourceUnfoldrM value)
94#ifdef DEVBUILD
76#ifdef DEVBUILD
96− , benchPureSink value "foldable/foldl'" Ops.foldableFoldl'
78+ benchPureSink value "foldable/foldl'" foldableFoldl'
97− , benchPureSink value "foldable/sum" Ops.foldableSum
79+ , benchPureSink value "foldable/sum" foldableSum
@@ -107,29 +89,9 @@ o_n_heap_serial value =
107 -- Converting the stream to an array
89 -- Converting the stream to an array
108 benchFold "writeLastN.Max" (S.fold (IA.writeLastN (value + 1)))
90 benchFold "writeLastN.Max" (S.fold (IA.writeLastN (value + 1)))
109 (P.sourceUnfoldrM value)
91 (P.sourceUnfoldrM value)
110− , benchFold "writeN" (S.fold (A.writeN value))
111− (P.sourceUnfoldrM value)
115−o_1_space_transformation :: Int -> [Benchmark]
116−o_1_space_transformation value =
117− [ bgroup "transformation"
118− [ benchIOSink value "scanl'" (Ops.scanl' value 1)
119− , benchIOSink value "scanl1'" (Ops.scanl1' value 1)
120− , benchIOSink value "map" (Ops.map value 1)
124−o_1_space_transformationX4 :: Int -> [Benchmark]
125−o_1_space_transformationX4 value =
126− [ bgroup "transformationX4"
127− [ benchIOSink value "scanl'" (Ops.scanl' value 4)
128− , benchIOSink value "scanl1'" (Ops.scanl1' value 4)
129− , benchIOSink value "map" (Ops.map value 4)
133moduleName :: String
95moduleName :: String
134moduleName = "Data.Array"
96moduleName = "Data.Array"
@@ -142,12 +104,10 @@ main = runWithCLIOpts defStreamSize allBenchmarks
144 allBenchmarks size =
106 allBenchmarks size =
145− [ bgroup (o_1_space_prefix moduleName) $ concat
107+ [ bgroup (o_1_space_prefix moduleName) $ concat $
146− [ o_1_space_generation size
108+ [ o_1_space_generation size
147− , o_1_space_elimination size
109+ , o_1_space_elimination size
148− , o_1_space_transformation size
149− , o_1_space_transformationX4 size
151 , bgroup (o_n_space_prefix moduleName) $
111 , bgroup (o_n_space_prefix moduleName) $
152 o_n_heap_serial size
112 o_n_heap_serial size
153− ]
113+ ] ++ commonBenchmarks size