on ad1bb0d help sign in
197 lines 5.9 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_ARRAY_PRIM_PINNED
17module Streamly.Benchmark.Data.ArrayOps where
19import Control.Monad.IO.Class (MonadIO)
20import Prelude (Bool, Int, Maybe(..), ($), (+), (.), (==), (>), undefined)
22import qualified Prelude as P
23import qualified Streamly.Prelude as S
25#if !defined(DATA_ARRAY_PRIM_PINNED)
26#ifdef DEVBUILD
27{-
28import qualified Data.Foldable as F
29-}
30#endif
31#endif
33#if defined(DATA_ARRAY_PRIM_PINNED)
34import qualified Streamly.Internal.Data.Array.Prim.Pinned as A
35type Stream = A.Array
36#elif defined(DATA_ARRAY)
37import qualified Streamly.Internal.Data.Array as A
38type Stream = A.Array
39#endif
41-------------------------------------------------------------------------------
42-- Benchmark ops
43-------------------------------------------------------------------------------
45-------------------------------------------------------------------------------
46-- Stream generation and elimination
47-------------------------------------------------------------------------------
49-------------------------------------------------------------------------------
50-- CPP Common to:
51-- DATA_ARRAY
52-- DATA_ARRAY_PRIM_PINNED
53-------------------------------------------------------------------------------
55{-# INLINE sourceUnfoldr #-}
56sourceUnfoldr :: MonadIO m => Int -> Int -> m (Stream Int)
57sourceUnfoldr value n = S.fold (A.writeN value) $ S.unfoldr step n
58 where
59 step cnt =
60 if cnt > n + value
61 then Nothing
62 else (Just (cnt, cnt + 1))
64{-# INLINE sourceIntFromTo #-}
65sourceIntFromTo :: MonadIO m => Int -> Int -> m (Stream Int)
66sourceIntFromTo value n = S.fold (A.writeN value) $ S.enumerateFromTo n (n + value)
68{-# INLINE sourceFromList #-}
69sourceFromList :: MonadIO m => Int -> Int -> m (Stream Int)
70sourceFromList value n = S.fold (A.writeN value) $ S.fromList [n..n+value]
72-- CPP:
73{-# INLINE sourceIntFromToFromList #-}
74sourceIntFromToFromList :: MonadIO m => Int -> Int -> m (Stream Int)
75sourceIntFromToFromList value n = P.return $ A.fromList $ [n..n + value]
77-------------------------------------------------------------------------------
78-- CPP Common to:
79-- DATA_ARRAY
80-- DATA_ARRAY_PRIM_PINNED
81-------------------------------------------------------------------------------
83-- CPP:
84{-# INLINE sourceIntFromToFromStream #-}
85sourceIntFromToFromStream :: MonadIO m => Int -> Int -> m (Stream Int)
86sourceIntFromToFromStream value n = S.fold A.write $ S.enumerateFromTo n (n + value)
88-------------------------------------------------------------------------------
89-- Transformation
90-------------------------------------------------------------------------------
92-------------------------------------------------------------------------------
93-- CPP Common to:
94-- DATA_ARRAY
95-- DATA_ARRAY_PRIM_PINNED
96-------------------------------------------------------------------------------
98{-# INLINE composeN #-}
99composeN :: P.Monad m
100 => Int -> (Stream Int -> m (Stream Int)) -> Stream Int -> m (Stream Int)
101composeN n f x =
102 case n of
103 1 -> f x
104 2 -> f x P.>>= f
105 3 -> f x P.>>= f P.>>= f
106 4 -> f x P.>>= f P.>>= f P.>>= f
107 _ -> undefined
109{-# INLINE scanl' #-}
110{-# INLINE scanl1' #-}
111{-# INLINE map #-}
113scanl' , scanl1', map
114 :: MonadIO m => Int -> Int -> Stream Int -> m (Stream Int)
117{-# INLINE onArray #-}
118onArray
119 :: MonadIO m => Int -> (S.SerialT m Int -> S.SerialT m Int)
120 -> Stream Int
121 -> m (Stream Int)
122onArray value f arr = S.fold (A.writeN value) $ f $ (S.unfold A.read arr)
124scanl' value n = composeN n $ onArray value $ S.scanl' (+) 0
125scanl1' value n = composeN n $ onArray value $ S.scanl1' (+)
126map value n = composeN n $ onArray value $ S.map (+1)
127-- map n = composeN n $ A.map (+1)
129{-# INLINE eqInstance #-}
130eqInstance :: Stream Int -> Bool
131eqInstance src = src == src
133{-# INLINE eqInstanceNotEq #-}
134eqInstanceNotEq :: Stream Int -> Bool
135eqInstanceNotEq src = src P./= src
137{-# INLINE ordInstance #-}
138ordInstance :: Stream Int -> Bool
139ordInstance src = src P.< src
141{-# INLINE ordInstanceMin #-}
142ordInstanceMin :: Stream Int -> Stream Int
143ordInstanceMin src = P.min src src
145{-# INLINE showInstance #-}
146showInstance :: Stream Int -> P.String
147showInstance src = P.show src
149{-# INLINE pureFoldl' #-}
150pureFoldl' :: MonadIO m => Stream Int -> m Int
151pureFoldl' = S.foldl' (+) 0 . S.unfold A.read
153-------------------------------------------------------------------------------
154-- CPP Common to:
155-- DATA_ARRAY
156-------------------------------------------------------------------------------
158-- CPP:
159#if !defined(DATA_ARRAY_PRIM_PINNED)
160{-# INLINE readInstance #-}
161readInstance :: P.String -> Stream Int
162readInstance str =
163 let r = P.reads str
164 in case r of
165 [(x,"")] -> x
166 _ -> P.error "readInstance: no parse"
168#ifdef DEVBUILD
169{-
170{-# INLINE foldableFoldl' #-}
171foldableFoldl' :: Stream Int -> Int
172foldableFoldl' = F.foldl' (+) 0
174{-# INLINE foldableSum #-}
175foldableSum :: Stream Int -> Int
176foldableSum = P.sum
177-}
178#endif
179#endif
181-------------------------------------------------------------------------------
182-- Elimination
183-------------------------------------------------------------------------------
185-------------------------------------------------------------------------------
186-- CPP Common to:
187-- DATA_ARRAY
188-- DATA_ARRAY_PRIM_PINNED
189-------------------------------------------------------------------------------
191{-# INLINE unfoldReadDrain #-}
192unfoldReadDrain :: MonadIO m => Stream Int -> m ()
193unfoldReadDrain = S.drain . S.unfold A.read
195{-# INLINE toStreamRevDrain #-}
196toStreamRevDrain :: MonadIO m => Stream Int -> m ()
197toStreamRevDrain = S.drain . A.toStreamRev