streamly/benchmark/LinearOps.hs

227 lines
7.1 KiB
Haskell
Raw Normal View History

-- |
-- Module : BenchmarkOps
-- Copyright : (c) 2018 Harendra Kumar
--
-- License : MIT
-- Maintainer : harendra.kumar@gmail.com
{-# LANGUAGE FlexibleContexts #-}
2018-04-24 15:13:10 +03:00
module LinearOps where
import Prelude
(Monad, Int, (+), ($), (.), return, fmap, even, (>), (<=),
subtract, undefined, Maybe(..), odd)
import qualified Streamly as S
import qualified Streamly.Prelude as S
value, maxValue :: Int
value = 100000
maxValue = value + 1000
-------------------------------------------------------------------------------
-- Benchmark ops
-------------------------------------------------------------------------------
{-# INLINE scan #-}
2018-06-25 01:21:02 +03:00
{-# INLINE mapM_ #-}
{-# INLINE map #-}
{-# INLINE fmap #-}
2018-06-25 01:21:02 +03:00
{-# INLINE mapMaybe #-}
{-# INLINE filterEven #-}
{-# INLINE filterAllOut #-}
{-# INLINE filterAllIn #-}
{-# INLINE takeOne #-}
{-# INLINE takeAll #-}
{-# INLINE takeWhileTrue #-}
{-# INLINE dropAll #-}
{-# INLINE dropWhileTrue #-}
{-# INLINE zip #-}
{-# INLINE concat #-}
{-# INLINE composeAllInFilters #-}
{-# INLINE composeAllOutFilters #-}
{-# INLINE composeMapAllInFilter #-}
2018-06-25 01:21:02 +03:00
scan, mapM_, map, fmap, mapMaybe, filterEven, filterAllOut,
filterAllIn, takeOne, takeAll, takeWhileTrue, dropAll, dropWhileTrue, zip,
concat, composeAllInFilters, composeAllOutFilters,
composeMapAllInFilter
:: Monad m
=> Stream m Int -> m ()
{-# INLINE composeMapM #-}
2018-06-25 01:21:02 +03:00
{-# INLINE zipAsync #-}
{-# INLINE mapMaybeM #-}
composeMapM, zipAsync, mapMaybeM :: S.MonadAsync m => Stream m Int -> m ()
{-# INLINE toList #-}
{-# INLINE foldr #-}
{-# INLINE foldrM #-}
toList, foldr, foldrM :: Monad m => Stream m Int -> m [Int]
2018-06-25 01:21:02 +03:00
{-# INLINE last #-}
2018-06-25 01:21:02 +03:00
{-# INLINE maximum #-}
{-# INLINE minimum #-}
last, minimum, maximum :: Monad m => Stream m Int -> m (Maybe Int)
{-# INLINE foldl #-}
{-# INLINE length #-}
{-# INLINE sum #-}
{-# INLINE product #-}
foldl, length, sum, product :: Monad m => Stream m Int -> m Int
{-# INLINE toNull #-}
toNull :: Monad m => (t m Int -> S.SerialT m Int) -> t m Int -> m ()
2018-06-25 01:21:02 +03:00
{-# INLINE mapM #-}
2018-06-09 18:41:15 +03:00
mapM :: (S.IsStream t, S.MonadAsync m)
=> (t m Int -> S.SerialT m Int) -> t m Int -> m ()
2018-06-25 01:21:02 +03:00
{-# INLINE sequence #-}
sequence :: (S.IsStream t, S.MonadAsync m)
=> (t m Int -> S.SerialT m Int) -> t m (m Int) -> m ()
-------------------------------------------------------------------------------
-- Stream generation and elimination
-------------------------------------------------------------------------------
2018-05-13 07:57:49 +03:00
type Stream m a = S.SerialT m a
{-# INLINE source #-}
2018-06-09 18:41:15 +03:00
source :: (S.MonadAsync m, S.IsStream t) => Int -> t m Int
source n = S.serially $ sourceUnfoldrM n
-- source n = S.serially $ sourceFromList n
{-# INLINE sourceFromList #-}
sourceFromList :: (Monad m, S.IsStream t) => Int -> t m Int
sourceFromList n = S.fromList [n..n+value]
{-# INLINE sourceFromListM #-}
sourceFromListM :: (S.MonadAsync m, S.IsStream t) => Int -> t m Int
sourceFromListM n = S.fromListM (Prelude.fmap return [n..n+value])
{-# INLINE sourceFromFoldable #-}
sourceFromFoldable :: S.IsStream t => Int -> t m Int
sourceFromFoldable n = S.fromFoldable [n..n+value]
{-# INLINE sourceFromFoldableM #-}
sourceFromFoldableM :: (S.IsStream t, S.MonadAsync m) => Int -> t m Int
sourceFromFoldableM n = S.fromFoldableM (Prelude.fmap return [n..n+value])
{-# INLINE sourceFoldMapWith #-}
sourceFoldMapWith :: (S.IsStream t, Monad (t m), S.Semigroup (t m Int))
=> Int -> t m Int
sourceFoldMapWith n = S.foldMapWith (S.<>) return [n..n+value]
{-# INLINE sourceFoldMapWithM #-}
sourceFoldMapWithM :: (S.IsStream t, Monad m, S.Semigroup (t m Int))
=> Int -> t m Int
sourceFoldMapWithM n = S.foldMapWith (S.<>) (S.yieldM . return) [n..n+value]
2018-06-09 18:41:15 +03:00
{-# INLINE sourceUnfoldr #-}
sourceUnfoldr :: (Monad m, S.IsStream t) => Int -> t m Int
2018-06-09 18:41:15 +03:00
sourceUnfoldr n = S.unfoldr step n
where
step cnt =
if cnt > n + value
then Nothing
else (Just (cnt, cnt + 1))
{-# INLINE sourceUnfoldrM #-}
sourceUnfoldrM :: (S.IsStream t, S.MonadAsync m) => Int -> t m Int
sourceUnfoldrM n = S.unfoldrM step n
where
step cnt =
if cnt > n + value
then return Nothing
else return (Just (cnt, cnt + 1))
{-# INLINE sourceUnfoldrMAction #-}
sourceUnfoldrMAction :: (S.IsStream t, S.MonadAsync m) => Int -> t m (m Int)
sourceUnfoldrMAction n = S.serially $ S.unfoldrM step n
where
step cnt =
if cnt > n + value
then return Nothing
else return (Just (return cnt, cnt + 1))
-------------------------------------------------------------------------------
-- Elimination
-------------------------------------------------------------------------------
{-# INLINE runStream #-}
runStream :: Monad m => Stream m a -> m ()
runStream = S.runStream
toNull t = runStream . t
mapM_ = S.mapM_ (\_ -> return ())
toList = S.toList
foldr = S.foldr (:) []
foldrM = S.foldrM (\a xs -> return (a : xs)) []
foldl = S.foldl' (+) 0
last = S.last
2018-06-25 01:21:02 +03:00
length = S.length
sum = S.sum
product = S.product
maximum = S.maximum
minimum = S.minimum
-------------------------------------------------------------------------------
-- Transformation
-------------------------------------------------------------------------------
{-# INLINE transform #-}
transform :: Monad m => Stream m a -> m ()
transform = runStream
scan = transform . S.scanl' (+) 0
fmap = transform . Prelude.fmap (+1)
map = transform . S.map (+1)
2018-06-09 18:41:15 +03:00
mapM t = transform . t . S.mapM return
mapMaybe = transform . S.mapMaybe
(\x -> if Prelude.odd x then Nothing else Just ())
mapMaybeM = transform . S.mapMaybeM
(\x -> if Prelude.odd x then (return Nothing) else return $ Just ())
sequence t = transform . t . S.sequence
filterEven = transform . S.filter even
filterAllOut = transform . S.filter (> maxValue)
filterAllIn = transform . S.filter (<= maxValue)
takeOne = transform . S.take 1
takeAll = transform . S.take maxValue
takeWhileTrue = transform . S.takeWhile (<= maxValue)
dropAll = transform . S.drop maxValue
dropWhileTrue = transform . S.dropWhile (<= maxValue)
-------------------------------------------------------------------------------
-- Zipping and concat
-------------------------------------------------------------------------------
zip src = transform $ (S.zipWith (,) src src)
2018-06-09 18:41:15 +03:00
zipAsync src = transform $ (S.zipAsyncWith (,) src src)
concat _n = return ()
-------------------------------------------------------------------------------
-- Composition
-------------------------------------------------------------------------------
{-# INLINE compose #-}
compose :: Monad m => (Stream m Int -> Stream m Int) -> Stream m Int -> m ()
compose f = transform . f . f . f . f
composeMapM = compose (S.mapM return)
composeAllInFilters = compose (S.filter (<= maxValue))
composeAllOutFilters = compose (S.filter (> maxValue))
composeMapAllInFilter =
compose (S.filter (<= maxValue) . Prelude.fmap (subtract 1))
2018-05-27 08:03:21 +03:00
{-# INLINABLE composeScaling #-}
composeScaling :: Monad m => Int -> Stream m Int -> m ()
composeScaling m =
case m of
1 -> transform . f
2 -> transform . f . f
3 -> transform . f . f . f
4 -> transform . f . f . f . f
_ -> undefined
where f = S.filter (<= maxValue)