3-- Copyright : (c) 2020 Composewell Technologies
5-- License : BSD-3-Clause
6-- Maintainer : streamly@composewell.com
9{-# LANGUAGE FlexibleContexts #-}
11import Control.DeepSeq (NFData(..))
12import System.Random (randomRIO)
14import qualified Streamly.Benchmark.Data.ArrayOps as Ops
15import qualified Streamly.Data.Array.Foreign as A
16import qualified Streamly.Internal.Data.Array.Foreign as IA
17import qualified Streamly.Prelude as S
20import Streamly.Benchmark.Common hiding (benchPureSrc)
21import qualified Streamly.Benchmark.Prelude as P
24import qualified GHC.Exts as GHC
25import Foreign.Storable (Storable(..))
28#if !defined(DATA_ARRAY_PRIM) && !defined(DATA_ARRAY_PRIM_PINNED)
29import Control.DeepSeq (deepseq)
32-------------------------------------------------------------------------------
34-------------------------------------------------------------------------------
37-- Drain a source that generates a pure array
38{-# INLINE benchPureSrc #-}
40 (NFData a, Storable a) => String -> (Int -> Ops.Stream a) -> Benchmark
41benchPureSrc name src = benchPure name src id
45benchIO :: NFData b => String -> (Int -> IO a) -> (a -> b) -> Benchmark
46benchIO name src f = bench name $ nfIO $
47 randomRIO (1,1) >>= src >>= return . f
49-- Drain a source that generates an array in the IO monad
50{-# INLINE benchIOSrc #-}
52#if !defined(DATA_ARRAY_PRIM) && !defined(DATA_ARRAY_PRIM_PINNED)
58 String -> (Int -> IO (Ops.Stream a)) -> Benchmark
59benchIOSrc name src = benchIO name src id
61{-# INLINE benchPureSink #-}
62benchPureSink :: NFData b => Int -> String -> (Ops.Stream Int -> b) -> Benchmark
63benchPureSink value name f = benchIO name (Ops.sourceIntFromTo value) f
65{-# INLINE benchIO' #-}
66benchIO' :: NFData b => String -> (Int -> IO a) -> (a -> IO b) -> Benchmark
67benchIO' name src f = bench name $ nfIO $
68 randomRIO (1,1) >>= src >>= f
70{-# INLINE benchIOSink #-}
71benchIOSink :: NFData b => Int -> String -> (Ops.Stream Int -> IO b) -> Benchmark
72benchIOSink value name f = benchIO' name (Ops.sourceIntFromTo value) f
74o_1_space_generation :: Int -> [Benchmark]
75o_1_space_generation value =
78 [ benchIOSrc "writeN . intFromTo" (Ops.sourceIntFromTo value)
79#ifndef DATA_SMALLARRAY
80 , benchIOSrc "write . intFromTo" (Ops.sourceIntFromToFromStream value)
83 "fromList . intFromTo"
84 (Ops.sourceIntFromToFromList value)
85 , benchIOSrc "writeN . unfoldr" (Ops.sourceUnfoldr value)
86 , benchIOSrc "writeN . fromList" (Ops.sourceFromList value)
88 , benchPureSrc "writeN . IsList.fromList" (Ops.sourceIsList value)
90 "writeN . IsString.fromString"
91 (Ops.sourceIsString value)
93#if !defined(DATA_ARRAY_PRIM) && !defined(DATA_ARRAY_PRIM_PINNED)
98 "[1" ++ concat (replicate value ",1") ++ "]"
100 , let testStr = mkListString value
102 in testStr `deepseq` (bench "read" $ nf Ops.readInstance testStr)
104 , benchPureSink value "show" Ops.showInstance
108o_1_space_elimination :: Int -> [Benchmark]
109o_1_space_elimination value =
110 [ bgroup "elimination"
111 [ benchPureSink value "id" id
112 , benchPureSink value "==" Ops.eqInstance
113 , benchPureSink value "/=" Ops.eqInstanceNotEq
114 , benchPureSink value "<" Ops.ordInstance
115 , benchPureSink value "min" Ops.ordInstanceMin
117 -- length is used to check for foldr/build fusion
118 , benchPureSink value "length . IsList.toList" (length . GHC.toList)
120 , benchIOSink value "foldl'" Ops.pureFoldl'
121 , benchIOSink value "read" Ops.unfoldReadDrain
122 , benchIOSink value "toStreamRev" Ops.toStreamRevDrain
123 , benchFold "writeLastN.1" (S.fold (IA.writeLastN 1)) (P.sourceUnfoldrM value)
124 , benchFold "writeLastN.10" (S.fold (IA.writeLastN 10)) (P.sourceUnfoldrM value)
125#if !defined(DATA_ARRAY_PRIM) && !defined(DATA_ARRAY_PRIM_PINNED)
127 , benchPureSink value "foldable/foldl'" Ops.foldableFoldl'
128 , benchPureSink value "foldable/sum" Ops.foldableSum
134o_n_heap_serial :: Int -> [Benchmark]
135o_n_heap_serial value =
136 [ bgroup "elimination"
138 -- Converting the stream to an array
139 benchFold "writeLastN.Max" (S.fold (IA.writeLastN (value + 1)))
140 (P.sourceUnfoldrM value)
141 , benchFold "writeN" (S.fold (A.writeN value))
142 (P.sourceUnfoldrM value)
146o_1_space_transformation :: Int -> [Benchmark]
147o_1_space_transformation value =
148 [ bgroup "transformation"
149 [ benchIOSink value "scanl'" (Ops.scanl' value 1)
150 , benchIOSink value "scanl1'" (Ops.scanl1' value 1)
151 , benchIOSink value "map" (Ops.map value 1)
155o_1_space_transformationX4 :: Int -> [Benchmark]
156o_1_space_transformationX4 value =
157 [ bgroup "transformationX4"
158 [ benchIOSink value "scanl'" (Ops.scanl' value 4)
159 , benchIOSink value "scanl1'" (Ops.scanl1' value 4)
160 , benchIOSink value "map" (Ops.map value 4)
165#ifdef DATA_SMALLARRAY
166moduleName = "Data.SmallArray"
167#elif defined(MEMORY_ARRAY)
168moduleName = "Data.Array.Foreign"
169#elif defined(DATA_ARRAY_PRIM)
170moduleName = "Data.Array.Prim"
171#elif defined(DATA_ARRAY_PRIM_PINNED)
172moduleName = "Data.Array.Prim.Pinned"
174moduleName = "Data.Array"
177#ifdef DATA_SMALLARRAY
182defStreamSize = defaultStreamSize
186main = runWithCLIOpts defStreamSize allBenchmarks
191 [ bgroup (o_1_space_prefix moduleName) $ concat
192 [ o_1_space_generation size
193 , o_1_space_elimination size
194 , o_1_space_transformation size
195 , o_1_space_transformationX4 size
197 , bgroup (o_n_space_prefix moduleName) $
keysesc to close
- j k
- move down, up
- enter
- open
- esc
- back, or up a level
- 1 2 3 4
- readme, files, commits, refs
- /
- filter
- y
- copy the clone command
- c
- compare the chosen commit with another (or shift-click both)
- ?
- show or hide this list