3-- Copyright : (c) 2018 Composewell Technologies
4-- License : BSD-3-Clause
5-- Maintainer : streamly@composewell.com
8{-# LANGUAGE FlexibleContexts #-}
9{-# LANGUAGE ScopedTypeVariables #-}
10{-# LANGUAGE RankNTypes #-}
13{-# OPTIONS_GHC -Wno-deprecations #-}
16module Stream.Lift (benchmarks) where
18import Control.DeepSeq (NFData(..))
19import Control.Monad.State.Strict (StateT, get, put)
20import Data.Functor.Identity (Identity)
21import Stream.Common (sourceUnfoldr, sourceUnfoldrM, benchIOSrc)
22import System.Random (randomRIO)
24import qualified Stream.Common as Common
25import qualified Streamly.Internal.Data.Fold as Fold
28import qualified Streamly.Internal.Data.Stream.IsStream as Stream
30import Streamly.Internal.Data.Stream (Stream)
31import qualified Streamly.Internal.Data.Stream as Stream
33import Stream.Common (benchIO, drain)
34import Streamly.Internal.Data.StreamK (StreamK)
35import qualified Streamly.Internal.Data.StreamK as StreamK
36import qualified Control.Monad.State.Strict as State
41import Streamly.Benchmark.Common
42import Prelude hiding (reverse, tail)
45type Stream = Stream.SerialT
48-------------------------------------------------------------------------------
49-- Monad transformation (hoisting etc.)
50-------------------------------------------------------------------------------
52{-# INLINE sourceUnfoldrState #-}
53sourceUnfoldrState :: Common.MonadAsync m =>
54 Int -> Int -> Stream (StateT Int m) Int
55sourceUnfoldrState value n = Stream.unfoldrM step n
63 return (Just (s, cnt + 1))
65{-# INLINE evalStateT #-}
66evalStateT :: Common.MonadAsync m => Int -> Int -> Stream m Int
68 Stream.evalStateT (return 0) (sourceUnfoldrState value n)
70{-# INLINE withState #-}
71withState :: Common.MonadAsync m => Int -> Int -> Stream m Int
74 (return (0 :: Int)) (Stream.liftInner (sourceUnfoldrM value n))
76{-# INLINE benchHoistSink #-}
79 => Int -> String -> (Stream Identity Int -> IO b) -> Benchmark
80benchHoistSink value name f =
81 bench name $ nfIO $ randomRIO (1,1) >>= f . sourceUnfoldr value
83o_1_space_hoisting :: Int -> [Benchmark]
84o_1_space_hoisting value =
86 [ benchIOSrc "evalState" (evalStateT value)
87 , benchIOSrc "withState" (withState value)
89 , benchHoistSink value "generalizeInner"
90 ((\xs -> Stream.fold Fold.length xs :: IO Int)
91 . Stream.generalizeInner)
97{-# INLINE iterateStateIO #-}
110-- XXX This is basically testing the perf of concatEffect, change it to just
111-- use concatEffect and move it along with other concatMap benchmarks.
112{-# INLINE iterateStateT #-}
113iterateStateT :: Int -> StreamK (StateT Int IO) Int
114iterateStateT n = StreamK.concatEffect $ do
119 return $ iterateStateT n
120 else return $ StreamK.fromPure x
122o_n_heap_transformer :: Int -> [Benchmark]
123o_n_heap_transformer value =
124 [ bgroup "transformer"
125 [ benchIO "StateT Int IO (n times) (baseline)" $ \n ->
126 State.evalStateT (iterateStateIO n) value
127 , benchIO "Stream (StateT Int IO) (n times)" $ \n ->
128 State.evalStateT (drain $ Common.toStream (iterateStateT n)) value
133-------------------------------------------------------------------------------
135-------------------------------------------------------------------------------
137-- In addition to gauge options, the number of elements in the stream can be
138-- passed using the --stream-size option.
140benchmarks :: String -> Int -> [Benchmark]
141benchmarks moduleName size =
142 [ bgroup (o_1_space_prefix moduleName) (o_1_space_hoisting size)
144 , bgroup (o_n_heap_prefix moduleName) (o_n_heap_transformer size)
keysesc to close
- j k
- move down, up
- enter
- open
- esc
- back, or up a level
- 1 2 3 4
- readme, files, commits, refs
- /
- filter
- y
- copy the clone command
- c
- compare the chosen commit with another (or shift-click both)
- ?
- show or hide this list