on 96dca8e help sign in
streamly/benchmark/LinearOps.hs
166 lines 5.3 kB history raw
1-- |
2-- Module : BenchmarkOps
3-- Copyright : (c) 2018 Harendra Kumar
4--
5-- License : MIT
6-- Maintainer : harendra.kumar@gmail.com
8{-# LANGUAGE FlexibleContexts #-}
10module LinearOps where
12import Prelude
13 (Monad, Int, (+), ($), (.), return, fmap, even, (>), (<=),
14 subtract, undefined, Maybe(..), Monoid, foldMap)
16import qualified Streamly as S
17import qualified Streamly.Prelude as S
19value, appendValue, maxValue :: Int
20value = 1000000
21appendValue = 100000
22maxValue = value + 1000
24-------------------------------------------------------------------------------
25-- Benchmark ops
26-------------------------------------------------------------------------------
28{-# INLINE toNull #-}
29{-# INLINE toList #-}
30{-# INLINE foldl #-}
31{-# INLINE last #-}
32{-# INLINE scan #-}
33{-# INLINE map #-}
34{-# INLINE filterEven #-}
35{-# INLINE mapM #-}
36{-# INLINE filterAllOut #-}
37{-# INLINE filterAllIn #-}
38{-# INLINE takeOne #-}
39{-# INLINE takeAll #-}
40{-# INLINE takeWhileTrue #-}
41{-# INLINE dropAll #-}
42{-# INLINE dropWhileTrue #-}
43{-# INLINE zip #-}
44{-# INLINE concat #-}
45{-# INLINE composeMapM #-}
46{-# INLINE composeAllInFilters #-}
47{-# INLINE composeAllOutFilters #-}
48{-# INLINE composeMapAllInFilter #-}
49toNull, scan, map, filterEven, filterAllOut,
50 filterAllIn, takeOne, takeAll, takeWhileTrue, dropAll, dropWhileTrue, zip,
51 concat, composeAllInFilters, composeAllOutFilters,
52 composeMapAllInFilter
53 :: Monad m
54 => Stream m Int -> m ()
56mapM, composeMapM :: S.MonadAsync m => Stream m Int -> m ()
57toList :: Monad m => Stream m Int -> m [Int]
58foldl :: Monad m => Stream m Int -> m Int
59last :: Monad m => Stream m Int -> m (Maybe Int)
61-------------------------------------------------------------------------------
62-- Stream generation and elimination
63-------------------------------------------------------------------------------
65type Stream m a = S.SerialT m a
67{-# INLINE source #-}
68source :: Int -> Stream m Int
69source n = S.unfoldr step n
70 where
71 step cnt =
72 if cnt > n + value
73 then Nothing
74 else (Just (cnt, cnt + 1))
76sourceFromFoldable :: Int -> Stream m Int
77sourceFromFoldable n = S.fromFoldable [n..n+value]
79{-# INLINE sourceFromFoldableM #-}
80sourceFromFoldableM :: S.MonadAsync m => Int -> Stream m Int
81sourceFromFoldableM n = S.fromFoldableM (Prelude.fmap return [n..n+value])
83sourceFoldMapWith :: Monad m => Int -> Stream m Int
84sourceFoldMapWith n = S.foldMapWith S.serial return [n..n+value]
86{-# INLINE sourceUnfoldrM #-}
87sourceUnfoldrM :: S.MonadAsync m => Int -> Stream m Int
88sourceUnfoldrM n = S.unfoldrM step n
89 where
90 step cnt =
91 if cnt > n + value
92 then return Nothing
93 else return (Just (cnt, cnt + 1))
95{-# INLINE runStream #-}
96runStream :: Monad m => Stream m a -> m ()
97runStream = S.runStream
99-------------------------------------------------------------------------------
100-- Elimination
101-------------------------------------------------------------------------------
103toNull = runStream
104toList = S.toList
105foldl = S.foldl' (+) 0
106last = S.last
108-------------------------------------------------------------------------------
109-- Transformation
110-------------------------------------------------------------------------------
112{-# INLINE transform #-}
113transform :: Monad m => Stream m a -> m ()
114transform = runStream
116scan = transform . S.scanl' (+) 0
117map = transform . fmap (+1)
118mapM = transform . S.mapM return
119filterEven = transform . S.filter even
120filterAllOut = transform . S.filter (> maxValue)
121filterAllIn = transform . S.filter (<= maxValue)
122takeOne = transform . S.take 1
123takeAll = transform . S.take maxValue
124takeWhileTrue = transform . S.takeWhile (<= maxValue)
125dropAll = transform . S.drop maxValue
126dropWhileTrue = transform . S.dropWhile (<= maxValue)
128-------------------------------------------------------------------------------
129-- Zipping and concat
130-------------------------------------------------------------------------------
132zip src = transform $ (S.zipWith (,) src src)
133concat _n = return ()
135-------------------------------------------------------------------------------
136-- Append
137-------------------------------------------------------------------------------
139{-# INLINE append #-}
140append
141 :: (Monoid (t m Int), Monad m, Monad (t m))
142 => (t m Int -> S.SerialT m Int) -> Int -> m ()
143append t n = runStream $ t $ foldMap return [n..n+appendValue]
145-------------------------------------------------------------------------------
146-- Composition
147-------------------------------------------------------------------------------
149{-# INLINE compose #-}
150compose :: Monad m => (Stream m Int -> Stream m Int) -> Stream m Int -> m ()
151compose f = transform . f . f . f . f
153composeMapM = compose (S.mapM return)
154composeAllInFilters = compose (S.filter (<= maxValue))
155composeAllOutFilters = compose (S.filter (> maxValue))
156composeMapAllInFilter = compose (S.filter (<= maxValue) . fmap (subtract 1))
158composeScaling :: Monad m => Int -> Stream m Int -> m ()
159composeScaling m =
160 case m of
161 1 -> transform . f
162 2 -> transform . f . f
163 3 -> transform . f . f . f
164 4 -> transform . f . f . f . f
165 _ -> undefined
166 where f = S.filter (<= maxValue)