2-- Module : Streamly.Internal.Data.Stream.Async.Channel
3-- Copyright : (c) 2017 Composewell Technologies
4-- License : BSD-3-Clause
5-- Maintainer : streamly@composewell.com
6-- Stability : experimental
9module Streamly.Internal.Data.Stream.Async.Channel
33 -- XXX Move to the higher level Async module. Low level module would have
34 -- only explicit channel operations.
36 -- * Stream operations
40 , concatMapWithK -- appendMap
41 , concatMapInterleaveWithK -- interleaveMap
45import Control.Monad.IO.Class (MonadIO(liftIO))
46import Streamly.Internal.Control.Concurrent (MonadAsync, askRunInIO)
47import Streamly.Internal.Data.Stream.Async.Channel.Operations
48 (fromChannel, fromChannelK, toChannel, toChannelK)
50import qualified Streamly.Internal.Data.Stream as Stream
51import qualified Streamly.Internal.Data.Stream.Async.Channel.Append as Append
52import qualified Streamly.Internal.Data.Stream.Async.Channel.Interleave
54import qualified Streamly.Internal.Data.Stream.StreamK.Type as K
56import Streamly.Internal.Data.Stream.Async.Channel.Type
57import Streamly.Internal.Data.Stream.Channel.Types
59-- | Allocate a channel and evaluate the stream using the channel and the
60-- supplied evaluator function. The evaluator is run in a worker thread.
61{-# INLINE withChannelK #-}
62withChannelK :: MonadAsync m =>
63 (Channel m b -> K.Stream m a -> K.Stream m b)
67withChannelK evaluator modifier input = K.concatEffect action
72 chan <- Append.newChannel modifier
73 toChannelK chan (evaluator chan input)
74 return $ fromChannelK chan
76-- XXX Dedup with withChannelK
78{-# INLINE withInterleaveChannelK #-}
79withInterleaveChannelK :: MonadAsync m =>
80 (Channel m b -> K.Stream m a -> K.Stream m b)
84withInterleaveChannelK evaluator modifier input = K.concatEffect action
89 chan <- Interleave.newChannel modifier
90 toChannelK chan (evaluator chan input)
91 return $ Stream.toStreamK $ fromChannel chan
93-------------------------------------------------------------------------------
95-------------------------------------------------------------------------------
97-- XXX This should go in the parallel module, this is not specific to async.
99-- | Evaluate the stream asynchronously in a worker thread separate from the
102-- >> evalWithK = withChannelK (const id)
104{-# INLINABLE evalWithK #-}
105evalWithK :: MonadAsync m => (Config -> Config) -> K.Stream m a -> K.Stream m a
106evalWithK = withChannelK (const id)
109{-# INLINE_NORMAL evalWithD #-}
110evalWithD :: MonadAsync m => (Config -> Config) -> D.Stream m a -> D.Stream m a
111evalWithD modifier m = D.Stream step Nothing
115 chan <- newChannel modifier
116 sendFirstWorker chan (D.toStreamK m)
117 -- XXX should use an unfold to make this efficient
118 return $ D.Skip $ Just $ fromChannelD chan
120 step gst (Just (D.UnStream step1 st)) = do
123 D.Yield a s -> D.Yield a (Just $ D.Stream step1 s)
124 D.Skip s -> D.Skip (Just $ D.Stream step1 s)
128-------------------------------------------------------------------------------
129-- appending two streams
130-------------------------------------------------------------------------------
132-- | Create a new channel and add both the streams to it for async evaluation.
133-- The output stream is the result of the evaluation.
134{-# INLINE appendWithK #-}
135appendWithK :: MonadAsync m =>
136 (Config -> Config) -> K.Stream m a -> K.Stream m a -> K.Stream m a
137appendWithK modifier stream1 stream2 = K.concatEffect action
142 chan <- Append.newChannel modifier
143 toChannelK chan stream2
144 toChannelK chan stream1
145 return $ Stream.toStreamK $ fromChannel chan
147-- | Create a new channel and add both the streams to it for async evaluation.
148-- The output stream is the result of the evaluation.
149{-# INLINE interleaveWithK #-}
150interleaveWithK :: MonadAsync m =>
151 (Config -> Config) -> K.Stream m a -> K.Stream m a -> K.Stream m a
152interleaveWithK modifier stream1 stream2 = K.concatEffect action
157 chan <- Interleave.newChannel modifier
158 toChannelK chan stream2
159 toChannelK chan stream1
160 return $ Stream.toStreamK $ fromChannel chan
162-- | Evaluate the first stream in the current thread and add the second stream
163-- to the supplied channel. This is to be used by a worker thread.
165-- This can be used with concatMapWith:
168-- concatMap = K.concatMapWith (_appendWithChanK chan) f stream
171{-# INLINE _appendWithChanK #-}
172_appendWithChanK :: MonadAsync m =>
173 Channel m a -> K.Stream m a -> K.Stream m a -> K.Stream m a
174_appendWithChanK chan stream1 stream2 =
175 K.before (toChannelK chan stream2) stream1
177-------------------------------------------------------------------------------
179-------------------------------------------------------------------------------
181-- | A runner function takes a queuing function @q@ and a stream, it splits the
182-- input stream, queuing the tail and using the head to generate a stream.
183-- 'mkEnqueue' takes a runner function and generates the queuing function @q@.
184-- Note that @q@ and the runner are mutually recursive, mkEnqueue ties the knot
186{-# INLINE mkEnqueue #-}
187mkEnqueue :: MonadAsync m =>
189 -> ((K.Stream m a -> m ()) -> K.Stream m a -> K.Stream m b)
190 -> m (K.Stream m a -> m ())
191mkEnqueue chan runner = do
192 runInIO <- askRunInIO
195 -- Enqueue the outer loop
196 liftIO $ enqueue chan False (runInIO, runner q stream)
197 -- XXX In case of eager dispatch we can just directly dispatch
198 -- a worker with the tail stream here rather than first queuing
199 -- and then dispatching a worker which dequeues the work. The
200 -- older implementation did a direct dispatch here and its perf
201 -- characterstics looked much better.
205-- XXX Can be renamed to concatMapWithK if we move concatMapWithK to higher
206-- level module. We can keep only Channel based ops in this module.
208-- | Takes the head element of the input stream and queues the tail of the
209-- stream to the channel, then maps the supplied function on the head and
210-- evaluates the resulting stream.
212-- This function is designed to be used by worker threads on a channel to
213-- concurrently map and evaluate a stream.
214{-# INLINE concatMapWithChanK #-}
215concatMapWithChanK :: MonadAsync m =>
216 Channel m b -> (a -> K.Stream m b) -> K.Stream m a -> K.Stream m b
217concatMapWithChanK chan f stream =
218 let run q = concatMapDivK q f
219 in K.concatMapEffect (`run` stream) (mkEnqueue chan run)
220 -- K.concatMapWith (_appendWithChanK chan) f stream
222-- XXX Add a deep evaluation variant that evaluates individual elements in the
223-- generated streams in parallel.
225-- | Allocate a channel and use it to concurrently evaluate the streams
226-- generated by the mapped function.
228-- >> concatMapWithK modifier f = withChannelK (`concatMapWithChanK` f) modifier
230{-# INLINE concatMapWithK #-}
231concatMapWithK :: MonadAsync m =>
232 (Config -> Config) -> (a -> K.Stream m b) -> K.Stream m a -> K.Stream m b
233concatMapWithK modifier f = withChannelK (`concatMapWithChanK` f) modifier
235-- | Allocate a channel and use it to concurrently evaluate the streams
236-- generated by the mapped function.
238-- >> concatMapInterleaveWithK modifier f = withInterleaveChannelK (`concatMapWithChanK` f) modifier
240{-# INLINE concatMapInterleaveWithK #-}
241concatMapInterleaveWithK :: MonadAsync m =>
242 (Config -> Config) -> (a -> K.Stream m b) -> K.Stream m a -> K.Stream m b
243concatMapInterleaveWithK modifier f =
244 withInterleaveChannelK (`concatMapWithChanK` f) modifier
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