on 8f00412 help sign in
235 lines 7.1 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 as S hiding (foldMapWith, runStream)
25import qualified Streamly.Prelude as S
27#ifndef DATA_PRIM_ARRAY
28#ifdef DEVBUILD
29import qualified Data.Foldable as F
30#endif
31#endif
33#ifdef DATA_SMALLARRAY
34import qualified Streamly.Internal.Data.SmallArray as A
35type Stream = A.SmallArray
36#elif defined(MEMORY_ARRAY)
37import qualified GHC.Exts as GHC
38import qualified Streamly.Memory.Array as A
39import qualified Streamly.Internal.Memory.Array as A
40type Stream = A.Array
41#elif defined(DATA_PRIM_ARRAY)
42import qualified Streamly.Internal.Data.Prim.Array as A
43type Stream = A.PrimArray
44#else
45import qualified Streamly.Internal.Data.Array as A
46type Stream = A.Array
47#endif
49-------------------------------------------------------------------------------
50-- Benchmark ops
51-------------------------------------------------------------------------------
53-------------------------------------------------------------------------------
54-- Stream generation and elimination
55-------------------------------------------------------------------------------
57-------------------------------------------------------------------------------
58-- CPP Common to:
59-- MEMORY_ARRAY
60-- DATA_ARRAY
61-- DATA_PRIM_ARRAY
62-- DATA_SMALLARRAY
63-------------------------------------------------------------------------------
65{-# INLINE sourceUnfoldr #-}
66sourceUnfoldr :: MonadIO m => Int -> Int -> m (Stream Int)
67sourceUnfoldr value n = S.fold (A.writeN value) $ S.unfoldr step n
68 where
69 step cnt =
70 if cnt > n + value
71 then Nothing
72 else (Just (cnt, cnt + 1))
74{-# INLINE sourceIntFromTo #-}
75sourceIntFromTo :: MonadIO m => Int -> Int -> m (Stream Int)
76sourceIntFromTo value n = S.fold (A.writeN value) $ S.enumerateFromTo n (n + value)
78{-# INLINE sourceFromList #-}
79sourceFromList :: MonadIO m => Int -> Int -> m (Stream Int)
80sourceFromList value n = S.fold (A.writeN value) $ S.fromList [n..n+value]
82-- Different defination of sourceIntFromToFromList for DATA_SMALLARRAY
83-- CPP:
84{-# INLINE sourceIntFromToFromList #-}
85sourceIntFromToFromList :: MonadIO m => Int -> Int -> m (Stream Int)
86#ifndef DATA_SMALLARRAY
87sourceIntFromToFromList value n = P.return $ A.fromList $ [n..n + value]
88#else
89sourceIntFromToFromList value n = P.return $ A.fromListN value $ [n..n + value]
90#endif
92-------------------------------------------------------------------------------
93-- CPP Common to:
94-- MEMORY_ARRAY
95-- DATA_ARRAY
96-- DATA_PRIM_ARRAY
97-------------------------------------------------------------------------------
99-- CPP:
100#ifndef DATA_SMALLARRAY
101{-# INLINE sourceIntFromToFromStream #-}
102sourceIntFromToFromStream :: MonadIO m => Int -> Int -> m (Stream Int)
103sourceIntFromToFromStream value n = S.fold A.write $ S.enumerateFromTo n (n + value)
104#endif
106-------------------------------------------------------------------------------
107-- CPP Common to:
108-- MEMORY_ARRAY
109-------------------------------------------------------------------------------
111-- CPP:
112#ifdef MEMORY_ARRAY
113{-# INLINE sourceIsList #-}
114sourceIsList :: Int -> Int -> Stream Int
115sourceIsList value n = GHC.fromList [n..n+value]
117{-# INLINE sourceIsString #-}
118sourceIsString :: Int -> Int -> Stream P.Char
119sourceIsString value n = GHC.fromString (P.replicate (n + value) 'a')
120#endif
122-------------------------------------------------------------------------------
123-- Transformation
124-------------------------------------------------------------------------------
126-------------------------------------------------------------------------------
127-- CPP Common to:
128-- MEMORY_ARRAY
129-- DATA_ARRAY
130-- DATA_PRIM_ARRAY
131-- DATA_SMALLARRAY
132-------------------------------------------------------------------------------
134{-# INLINE composeN #-}
135composeN :: P.Monad m
136 => Int -> (Stream Int -> m (Stream Int)) -> Stream Int -> m (Stream Int)
137composeN n f x =
138 case n of
139 1 -> f x
140 2 -> f x P.>>= f
141 3 -> f x P.>>= f P.>>= f
142 4 -> f x P.>>= f P.>>= f P.>>= f
143 _ -> undefined
145{-# INLINE scanl' #-}
146{-# INLINE scanl1' #-}
147{-# INLINE map #-}
149scanl' , scanl1', map
150 :: MonadIO m => Int -> Int -> Stream Int -> m (Stream Int)
153{-# INLINE onArray #-}
154onArray
155 :: MonadIO m => Int -> (S.SerialT m Int -> S.SerialT m Int)
156 -> Stream Int
157 -> m (Stream Int)
158onArray value f arr = S.fold (A.writeN value) $ f $ (S.unfold A.read arr)
160scanl' value n = composeN n $ onArray value $ S.scanl' (+) 0
161scanl1' value n = composeN n $ onArray value $ S.scanl1' (+)
162map value n = composeN n $ onArray value $ S.map (+1)
163-- map n = composeN n $ A.map (+1)
165{-# INLINE eqInstance #-}
166eqInstance :: Stream Int -> Bool
167eqInstance src = src == src
169{-# INLINE eqInstanceNotEq #-}
170eqInstanceNotEq :: Stream Int -> Bool
171eqInstanceNotEq src = src P./= src
173{-# INLINE ordInstance #-}
174ordInstance :: Stream Int -> Bool
175ordInstance src = src P.< src
177{-# INLINE ordInstanceMin #-}
178ordInstanceMin :: Stream Int -> Stream Int
179ordInstanceMin src = P.min src src
181{-# INLINE showInstance #-}
182showInstance :: Stream Int -> P.String
183showInstance src = P.show src
185{-# INLINE pureFoldl' #-}
186pureFoldl' :: MonadIO m => Stream Int -> m Int
187pureFoldl' = S.foldl' (+) 0 . S.unfold A.read
189-------------------------------------------------------------------------------
190-- CPP Common to:
191-- MEMORY_ARRAY
192-- DATA_ARRAY
193-- DATA_SMALLARRAY
194-------------------------------------------------------------------------------
196-- CPP:
197#ifndef DATA_PRIM_ARRAY
198{-# INLINE readInstance #-}
199readInstance :: P.String -> Stream Int
200readInstance str =
201 let r = P.reads str
202 in case r of
203 [(x,"")] -> x
204 _ -> P.error "readInstance: no parse"
206#ifdef DEVBUILD
207{-# INLINE foldableFoldl' #-}
208foldableFoldl' :: Stream Int -> Int
209foldableFoldl' = F.foldl' (+) 0
211{-# INLINE foldableSum #-}
212foldableSum :: Stream Int -> Int
213foldableSum = P.sum
214#endif
215#endif
217-------------------------------------------------------------------------------
218-- Elimination
219-------------------------------------------------------------------------------
221-------------------------------------------------------------------------------
222-- CPP Common to:
223-- MEMORY_ARRAY
224-- DATA_ARRAY
225-- DATA_PRIM_ARRAY
226-- DATA_SMALLARRAY
227-------------------------------------------------------------------------------
229{-# INLINE unfoldReadDrain #-}
230unfoldReadDrain :: MonadIO m => Stream Int -> m ()
231unfoldReadDrain = S.drain . S.unfold A.read
233{-# INLINE toStreamRevDrain #-}
234toStreamRevDrain :: MonadIO m => Stream Int -> m ()
235toStreamRevDrain = S.drain . A.toStreamRev