on master help sign in
Keep the INLINE statement & signature with function 4 bench:StreamK
commitc7cd7c877ae256ba48431382a3ff9d6434947065 authorAdithya Kumar <adithya@composewell.com> dateMon 29 Mar 2021, 09:01 (6 years ago) parent16dfa56
1 file changed+130−82
benchmark/Streamly/Benchmark/Data/Stream/StreamK.hs+130−82
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
4747value16 = round (P.fromIntegral value**(1/16::P.Double)) -- triple nested loop
4848maxValue = value
4949
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−
7050-------------------------------------------------------------------------------
7151-- Stream generation and elimination
7252-------------------------------------------------------------------------------
@@ -145,7 +125,12 @@ runStream = S.drain
145125mapM_ :: Monad m => Stream m a -> m ()
146126mapM_ = S.mapM_ (\_ -> return ())
147127
128+{-# INLINE toNull #-}
129+toNull :: Monad m => Stream m Int -> m ()
148130toNull = runStream
131+
132+{-# INLINE uncons #-}
133+uncons :: Monad m => Stream m Int -> m ()
149134uncons s = do
150135 r <- S.uncons s
151136 case r of
@@ -162,17 +147,29 @@ init s = do
162147tail :: (Monad m, S.IsStream t) => t m a -> m ()
163148tail s = S.tail s >>= P.mapM_ tail
164149
150+{-# INLINE nullTail #-}
151+nullTail :: Monad m => Stream m Int -> m ()
165152nullTail s = do
166153 r <- S.null s
167154 when (not r) $ S.tail s >>= P.mapM_ nullTail
168155
156+{-# INLINE headTail #-}
157+headTail :: Monad m => Stream m Int -> m ()
169158headTail s = do
170159 h <- S.head s
171160 when (isJust h) $ S.tail s >>= P.mapM_ headTail
172161
162+{-# INLINE toList #-}
163+toList :: Monad m => Stream m Int -> m [Int]
173164toList = S.toList
174−foldl = S.foldl' (+) 0
175−last = S.last
165+
166+{-# INLINE foldl #-}
167+foldl :: Monad m => Stream m Int -> m Int
168+foldl = S.foldl' (+) 0
169+
170+{-# INLINE last #-}
171+last :: Monad m => Stream m Int -> m (Maybe Int)
172+last = S.last
176173
177174-------------------------------------------------------------------------------
178175-- Transformation
@@ -195,50 +192,76 @@ composeN n f =
195192 _ -> undefined
196193
197194{-# INLINE scan #-}
195+scan :: Monad m => Int -> Stream m Int -> m ()
196+scan n = composeN n $ S.scanl' (+) 0
197+
198198{-# INLINE map #-}
199+map :: Monad m => Int -> Stream m Int -> m ()
200+map n = composeN n $ P.fmap (+ 1)
201+
199202{-# 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+
200214{-# INLINE filterEven #-}
215+filterEven :: Monad m => Int -> Stream m Int -> m ()
216+filterEven n = composeN n $ S.filter even
217+
201218{-# INLINE filterAllOut #-}
219+filterAllOut :: Monad m => Int -> Stream m Int -> m ()
220+filterAllOut n = composeN n $ S.filter (> maxValue)
221+
202222{-# INLINE filterAllIn #-}
223+filterAllIn :: Monad m => Int -> Stream m Int -> m ()
224+filterAllIn n = composeN n $ S.filter (<= maxValue)
225+
203226{-# INLINE _takeOne #-}
227+_takeOne :: Monad m => Int -> Stream m Int -> m ()
228+_takeOne n = composeN n $ S.take 1
229+
204230{-# INLINE takeAll #-}
231+takeAll :: Monad m => Int -> Stream m Int -> m ()
232+takeAll n = composeN n $ S.take maxValue
233+
205234{-# INLINE takeWhileTrue #-}
235+takeWhileTrue :: Monad m => Int -> Stream m Int -> m ()
236+takeWhileTrue n = composeN n $ S.takeWhile (<= maxValue)
237+
206238{-# INLINE dropOne #-}
239+dropOne :: Monad m => Int -> Stream m Int -> m ()
240+dropOne n = composeN n $ S.drop 1
241+
207242{-# INLINE dropAll #-}
243+dropAll :: Monad m => Int -> Stream m Int -> m ()
244+dropAll n = composeN n $ S.drop maxValue
245+
208246{-# INLINE dropWhileTrue #-}
247+dropWhileTrue :: Monad m => Int -> Stream m Int -> m ()
248+dropWhileTrue n = composeN n $ S.dropWhile (<= maxValue)
249+
209250{-# INLINE dropWhileFalse #-}
251+dropWhileFalse :: Monad m => Int -> Stream m Int -> m ()
252+dropWhileFalse n = composeN n $ S.dropWhile (<= 1)
253+
210254{-# INLINE foldrS #-}
255+foldrS :: Monad m => Int -> Stream m Int -> m ()
256+foldrS n = composeN n $ S.foldrS S.cons S.nil
257+
211258{-# 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
217261
218−{-# INLINE mapM #-}
219−{-# INLINE mapMSerial #-}
220262{-# INLINE intersperse #-}
221−mapM, mapMSerial, intersperse
222− :: 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
263+intersperse :: S.MonadAsync m => Int -> Stream m Int -> m ()
264+intersperse n = composeN n $ S.intersperse maxValue
242265
243266-------------------------------------------------------------------------------
244267-- Iteration
@@ -257,63 +280,88 @@ iterateSource g i n = f i (sourceUnfoldrMN iterStreamLen n)
257280 f (0 :: Int) m = g m
258281 f x m = g (f (x P.- 1) m)
259282
260−{-# INLINE iterateMapM #-}
283+-- this is quadratic
261284{-# 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+
262298{-# INLINE iterateFilterEven #-}
299+iterateFilterEven :: S.MonadAsync m => Int -> Stream m Int
300+iterateFilterEven = iterateSource (S.filter even) maxIters
301+
263302{-# 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
271305
272−-- this is quadratic
273−iterateScan = iterateSource (S.scanl' (+) 0) (maxIters `div` 10)
274−iterateDropWhileFalse = iterateSource (S.dropWhile (> maxValue))
275− (maxIters `div` 10)
306+{-# INLINE iterateDropOne #-}
307+iterateDropOne :: S.MonadAsync m => Int -> Stream m Int
308+iterateDropOne = iterateSource (S.drop 1) maxIters
276309
277−iterateMapM = iterateSource (S.mapM return) maxIters
278−iterateFilterEven = iterateSource (S.filter even) maxIters
279−iterateTakeAll = iterateSource (S.take maxValue) maxIters
280−iterateDropOne = iterateSource (S.drop 1) maxIters
281−iterateDropWhileTrue = iterateSource (S.dropWhile (<= maxValue)) maxIters
310+{-# INLINE iterateDropWhileTrue #-}
311+iterateDropWhileTrue :: S.MonadAsync m => Int -> Stream m Int
312+iterateDropWhileTrue = iterateSource (S.dropWhile (<= maxValue)) maxIters
282313
283314-------------------------------------------------------------------------------
284315-- Zipping
285316-------------------------------------------------------------------------------
286317
287−zip src = transform $ S.zipWith (,) src src
318+{-# INLINE zip #-}
319+zip :: Monad m => Stream m Int -> m ()
320+zip src = transform $ S.zipWith (,) src src
288321
289322-------------------------------------------------------------------------------
290323-- Mixed Composition
291324-------------------------------------------------------------------------------
292325
293326{-# INLINE scanMap #-}
327+scanMap :: Monad m => Int -> Stream m Int -> m ()
328+scanMap n = composeN n $ S.map (subtract 1) . S.scanl' (+) 0
329+
294330{-# INLINE dropMap #-}
331+dropMap :: Monad m => Int -> Stream m Int -> m ()
332+dropMap n = composeN n $ S.map (subtract 1) . S.drop 1
333+
295334{-# INLINE dropScan #-}
335+dropScan :: Monad m => Int -> Stream m Int -> m ()
336+dropScan n = composeN n $ S.scanl' (+) 0 . S.drop 1
337+
296338{-# INLINE takeDrop #-}
339+takeDrop :: Monad m => Int -> Stream m Int -> m ()
340+takeDrop n = composeN n $ S.drop 1 . S.take maxValue
341+
297342{-# INLINE takeScan #-}
343+takeScan :: Monad m => Int -> Stream m Int -> m ()
344+takeScan n = composeN n $ S.scanl' (+) 0 . S.take maxValue
345+
298346{-# INLINE takeMap #-}
347+takeMap :: Monad m => Int -> Stream m Int -> m ()
348+takeMap n = composeN n $ S.map (subtract 1) . S.take maxValue
349+
299350{-# 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 ()
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
351+filterDrop :: Monad m => Int -> Stream m Int -> m ()
313352filterDrop n = composeN n $ S.drop 1 . S.filter (<= maxValue)
353+
354+{-# INLINE filterTake #-}
355+filterTake :: Monad m => Int -> Stream m Int -> m ()
314356filterTake n = composeN n $ S.take maxValue . S.filter (<= maxValue)
357+
358+{-# INLINE filterScan #-}
359+filterScan :: Monad m => Int -> Stream m Int -> m ()
315360filterScan n = composeN n $ S.scanl' (+) 0 . S.filter (<= maxBound)
316−filterMap n = composeN n $ S.map (subtract 1) . S.filter (<= maxValue)
361+
362+{-# INLINE filterMap #-}
363+filterMap :: Monad m => Int -> Stream m Int -> m ()
364+filterMap n = composeN n $ S.map (subtract 1) . S.filter (<= maxValue)
317365
318366-------------------------------------------------------------------------------
319367-- ConcatMap