on master help sign in
Keep the INLINE statement & signature with function 4 bench:StreamK
c7cd7c8Adithya Kumar
benchmark/Streamly/Benchmark/Data/Stream/StreamK.hs+130−82
@@ -47,26 +47,6 @@ value3 = round (P.fromIntegral value**(1/3::P.Double)) -- triple nested loop
47value16 = round (P.fromIntegral value**(1/16::P.Double)) -- triple nested loop
47value16 = round (P.fromIntegral value**(1/16::P.Double)) -- triple nested loop
48maxValue = value
48maxValue = value
49
49
50−-------------------------------------------------------------------------------
51−-- Benchmark ops
52−-------------------------------------------------------------------------------
53−
54−{-# INLINE toNull #-}
55−{-# INLINE uncons #-}
56−{-# INLINE nullTail #-}
57−{-# INLINE headTail #-}
58−{-# INLINE zip #-}
59−toNull, uncons, nullTail, headTail, zip
60− :: Monad m
61− => Stream m Int -> m ()
62−
63−{-# INLINE toList #-}
64−toList :: Monad m => Stream m Int -> m [Int]
65−{-# INLINE foldl #-}
66−foldl :: Monad m => Stream m Int -> m Int
67−{-# INLINE last #-}
68−last :: Monad m => Stream m Int -> m (Maybe Int)
69−
70-------------------------------------------------------------------------------
50-------------------------------------------------------------------------------
71-- Stream generation and elimination
51-- Stream generation and elimination
72-------------------------------------------------------------------------------
52-------------------------------------------------------------------------------
@@ -145,7 +125,12 @@ runStream = S.drain
145mapM_ :: Monad m => Stream m a -> m ()
125mapM_ :: Monad m => Stream m a -> m ()
146mapM_ = S.mapM_ (\_ -> return ())
126mapM_ = S.mapM_ (\_ -> return ())
147
127
128+{-# INLINE toNull #-}
129+toNull :: Monad m => Stream m Int -> m ()
148toNull = runStream
130toNull = runStream
131+
132+{-# INLINE uncons #-}
133+uncons :: Monad m => Stream m Int -> m ()
149uncons s = do
134uncons s = do
150 r <- S.uncons s
135 r <- S.uncons s
151 case r of
136 case r of
@@ -162,17 +147,29 @@ init s = do
162tail :: (Monad m, S.IsStream t) => t m a -> m ()
147tail :: (Monad m, S.IsStream t) => t m a -> m ()
163tail s = S.tail s >>= P.mapM_ tail
148tail s = S.tail s >>= P.mapM_ tail
164
149
150+{-# INLINE nullTail #-}
151+nullTail :: Monad m => Stream m Int -> m ()
165nullTail s = do
152nullTail s = do
166 r <- S.null s
153 r <- S.null s
167 when (not r) $ S.tail s >>= P.mapM_ nullTail
154 when (not r) $ S.tail s >>= P.mapM_ nullTail
168
155
156+{-# INLINE headTail #-}
157+headTail :: Monad m => Stream m Int -> m ()
169headTail s = do
158headTail s = do
170 h <- S.head s
159 h <- S.head s
171 when (isJust h) $ S.tail s >>= P.mapM_ headTail
160 when (isJust h) $ S.tail s >>= P.mapM_ headTail
172
161
162+{-# INLINE toList #-}
163+toList :: Monad m => Stream m Int -> m [Int]
173toList = S.toList
164toList = S.toList
165+
166+{-# INLINE foldl #-}
167+foldl :: Monad m => Stream m Int -> m Int
174−foldl = S.foldl' (+) 0
168+foldl = S.foldl' (+) 0
169+
170+{-# INLINE last #-}
171+last :: Monad m => Stream m Int -> m (Maybe Int)
175−last = S.last
172+last = S.last
176
173
177-------------------------------------------------------------------------------
174-------------------------------------------------------------------------------
178-- Transformation
175-- Transformation
@@ -195,50 +192,76 @@ composeN n f =
195 _ -> undefined
192 _ -> undefined
196
193
197{-# INLINE scan #-}
194{-# INLINE scan #-}
195+scan :: Monad m => Int -> Stream m Int -> m ()
196+scan n = composeN n $ S.scanl' (+) 0
197+
198{-# INLINE map #-}
198{-# INLINE map #-}
199+map :: Monad m => Int -> Stream m Int -> m ()
200+map n = composeN n $ P.fmap (+ 1)
201+
199{-# INLINE fmapK #-}
202{-# INLINE fmapK #-}
203+fmapK :: Monad m => Int -> Stream m Int -> m ()
204+fmapK n = composeN n $ P.fmap (+ 1)
205+
206+{-# INLINE mapM #-}
207+mapM :: S.MonadAsync m => Int -> Stream m Int -> m ()
208+mapM n = composeN n $ S.mapM return
209+
210+{-# INLINE mapMSerial #-}
211+mapMSerial :: S.MonadAsync m => Int -> Stream m Int -> m ()
212+mapMSerial n = composeN n $ S.mapMSerial return
213+
200{-# INLINE filterEven #-}
214{-# INLINE filterEven #-}
215+filterEven :: Monad m => Int -> Stream m Int -> m ()
216+filterEven n = composeN n $ S.filter even
217+
201{-# INLINE filterAllOut #-}
218{-# INLINE filterAllOut #-}
219+filterAllOut :: Monad m => Int -> Stream m Int -> m ()
220+filterAllOut n = composeN n $ S.filter (> maxValue)
221+
202{-# INLINE filterAllIn #-}
222{-# INLINE filterAllIn #-}
223+filterAllIn :: Monad m => Int -> Stream m Int -> m ()
224+filterAllIn n = composeN n $ S.filter (<= maxValue)
225+
203{-# INLINE _takeOne #-}
226{-# INLINE _takeOne #-}
227+_takeOne :: Monad m => Int -> Stream m Int -> m ()
228+_takeOne n = composeN n $ S.take 1
229+
204{-# INLINE takeAll #-}
230{-# INLINE takeAll #-}
231+takeAll :: Monad m => Int -> Stream m Int -> m ()
232+takeAll n = composeN n $ S.take maxValue
233+
205{-# INLINE takeWhileTrue #-}
234{-# INLINE takeWhileTrue #-}
235+takeWhileTrue :: Monad m => Int -> Stream m Int -> m ()
236+takeWhileTrue n = composeN n $ S.takeWhile (<= maxValue)
237+
206{-# INLINE dropOne #-}
238{-# INLINE dropOne #-}
239+dropOne :: Monad m => Int -> Stream m Int -> m ()
240+dropOne n = composeN n $ S.drop 1
241+
207{-# INLINE dropAll #-}
242{-# INLINE dropAll #-}
243+dropAll :: Monad m => Int -> Stream m Int -> m ()
244+dropAll n = composeN n $ S.drop maxValue
245+
208{-# INLINE dropWhileTrue #-}
246{-# INLINE dropWhileTrue #-}
247+dropWhileTrue :: Monad m => Int -> Stream m Int -> m ()
248+dropWhileTrue n = composeN n $ S.dropWhile (<= maxValue)
249+
209{-# INLINE dropWhileFalse #-}
250{-# INLINE dropWhileFalse #-}
251+dropWhileFalse :: Monad m => Int -> Stream m Int -> m ()
252+dropWhileFalse n = composeN n $ S.dropWhile (<= 1)
253+
210{-# INLINE foldrS #-}
254{-# INLINE foldrS #-}
255+foldrS :: Monad m => Int -> Stream m Int -> m ()
256+foldrS n = composeN n $ S.foldrS S.cons S.nil
257+
211{-# INLINE foldlS #-}
258{-# INLINE foldlS #-}
212−scan, map, fmapK, filterEven, filterAllOut,
213− filterAllIn, _takeOne, takeAll, takeWhileTrue, dropAll, dropOne,
214− dropWhileTrue, dropWhileFalse, foldrS, foldlS
215− :: Monad m
216− => Int -> Stream m Int -> m ()
259+foldlS :: Monad m => Int -> Stream m Int -> m ()
260+foldlS n = composeN n $ S.foldlS (flip S.cons) S.nil
217
261
218−{-# INLINE mapM #-}
219−{-# INLINE mapMSerial #-}
220{-# INLINE intersperse #-}
262{-# INLINE intersperse #-}
221−mapM, mapMSerial, intersperse
222− :: S.MonadAsync m => Int -> Stream m Int -> m ()
263+intersperse :: S.MonadAsync m => Int -> Stream m Int -> m ()
223−
224−scan n = composeN n $ S.scanl' (+) 0
225−map n = composeN n $ P.fmap (+1)
226−fmapK n = composeN n $ P.fmap (+1)
227−mapM n = composeN n $ S.mapM return
228−mapMSerial n = composeN n $ S.mapMSerial return
229−filterEven n = composeN n $ S.filter even
230−filterAllOut n = composeN n $ S.filter (> maxValue)
231−filterAllIn n = composeN n $ S.filter (<= maxValue)
232−_takeOne n = composeN n $ S.take 1
233−takeAll n = composeN n $ S.take maxValue
234−takeWhileTrue n = composeN n $ S.takeWhile (<= maxValue)
235−dropOne n = composeN n $ S.drop 1
236−dropAll n = composeN n $ S.drop maxValue
237−dropWhileTrue n = composeN n $ S.dropWhile (<= maxValue)
238−dropWhileFalse n = composeN n $ S.dropWhile (<= 1)
239−foldrS n = composeN n $ S.foldrS S.cons S.nil
240−foldlS n = composeN n $ S.foldlS (flip S.cons) S.nil
241−intersperse n = composeN n $ S.intersperse maxValue
264+intersperse n = composeN n $ S.intersperse maxValue
242
265
243-------------------------------------------------------------------------------
266-------------------------------------------------------------------------------
244-- Iteration
267-- Iteration
@@ -257,63 +280,88 @@ iterateSource g i n = f i (sourceUnfoldrMN iterStreamLen n)
257 f (0 :: Int) m = g m
280 f (0 :: Int) m = g m
258 f x m = g (f (x P.- 1) m)
281 f x m = g (f (x P.- 1) m)
259
282
260−{-# INLINE iterateMapM #-}
283+-- this is quadratic
261{-# INLINE iterateScan #-}
284{-# INLINE iterateScan #-}
285+iterateScan :: S.MonadAsync m => Int -> Stream m Int
286+iterateScan = iterateSource (S.scanl' (+) 0) (maxIters `div` 10)
287+
288+-- this is quadratic
289+{-# INLINE iterateDropWhileFalse #-}
290+iterateDropWhileFalse :: S.MonadAsync m => Int -> Stream m Int
291+iterateDropWhileFalse =
292+ iterateSource (S.dropWhile (> maxValue)) (maxIters `div` 10)
293+
294+{-# INLINE iterateMapM #-}
295+iterateMapM :: S.MonadAsync m => Int -> Stream m Int
296+iterateMapM = iterateSource (S.mapM return) maxIters
297+
262{-# INLINE iterateFilterEven #-}
298{-# INLINE iterateFilterEven #-}
299+iterateFilterEven :: S.MonadAsync m => Int -> Stream m Int
300+iterateFilterEven = iterateSource (S.filter even) maxIters
301+
263{-# INLINE iterateTakeAll #-}
302{-# INLINE iterateTakeAll #-}
264−{-# INLINE iterateDropOne #-}
265−{-# INLINE iterateDropWhileFalse #-}
266−{-# INLINE iterateDropWhileTrue #-}
267−iterateMapM, iterateScan, iterateFilterEven, iterateTakeAll, iterateDropOne,
268− iterateDropWhileFalse, iterateDropWhileTrue
269− :: S.MonadAsync m
270− => Int -> Stream m Int
303+iterateTakeAll :: S.MonadAsync m => Int -> Stream m Int
304+iterateTakeAll = iterateSource (S.take maxValue) maxIters
271
305
272−-- this is quadratic
306+{-# INLINE iterateDropOne #-}
273−iterateScan = iterateSource (S.scanl' (+) 0) (maxIters `div` 10)
307+iterateDropOne :: S.MonadAsync m => Int -> Stream m Int
274−iterateDropWhileFalse = iterateSource (S.dropWhile (> maxValue))
308+iterateDropOne = iterateSource (S.drop 1) maxIters
275− (maxIters `div` 10)
276
309
277−iterateMapM = iterateSource (S.mapM return) maxIters
310+{-# INLINE iterateDropWhileTrue #-}
278−iterateFilterEven = iterateSource (S.filter even) maxIters
311+iterateDropWhileTrue :: S.MonadAsync m => Int -> Stream m Int
279−iterateTakeAll = iterateSource (S.take maxValue) maxIters
280−iterateDropOne = iterateSource (S.drop 1) maxIters
281−iterateDropWhileTrue = iterateSource (S.dropWhile (<= maxValue)) maxIters
312+iterateDropWhileTrue = iterateSource (S.dropWhile (<= maxValue)) maxIters
282
313
283-------------------------------------------------------------------------------
314-------------------------------------------------------------------------------
284-- Zipping
315-- Zipping
285-------------------------------------------------------------------------------
316-------------------------------------------------------------------------------
286
317
318+{-# INLINE zip #-}
319+zip :: Monad m => Stream m Int -> m ()
287−zip src = transform $ S.zipWith (,) src src
320+zip src = transform $ S.zipWith (,) src src
288
321
289-------------------------------------------------------------------------------
322-------------------------------------------------------------------------------
290-- Mixed Composition
323-- Mixed Composition
291-------------------------------------------------------------------------------
324-------------------------------------------------------------------------------
292
325
293{-# INLINE scanMap #-}
326{-# INLINE scanMap #-}
327+scanMap :: Monad m => Int -> Stream m Int -> m ()
328+scanMap n = composeN n $ S.map (subtract 1) . S.scanl' (+) 0
329+
294{-# INLINE dropMap #-}
330{-# INLINE dropMap #-}
331+dropMap :: Monad m => Int -> Stream m Int -> m ()
332+dropMap n = composeN n $ S.map (subtract 1) . S.drop 1
333+
295{-# INLINE dropScan #-}
334{-# INLINE dropScan #-}
335+dropScan :: Monad m => Int -> Stream m Int -> m ()
336+dropScan n = composeN n $ S.scanl' (+) 0 . S.drop 1
337+
296{-# INLINE takeDrop #-}
338{-# INLINE takeDrop #-}
339+takeDrop :: Monad m => Int -> Stream m Int -> m ()
340+takeDrop n = composeN n $ S.drop 1 . S.take maxValue
341+
297{-# INLINE takeScan #-}
342{-# INLINE takeScan #-}
343+takeScan :: Monad m => Int -> Stream m Int -> m ()
344+takeScan n = composeN n $ S.scanl' (+) 0 . S.take maxValue
345+
298{-# INLINE takeMap #-}
346{-# INLINE takeMap #-}
347+takeMap :: Monad m => Int -> Stream m Int -> m ()
348+takeMap n = composeN n $ S.map (subtract 1) . S.take maxValue
349+
299{-# INLINE filterDrop #-}
350{-# INLINE filterDrop #-}
300−{-# INLINE filterTake #-}
301−{-# INLINE filterScan #-}
302−{-# INLINE filterMap #-}
303−scanMap, dropMap, dropScan, takeDrop, takeScan, takeMap, filterDrop,
304− filterTake, filterScan, filterMap
305− :: Monad m => Int -> Stream m Int -> m ()
351+filterDrop :: Monad m => Int -> Stream m Int -> m ()
306−
307−scanMap n = composeN n $ S.map (subtract 1) . S.scanl' (+) 0
308−dropMap n = composeN n $ S.map (subtract 1) . S.drop 1
309−dropScan n = composeN n $ S.scanl' (+) 0 . S.drop 1
310−takeDrop n = composeN n $ S.drop 1 . S.take maxValue
311−takeScan n = composeN n $ S.scanl' (+) 0 . S.take maxValue
312−takeMap n = composeN n $ S.map (subtract 1) . S.take maxValue
313filterDrop n = composeN n $ S.drop 1 . S.filter (<= maxValue)
352filterDrop n = composeN n $ S.drop 1 . S.filter (<= maxValue)
353+
354+{-# INLINE filterTake #-}
355+filterTake :: Monad m => Int -> Stream m Int -> m ()
314filterTake n = composeN n $ S.take maxValue . S.filter (<= maxValue)
356filterTake n = composeN n $ S.take maxValue . S.filter (<= maxValue)
357+
358+{-# INLINE filterScan #-}
359+filterScan :: Monad m => Int -> Stream m Int -> m ()
315filterScan n = composeN n $ S.scanl' (+) 0 . S.filter (<= maxBound)
360filterScan n = composeN n $ S.scanl' (+) 0 . S.filter (<= maxBound)
361+
362+{-# INLINE filterMap #-}
363+filterMap :: Monad m => Int -> Stream m Int -> m ()
316−filterMap n = composeN n $ S.map (subtract 1) . S.filter (<= maxValue)
364+filterMap n = composeN n $ S.map (subtract 1) . S.filter (<= maxValue)
317
365
318-------------------------------------------------------------------------------
366-------------------------------------------------------------------------------
319-- ConcatMap
367-- ConcatMap