@@ -22,18 +22,18 @@ module Streamly.Internal.FileSystem.Handle
22 -- , readUtf8
22 -- , readUtf8
23 -- , readLines
23 -- , readLines
24 -- , readFrames
24 -- , readFrames
25− , readRequestsOf
25+ , readWithBufferOf
27 , toStream
27 , toStream
28− , toStreamRequestsOf
28+ , toStreamWithBufferOf
30 -- -- * Array Read
30 -- -- * Array Read
31 -- , readArrayUpto
31 -- , readArrayUpto
32 -- , readArrayOf
32 -- , readArrayOf
33 , readChunks
33 , readChunks
34− , readChunksRequestsOf
34+ , readChunksWithBufferOf
36− , toChunksRequestsOf
36+ , toChunksWithBufferOf
37 , toChunks
37 , toChunks
39 -- ** Write to Handle
39 -- ** Write to Handle
@@ -42,20 +42,20 @@ module Streamly.Internal.FileSystem.Handle
42 -- , writeUtf8
42 -- , writeUtf8
43 -- , writeUtf8ByLines
43 -- , writeUtf8ByLines
44 -- , writeByFrames
44 -- , writeByFrames
45− , writeRequestsOf
45+ , writeWithBufferOf
47 -- Byte stream write (Streams)
47 -- Byte stream write (Streams)
48 , fromStream
48 , fromStream
49− , fromStreamInChunksOf
49+ , fromStreamWithBufferOf
51 -- -- * Array Write
51 -- -- * Array Write
52 , writeArray
52 , writeArray
53 , writeChunks
53 , writeChunks
54− , writeChunksRequestsOf
54+ , writeChunksWithBufferOf
56 -- -- * Array stream Write
56 -- -- * Array stream Write
57− , fromStreamArrays
57+ , fromChunks
58− , fromStreamArraysInChunksOf
58+ , fromChunksWithBufferOf
60 -- -- * Random Access (Seek)
60 -- -- * Random Access (Seek)
61 -- -- | Unlike the streaming APIs listed above, these APIs apply to devices or
61 -- -- | Unlike the streaming APIs listed above, these APIs apply to devices or
@@ -79,8 +79,8 @@ module Streamly.Internal.FileSystem.Handle
80 -- , readChunksFrom
80 -- , readChunksFrom
81 -- , readChunksFromTo
81 -- , readChunksFromTo
82− -- , readChunksFromToRequestsOf
82+ -- , readChunksFromToWithBufferOf
83− -- , readChunksFromThenToRequestsOf
83+ -- , readChunksFromThenToWithBufferOf
85 -- , writeIndex
85 -- , writeIndex
86 -- , writeFrom -- start writing at the given position
86 -- , writeFrom -- start writing at the given position
@@ -92,8 +92,8 @@ module Streamly.Internal.FileSystem.Handle
93 -- , writeChunksFrom
93 -- , writeChunksFrom
94 -- , writeChunksFromTo
94 -- , writeChunksFromTo
95− -- , writeChunksFromToRequestsOf
95+ -- , writeChunksFromToWithBufferOf
96− -- , writeChunksFromThenToRequestsOf
96+ -- , writeChunksFromThenToWithBufferOf
@@ -164,13 +164,13 @@ readArrayUpto size h = do
164-- Stream of Arrays IO
164-- Stream of Arrays IO
165-------------------------------------------------------------------------------
165-------------------------------------------------------------------------------
167−-- | @toChunksRequestsOf size h@ reads a stream of arrays from file handle @h@.
167+-- | @toChunksWithBufferOf size h@ reads a stream of arrays from file handle @h@.
168-- The maximum size of a single array is specified by @size@. The actual size
168-- The maximum size of a single array is specified by @size@. The actual size
169-- read may be less than or equal to @size@.
169-- read may be less than or equal to @size@.
170−{-# INLINABLE _toChunksRequestsOf #-}
170+{-# INLINABLE _toChunksWithBufferOf #-}
171−_toChunksRequestsOf :: (IsStream t, MonadIO m)
171+_toChunksWithBufferOf :: (IsStream t, MonadIO m)
172 => Int -> Handle -> t m (Array Word8)
172 => Int -> Handle -> t m (Array Word8)
173−_toChunksRequestsOf size h = go
173+_toChunksWithBufferOf size h = go
175 -- XXX use cons/nil instead
175 -- XXX use cons/nil instead
176 go = mkStream $ \_ yld _ stp -> do
176 go = mkStream $ \_ yld _ stp -> do
@@ -179,14 +179,14 @@ _toChunksRequestsOf size h = go
180 else yld arr go
180 else yld arr go
182−-- | @toChunksRequestsOf size handle@ reads a stream of arrays from the file
182+-- | @toChunksWithBufferOf size handle@ reads a stream of arrays from the file
183-- handle @handle@. The maximum size of a single array is limited to @size@.
183-- handle @handle@. The maximum size of a single array is limited to @size@.
184-- The actual size read may be less than or equal to @size@.
184-- The actual size read may be less than or equal to @size@.
186-- @since 0.7.0
186-- @since 0.7.0
187−{-# INLINE_NORMAL toChunksRequestsOf #-}
187+{-# INLINE_NORMAL toChunksWithBufferOf #-}
188−toChunksRequestsOf :: (IsStream t, MonadIO m) => Int -> Handle -> t m (Array Word8)
188+toChunksWithBufferOf :: (IsStream t, MonadIO m) => Int -> Handle -> t m (Array Word8)
189−toChunksRequestsOf size h = D.fromStreamD (D.Stream step ())
189+toChunksWithBufferOf size h = D.fromStreamD (D.Stream step ())
191 {-# INLINE_LATE step #-}
191 {-# INLINE_LATE step #-}
192 step _ _ = do
192 step _ _ = do
@@ -196,14 +196,15 @@ toChunksRequestsOf size h = D.fromStreamD (D.Stream step ())
196 0 -> D.Stop
196 0 -> D.Stop
197 _ -> D.Yield arr ()
197 _ -> D.Yield arr ()
199−-- | Unfold the tuple @(size, handle)@ into a stream of 'Word8' arrays. The
199+-- | Unfold the tuple @(bufsize, handle)@ into a stream of 'Word8' arrays.
200−-- stream consists of arrays representing chunks of data read from the handle.
200+-- Read requests to the IO device are performed using a buffer of size
201−-- The size of an array in the stream is less than or equal to @size@.
201+-- @bufsize@. The size of an array in the resulting stream is always less than
202+-- or equal to @bufsize@.
203-- @since 0.7.0
204-- @since 0.7.0
204−{-# INLINE_NORMAL readChunksRequestsOf #-}
205+{-# INLINE_NORMAL readChunksWithBufferOf #-}
205−readChunksRequestsOf :: MonadIO m => Unfold m (Int, Handle) (Array Word8)
206+readChunksWithBufferOf :: MonadIO m => Unfold m (Int, Handle) (Array Word8)
206−readChunksRequestsOf = Unfold step return
207+readChunksWithBufferOf = Unfold step return
208 {-# INLINE_LATE step #-}
209 {-# INLINE_LATE step #-}
209 step (size, h) = do
210 step (size, h) = do
@@ -220,12 +221,12 @@ readChunksRequestsOf = Unfold step return
220-- @defaultChunkSize@. The actual size read may be less than or equal to
221-- @defaultChunkSize@. The actual size read may be less than or equal to
221-- @defaultChunkSize@.
222-- @defaultChunkSize@.
223−-- > toChunks = toChunksRequestsOf defaultChunkSize
224+-- > toChunks = toChunksWithBufferOf defaultChunkSize
225-- @since 0.7.0
226-- @since 0.7.0
226{-# INLINE toChunks #-}
227{-# INLINE toChunks #-}
227toChunks :: (IsStream t, MonadIO m) => Handle -> t m (Array Word8)
228toChunks :: (IsStream t, MonadIO m) => Handle -> t m (Array Word8)
228−toChunks = toChunksRequestsOf defaultChunkSize
229+toChunks = toChunksWithBufferOf defaultChunkSize
230-- | Unfolds a handle into a stream of 'Word8' arrays. Requests to the IO
231-- | Unfolds a handle into a stream of 'Word8' arrays. Requests to the IO
231-- device are performed using a buffer of size
232-- device are performed using a buffer of size
@@ -236,7 +237,7 @@ toChunks = toChunksRequestsOf defaultChunkSize
236-- @since 0.7.0
237-- @since 0.7.0
237{-# INLINE readChunks #-}
238{-# INLINE readChunks #-}
238readChunks :: MonadIO m => Unfold m Handle (Array Word8)
239readChunks :: MonadIO m => Unfold m Handle (Array Word8)
239−readChunks = UF.supplyFirst readChunksRequestsOf defaultChunkSize
240+readChunks = UF.supplyFirst readChunksWithBufferOf defaultChunkSize
241-------------------------------------------------------------------------------
242-------------------------------------------------------------------------------
242-- Read File to Stream
243-- Read File to Stream
@@ -246,21 +247,21 @@ readChunks = UF.supplyFirst readChunksRequestsOf defaultChunkSize
246-- read requests at the same time. For serial case we can use async IO. We can
247-- read requests at the same time. For serial case we can use async IO. We can
247-- also control the read throughput in mbps or IOPS.
248-- also control the read throughput in mbps or IOPS.
249−-- | Unfolds the tuple @(chunksize, handle)@ into a byte stream, read requests
250+-- | Unfolds the tuple @(bufsize, handle)@ into a byte stream, read requests
250−-- to the IO device are performed using buffers of @chunkSize@.
251+-- to the IO device are performed using buffers of @bufsize@.
252-- @since 0.7.0
253-- @since 0.7.0
253−{-# INLINE readRequestsOf #-}
254+{-# INLINE readWithBufferOf #-}
254−readRequestsOf :: MonadIO m => Unfold m (Int, Handle) Word8
255+readWithBufferOf :: MonadIO m => Unfold m (Int, Handle) Word8
255−readRequestsOf = UF.concat readChunksRequestsOf A.read
256+readWithBufferOf = UF.concat readChunksWithBufferOf A.read
257−-- | @toStreamRequestsOf chunkSize handle@ reads a byte stream from a file
258+-- | @toStreamWithBufferOf bufsize handle@ reads a byte stream from a file
258−-- handle, reads are performed in chunks of up to @chunkSize@.
259+-- handle, reads are performed in chunks of up to @bufsize@.
260-- /Internal/
261-- /Internal/
261−{-# INLINE toStreamRequestsOf #-}
262+{-# INLINE toStreamWithBufferOf #-}
262−toStreamRequestsOf :: (IsStream t, MonadIO m) => Int -> Handle -> t m Word8
263+toStreamWithBufferOf :: (IsStream t, MonadIO m) => Int -> Handle -> t m Word8
263−toStreamRequestsOf chunkSize h = AS.concat $ toChunksRequestsOf chunkSize h
264+toStreamWithBufferOf chunkSize h = AS.concat $ toChunksWithBufferOf chunkSize h
266-- Generate a stream of elements of the given type from a file 'Handle'.
267-- Generate a stream of elements of the given type from a file 'Handle'.
@@ -273,7 +274,7 @@ toStreamRequestsOf chunkSize h = AS.concat $ toChunksRequestsOf chunkSize h
273-- @since 0.7.0
274-- @since 0.7.0
274{-# INLINE read #-}
275{-# INLINE read #-}
275read :: MonadIO m => Unfold m Handle Word8
276read :: MonadIO m => Unfold m Handle Word8
276−read = UF.supplyFirst readRequestsOf defaultChunkSize
277+read = UF.supplyFirst readWithBufferOf defaultChunkSize
278-- | Generate a byte stream from a file 'Handle'.
279-- | Generate a byte stream from a file 'Handle'.
@@ -313,33 +314,33 @@ writeArray h Array{..} = withForeignPtr aStart $ \p -> hPutBuf h p aLen
313-- | Write a stream of arrays to a handle.
314-- | Write a stream of arrays to a handle.
315-- @since 0.7.0
316-- @since 0.7.0
316−{-# INLINE fromStreamArrays #-}
317+{-# INLINE fromChunks #-}
317−fromStreamArrays :: (MonadIO m, Storable a)
318+fromChunks :: (MonadIO m, Storable a)
318 => Handle -> SerialT m (Array a) -> m ()
319 => Handle -> SerialT m (Array a) -> m ()
319−fromStreamArrays h m = S.mapM_ (liftIO . writeArray h) m
320+fromChunks h m = S.mapM_ (liftIO . writeArray h) m
321−-- | @fromStreamArraysInChunksOf chunkSize handle stream@ writes a stream of arrays
322+-- | @fromChunksWithBufferOf bufsize handle stream@ writes a stream of arrays
322−-- to @handle@ after coalescing the adjacent arrays in chunks of @chunkSize@.
323+-- to @handle@ after coalescing the adjacent arrays in chunks of @bufsize@.
323-- The chunk size is only a maximum and the actual writes could be smaller as
324-- The chunk size is only a maximum and the actual writes could be smaller as
324-- we do not split the arrays to fit exactly to the specified size.
325-- we do not split the arrays to fit exactly to the specified size.
326-- @since 0.7.0
327-- @since 0.7.0
327−{-# INLINE fromStreamArraysInChunksOf #-}
328+{-# INLINE fromChunksWithBufferOf #-}
328−fromStreamArraysInChunksOf :: (MonadIO m, Storable a)
329+fromChunksWithBufferOf :: (MonadIO m, Storable a)
329 => Int -> Handle -> SerialT m (Array a) -> m ()
330 => Int -> Handle -> SerialT m (Array a) -> m ()
330−fromStreamArraysInChunksOf n h xs = fromStreamArrays h $ AS.compact n xs
331+fromChunksWithBufferOf n h xs = fromChunks h $ AS.compact n xs
332−-- | @fromStreamInChunksOf chunkSize handle stream@ writes @stream@ to @handle@ in
333+-- | @fromStreamWithBufferOf bufsize handle stream@ writes @stream@ to @handle@
333−-- chunks of @chunkSize@. A write is performed to the IO device as soon as we
334+-- in chunks of @bufsize@. A write is performed to the IO device as soon as we
334-- collect the required input size.
335-- collect the required input size.
336-- @since 0.7.0
337-- @since 0.7.0
337−{-# INLINE fromStreamInChunksOf #-}
338+{-# INLINE fromStreamWithBufferOf #-}
338−fromStreamInChunksOf :: MonadIO m => Int -> Handle -> SerialT m Word8 -> m ()
339+fromStreamWithBufferOf :: MonadIO m => Int -> Handle -> SerialT m Word8 -> m ()
339−fromStreamInChunksOf n h m = fromStreamArrays h $ S.arraysOf n m
340+fromStreamWithBufferOf n h m = fromChunks h $ S.arraysOf n m
340−-- fromStreamInChunksOf n h m = fromStreamArrays h $ AS.arraysOf n m
341+-- fromStreamWithBufferOf n h m = fromChunks h $ AS.arraysOf n m
342−-- > write = 'writeRequestsOf' A.defaultChunkSize
343+-- > write = 'writeWithBufferOf' A.defaultChunkSize
344-- | Write a byte stream to a file handle. Accumulates the input in chunks of
345-- | Write a byte stream to a file handle. Accumulates the input in chunks of
345-- up to 'Streamly.Internal.Memory.Array.Types.defaultChunkSize' before writing.
346-- up to 'Streamly.Internal.Memory.Array.Types.defaultChunkSize' before writing.
@@ -350,7 +351,7 @@ fromStreamInChunksOf n h m = fromStreamArrays h $ S.arraysOf n m
350-- @since 0.7.0
351-- @since 0.7.0
351{-# INLINE fromStream #-}
352{-# INLINE fromStream #-}
352fromStream :: MonadIO m => Handle -> SerialT m Word8 -> m ()
353fromStream :: MonadIO m => Handle -> SerialT m Word8 -> m ()
353−fromStream = fromStreamInChunksOf defaultChunkSize
354+fromStream = fromStreamWithBufferOf defaultChunkSize
355-- | Write a stream of arrays to a handle. Each array in the stream is written
356-- | Write a stream of arrays to a handle. Each array in the stream is written
356-- to the device as a separate IO request.
357-- to the device as a separate IO request.
@@ -360,17 +361,17 @@ fromStream = fromStreamInChunksOf defaultChunkSize
360writeChunks :: (MonadIO m, Storable a) => Handle -> Fold m (Array a) ()
361writeChunks :: (MonadIO m, Storable a) => Handle -> Fold m (Array a) ()
361writeChunks h = FL.drainBy (liftIO . writeArray h)
362writeChunks h = FL.drainBy (liftIO . writeArray h)
363−-- | @writeChunksRequestsOf chunkSize handle@ writes a stream of arrays
364+-- | @writeChunksWithBufferOf bufsize handle@ writes a stream of arrays
364−-- to @handle@ after coalescing the adjacent arrays in chunks of @chunkSize@.
365+-- to @handle@ after coalescing the adjacent arrays in chunks of @bufsize@.
365-- We never split an array, if a single array is bigger than the specified size
366-- We never split an array, if a single array is bigger than the specified size
366-- it emitted as it is. Multiple arrays are coalesed as long as the total size
367-- it emitted as it is. Multiple arrays are coalesed as long as the total size
367-- remains below the specified size.
368-- remains below the specified size.
369-- @since 0.7.0
370-- @since 0.7.0
370−{-# INLINE writeChunksRequestsOf #-}
371+{-# INLINE writeChunksWithBufferOf #-}
371−writeChunksRequestsOf :: (MonadIO m, Storable a)
372+writeChunksWithBufferOf :: (MonadIO m, Storable a)
372 => Int -> Handle -> Fold m (Array a) ()
373 => Int -> Handle -> Fold m (Array a) ()
373−writeChunksRequestsOf n h = lpackArraysChunksOf n (writeChunks h)
374+writeChunksWithBufferOf n h = lpackArraysChunksOf n (writeChunks h)
375-- GHC buffer size dEFAULT_FD_BUFFER_SIZE=8192 bytes.
376-- GHC buffer size dEFAULT_FD_BUFFER_SIZE=8192 bytes.
@@ -380,16 +381,16 @@ writeChunksRequestsOf n h = lpackArraysChunksOf n (writeChunks h)
380-- do not want buffering to occur at GHC level as well. Same thing applies to
381-- do not want buffering to occur at GHC level as well. Same thing applies to
381-- writes as well.
382-- writes as well.
383−-- | @writeRequestsOf reqSize handle@ writes the input stream to @handle@.
384+-- | @writeWithBufferOf reqSize handle@ writes the input stream to @handle@.
384-- Bytes in the input stream are collected into a buffer until we have a chunk
385-- Bytes in the input stream are collected into a buffer until we have a chunk
385-- of @reqSize@ and then written to the IO device.
386-- of @reqSize@ and then written to the IO device.
387-- @since 0.7.0
388-- @since 0.7.0
388−{-# INLINE writeRequestsOf #-}
389+{-# INLINE writeWithBufferOf #-}
389−writeRequestsOf :: MonadIO m => Int -> Handle -> Fold m Word8 ()
390+writeWithBufferOf :: MonadIO m => Int -> Handle -> Fold m Word8 ()
390−writeRequestsOf n h = FL.lchunksOf n (writeNUnsafe n) (writeChunks h)
391+writeWithBufferOf n h = FL.lchunksOf n (writeNUnsafe n) (writeChunks h)
392−-- > write = 'writeRequestsOf' A.defaultChunkSize
393+-- > write = 'writeWithBufferOf' A.defaultChunkSize
394-- | Write a byte stream to a file handle. Accumulates the input in chunks of
395-- | Write a byte stream to a file handle. Accumulates the input in chunks of
395-- up to 'Streamly.Internal.Memory.Array.Types.defaultChunkSize' before writing
396-- up to 'Streamly.Internal.Memory.Array.Types.defaultChunkSize' before writing
@@ -398,7 +399,7 @@ writeRequestsOf n h = FL.lchunksOf n (writeNUnsafe n) (writeChunks h)
398-- @since 0.7.0
399-- @since 0.7.0
399{-# INLINE write #-}
400{-# INLINE write #-}
400write :: MonadIO m => Handle -> Fold m Word8 ()
401write :: MonadIO m => Handle -> Fold m Word8 ()
401−write = writeRequestsOf defaultChunkSize
402+write = writeWithBufferOf defaultChunkSize
404{-# INLINE write #-}
405{-# INLINE write #-}