on cd40d90 help sign in
234 lines 7.0 kB history raw
1-- |
2-- Module : Streamly.Benchmark.Data.ArrayOps
3-- Copyright : (c) 2020 Composewell Technologies
4--
5-- License : BSD-3-Clause
6-- Maintainer : streamly@composewell.com
8{-# LANGUAGE CPP #-}
9{-# LANGUAGE FlexibleContexts #-}
10{-# LANGUAGE ScopedTypeVariables #-}
12-- CPP:
13-- MEMORY_ARRAY
14-- DATA_ARRAY
15-- DATA_PRIM_ARRAY
16-- DATA_SMALLARRAY
18module Streamly.Benchmark.Data.ArrayOps where
20import Control.Monad.IO.Class (MonadIO)
21import Prelude (Bool, Int, Maybe(..), ($), (+), (.), (==), (>), undefined)
23import qualified Prelude as P
24import qualified Streamly.Prelude as S
26#ifndef DATA_PRIM_ARRAY
27#ifdef DEVBUILD
28import qualified Data.Foldable as F
29#endif
30#endif
32#ifdef DATA_SMALLARRAY
33import qualified Streamly.Internal.Data.SmallArray as A
34type Stream = A.SmallArray
35#elif defined(MEMORY_ARRAY)
36import qualified GHC.Exts as GHC
37import qualified Streamly.Data.Array.Storable.Foreign as A
38import qualified Streamly.Internal.Data.Array.Storable.Foreign as A
39type Stream = A.Array
40#elif defined(DATA_PRIM_ARRAY)
41import qualified Streamly.Internal.Data.Prim.Array as A
42type Stream = A.PrimArray
43#else
44import qualified Streamly.Internal.Data.Array as A
45type Stream = A.Array
46#endif
48-------------------------------------------------------------------------------
49-- Benchmark ops
50-------------------------------------------------------------------------------
52-------------------------------------------------------------------------------
53-- Stream generation and elimination
54-------------------------------------------------------------------------------
56-------------------------------------------------------------------------------
57-- CPP Common to:
58-- MEMORY_ARRAY
59-- DATA_ARRAY
60-- DATA_PRIM_ARRAY
61-- DATA_SMALLARRAY
62-------------------------------------------------------------------------------
64{-# INLINE sourceUnfoldr #-}
65sourceUnfoldr :: MonadIO m => Int -> Int -> m (Stream Int)
66sourceUnfoldr value n = S.fold (A.writeN value) $ S.unfoldr step n
67 where
68 step cnt =
69 if cnt > n + value
70 then Nothing
71 else (Just (cnt, cnt + 1))
73{-# INLINE sourceIntFromTo #-}
74sourceIntFromTo :: MonadIO m => Int -> Int -> m (Stream Int)
75sourceIntFromTo value n = S.fold (A.writeN value) $ S.enumerateFromTo n (n + value)
77{-# INLINE sourceFromList #-}
78sourceFromList :: MonadIO m => Int -> Int -> m (Stream Int)
79sourceFromList value n = S.fold (A.writeN value) $ S.fromList [n..n+value]
81-- Different defination of sourceIntFromToFromList for DATA_SMALLARRAY
82-- CPP:
83{-# INLINE sourceIntFromToFromList #-}
84sourceIntFromToFromList :: MonadIO m => Int -> Int -> m (Stream Int)
85#ifndef DATA_SMALLARRAY
86sourceIntFromToFromList value n = P.return $ A.fromList $ [n..n + value]
87#else
88sourceIntFromToFromList value n = P.return $ A.fromListN value $ [n..n + value]
89#endif
91-------------------------------------------------------------------------------
92-- CPP Common to:
93-- MEMORY_ARRAY
94-- DATA_ARRAY
95-- DATA_PRIM_ARRAY
96-------------------------------------------------------------------------------
98-- CPP:
99#ifndef DATA_SMALLARRAY
100{-# INLINE sourceIntFromToFromStream #-}
101sourceIntFromToFromStream :: MonadIO m => Int -> Int -> m (Stream Int)
102sourceIntFromToFromStream value n = S.fold A.write $ S.enumerateFromTo n (n + value)
103#endif
105-------------------------------------------------------------------------------
106-- CPP Common to:
107-- MEMORY_ARRAY
108-------------------------------------------------------------------------------
110-- CPP:
111#ifdef MEMORY_ARRAY
112{-# INLINE sourceIsList #-}
113sourceIsList :: Int -> Int -> Stream Int
114sourceIsList value n = GHC.fromList [n..n+value]
116{-# INLINE sourceIsString #-}
117sourceIsString :: Int -> Int -> Stream P.Char
118sourceIsString value n = GHC.fromString (P.replicate (n + value) 'a')
119#endif
121-------------------------------------------------------------------------------
122-- Transformation
123-------------------------------------------------------------------------------
125-------------------------------------------------------------------------------
126-- CPP Common to:
127-- MEMORY_ARRAY
128-- DATA_ARRAY
129-- DATA_PRIM_ARRAY
130-- DATA_SMALLARRAY
131-------------------------------------------------------------------------------
133{-# INLINE composeN #-}
134composeN :: P.Monad m
135 => Int -> (Stream Int -> m (Stream Int)) -> Stream Int -> m (Stream Int)
136composeN n f x =
137 case n of
138 1 -> f x
139 2 -> f x P.>>= f
140 3 -> f x P.>>= f P.>>= f
141 4 -> f x P.>>= f P.>>= f P.>>= f
142 _ -> undefined
144{-# INLINE scanl' #-}
145{-# INLINE scanl1' #-}
146{-# INLINE map #-}
148scanl' , scanl1', map
149 :: MonadIO m => Int -> Int -> Stream Int -> m (Stream Int)
152{-# INLINE onArray #-}
153onArray
154 :: MonadIO m => Int -> (S.SerialT m Int -> S.SerialT m Int)
155 -> Stream Int
156 -> m (Stream Int)
157onArray value f arr = S.fold (A.writeN value) $ f $ (S.unfold A.read arr)
159scanl' value n = composeN n $ onArray value $ S.scanl' (+) 0
160scanl1' value n = composeN n $ onArray value $ S.scanl1' (+)
161map value n = composeN n $ onArray value $ S.map (+1)
162-- map n = composeN n $ A.map (+1)
164{-# INLINE eqInstance #-}
165eqInstance :: Stream Int -> Bool
166eqInstance src = src == src
168{-# INLINE eqInstanceNotEq #-}
169eqInstanceNotEq :: Stream Int -> Bool
170eqInstanceNotEq src = src P./= src
172{-# INLINE ordInstance #-}
173ordInstance :: Stream Int -> Bool
174ordInstance src = src P.< src
176{-# INLINE ordInstanceMin #-}
177ordInstanceMin :: Stream Int -> Stream Int
178ordInstanceMin src = P.min src src
180{-# INLINE showInstance #-}
181showInstance :: Stream Int -> P.String
182showInstance src = P.show src
184{-# INLINE pureFoldl' #-}
185pureFoldl' :: MonadIO m => Stream Int -> m Int
186pureFoldl' = S.foldl' (+) 0 . S.unfold A.read
188-------------------------------------------------------------------------------
189-- CPP Common to:
190-- MEMORY_ARRAY
191-- DATA_ARRAY
192-- DATA_SMALLARRAY
193-------------------------------------------------------------------------------
195-- CPP:
196#ifndef DATA_PRIM_ARRAY
197{-# INLINE readInstance #-}
198readInstance :: P.String -> Stream Int
199readInstance str =
200 let r = P.reads str
201 in case r of
202 [(x,"")] -> x
203 _ -> P.error "readInstance: no parse"
205#ifdef DEVBUILD
206{-# INLINE foldableFoldl' #-}
207foldableFoldl' :: Stream Int -> Int
208foldableFoldl' = F.foldl' (+) 0
210{-# INLINE foldableSum #-}
211foldableSum :: Stream Int -> Int
212foldableSum = P.sum
213#endif
214#endif
216-------------------------------------------------------------------------------
217-- Elimination
218-------------------------------------------------------------------------------
220-------------------------------------------------------------------------------
221-- CPP Common to:
222-- MEMORY_ARRAY
223-- DATA_ARRAY
224-- DATA_PRIM_ARRAY
225-- DATA_SMALLARRAY
226-------------------------------------------------------------------------------
228{-# INLINE unfoldReadDrain #-}
229unfoldReadDrain :: MonadIO m => Stream Int -> m ()
230unfoldReadDrain = S.drain . S.unfold A.read
232{-# INLINE toStreamRevDrain #-}
233toStreamRevDrain :: MonadIO m => Stream Int -> m ()
234toStreamRevDrain = S.drain . A.toStreamRev