on master help sign in
Rename bufferChunks, writeChunks, flattenArrays
ff40cf0Harendra Kumar
core/src/Streamly/Internal/Data/Array/Type.hs+31−13
@@ -91,12 +91,15 @@ module Streamly.Internal.Data.Array.Type
91 -- ** Streams of arrays
91 -- ** Streams of arrays
92 , chunksOf
92 , chunksOf
93 , pinnedChunksOf
93 , pinnedChunksOf
94− , bufferChunks
94+ , buildChunks
95− , flattenArrays
95+ , concatChunks
96− , flattenArraysRev
96+ , concatChunksRev
97
97
98 -- ** Deprecated
98 -- ** Deprecated
99 , unsafeIndex
99 , unsafeIndex
100+ , bufferChunks
101+ , flattenArrays
102+ , flattenArraysRev
100 )
103 )
101where
104where
102
105
@@ -348,10 +351,15 @@ pinnedClone = fmap unsafeFreeze . MA.pinnedClone . unsafeThaw
348-- Streams of arrays
351-- Streams of arrays
349-------------------------------------------------------------------------------
352-------------------------------------------------------------------------------
350
353
351−{-# INLINE bufferChunks #-}
354+{-# INLINE buildChunks #-}
355+buildChunks :: (MonadIO m, Unbox a) =>
356+ D.Stream m a -> m (K.StreamK m (Array a))
357+buildChunks m = D.foldr K.cons K.nil $ chunksOf defaultChunkSize m
358+
359+{-# DEPRECATED bufferChunks "Please use buildChunks instead." #-}
352bufferChunks :: (MonadIO m, Unbox a) =>
360bufferChunks :: (MonadIO m, Unbox a) =>
353 D.Stream m a -> m (K.StreamK m (Array a))
361 D.Stream m a -> m (K.StreamK m (Array a))
354−bufferChunks m = D.foldr K.cons K.nil $ chunksOf defaultChunkSize m
362+bufferChunks = buildChunks
355
363
356-- | @chunksOf n stream@ groups the elements in the input stream into arrays of
364-- | @chunksOf n stream@ groups the elements in the input stream into arrays of
357-- @n@ elements each.
365-- @n@ elements each.
@@ -372,27 +380,37 @@ pinnedChunksOf :: forall m a. (MonadIO m, Unbox a)
372 => Int -> D.Stream m a -> D.Stream m (Array a)
380 => Int -> D.Stream m a -> D.Stream m (Array a)
373pinnedChunksOf n str = D.map unsafeFreeze $ MA.pinnedChunksOf n str
381pinnedChunksOf n str = D.map unsafeFreeze $ MA.pinnedChunksOf n str
374
382
375−-- | Use the "read" unfold instead.
383+-- | Use the "reader" unfold instead.
376--
384--
377−-- @flattenArrays = unfoldMany read@
385+-- @concatChunks = unfoldMany reader@
378--
386--
379-- We can try this if there are any fusion issues in the unfold.
387-- We can try this if there are any fusion issues in the unfold.
380--
388--
381−{-# INLINE_NORMAL flattenArrays #-}
389+{-# INLINE_NORMAL concatChunks #-}
390+concatChunks :: forall m a. (MonadIO m, Unbox a)
391+ => D.Stream m (Array a) -> D.Stream m a
392+concatChunks = MA.concatChunks . D.map unsafeThaw
393+
394+{-# DEPRECATED flattenArrays "Please use \"unfoldMany reader\" instead." #-}
382flattenArrays :: forall m a. (MonadIO m, Unbox a)
395flattenArrays :: forall m a. (MonadIO m, Unbox a)
383 => D.Stream m (Array a) -> D.Stream m a
396 => D.Stream m (Array a) -> D.Stream m a
384−flattenArrays = MA.flattenArrays . D.map unsafeThaw
397+flattenArrays = concatChunks
385
398
386−-- | Use the "readRev" unfold instead.
399+-- | Use the "readerRev" unfold instead.
387--
400--
388−-- @flattenArrays = unfoldMany readRev@
401+-- @concatChunksRev = unfoldMany readerRev@
389--
402--
390-- We can try this if there are any fusion issues in the unfold.
403-- We can try this if there are any fusion issues in the unfold.
391--
404--
392−{-# INLINE_NORMAL flattenArraysRev #-}
405+{-# INLINE_NORMAL concatChunksRev #-}
406+concatChunksRev :: forall m a. (MonadIO m, Unbox a)
407+ => D.Stream m (Array a) -> D.Stream m a
408+concatChunksRev = MA.concatChunksRev . D.map unsafeThaw
409+
410+{-# DEPRECATED flattenArraysRev "Please use \"unfoldMany readerRev\" instead." #-}
393flattenArraysRev :: forall m a. (MonadIO m, Unbox a)
411flattenArraysRev :: forall m a. (MonadIO m, Unbox a)
394 => D.Stream m (Array a) -> D.Stream m a
412 => D.Stream m (Array a) -> D.Stream m a
395−flattenArraysRev = MA.flattenArraysRev . D.map unsafeThaw
413+flattenArraysRev = concatChunksRev
396
414
397-- Drops the separator byte
415-- Drops the separator byte
398{-# INLINE breakOn #-}
416{-# INLINE breakOn #-}
core/src/Streamly/Internal/Data/MutArray/Type.hs+36−16
@@ -197,11 +197,11 @@ module Streamly.Internal.Data.MutArray.Type
197 -- *** Construct from streams
197 -- *** Construct from streams
198 , chunksOf
198 , chunksOf
199 , pinnedChunksOf
199 , pinnedChunksOf
200− , writeChunks
200+ , buildChunks
201
201
202 -- *** Eliminate to streams
202 -- *** Eliminate to streams
203− , flattenArrays
203+ , concatChunks
204− , flattenArraysRev
204+ , concatChunksRev
205
205
206 -- *** Construct from arrays
206 -- *** Construct from arrays
207 -- get chunks without copying
207 -- get chunks without copying
@@ -230,6 +230,11 @@ module Streamly.Internal.Data.MutArray.Type
230 , memcpy
230 , memcpy
231 , memcmp
231 , memcmp
232 , c_memchr
232 , c_memchr
233+
234+ -- * Deprecated
235+ , writeChunks
236+ , flattenArrays
237+ , flattenArraysRev
233 )
238 )
234where
239where
235
240
@@ -1418,14 +1423,14 @@ data FlattenState s contents a =
1418
1423
1419-- | Use the "reader" unfold instead.
1424-- | Use the "reader" unfold instead.
1420--
1425--
1421−-- @flattenArrays = unfoldMany reader@
1426+-- @concatChunks = unfoldMany reader@
1422--
1427--
1423-- We can try this if there are any fusion issues in the unfold.
1428-- We can try this if there are any fusion issues in the unfold.
1424--
1429--
1425−{-# INLINE_NORMAL flattenArrays #-}
1430+{-# INLINE_NORMAL concatChunks #-}
1426−flattenArrays :: forall m a. (MonadIO m, Unbox a)
1431+concatChunks :: forall m a. (MonadIO m, Unbox a)
1427 => D.Stream m (MutArray a) -> D.Stream m a
1432 => D.Stream m (MutArray a) -> D.Stream m a
1428−flattenArrays (D.Stream step state) = D.Stream step' (OuterLoop state)
1433+concatChunks (D.Stream step state) = D.Stream step' (OuterLoop state)
1429
1434
1430 where
1435 where
1431
1436
@@ -1445,16 +1450,21 @@ flattenArrays (D.Stream step state) = D.Stream step' (OuterLoop state)
1445 x <- liftIO $ peekAt p contents
1450 x <- liftIO $ peekAt p contents
1446 return $ D.Yield x (InnerLoop st contents (INDEX_NEXT(p,a)) end)
1451 return $ D.Yield x (InnerLoop st contents (INDEX_NEXT(p,a)) end)
1447
1452
1453+{-# DEPRECATED flattenArrays "Please use \"unfoldMany reader\" instead." #-}
1454+flattenArrays :: forall m a. (MonadIO m, Unbox a)
1455+ => D.Stream m (MutArray a) -> D.Stream m a
1456+flattenArrays = concatChunks
1457+
1448-- | Use the "readerRev" unfold instead.
1458-- | Use the "readerRev" unfold instead.
1449--
1459--
1450−-- @flattenArrays = unfoldMany readerRev@
1460+-- @concatChunks = unfoldMany readerRev@
1451--
1461--
1452-- We can try this if there are any fusion issues in the unfold.
1462-- We can try this if there are any fusion issues in the unfold.
1453--
1463--
1454−{-# INLINE_NORMAL flattenArraysRev #-}
1464+{-# INLINE_NORMAL concatChunksRev #-}
1455−flattenArraysRev :: forall m a. (MonadIO m, Unbox a)
1465+concatChunksRev :: forall m a. (MonadIO m, Unbox a)
1456 => D.Stream m (MutArray a) -> D.Stream m a
1466 => D.Stream m (MutArray a) -> D.Stream m a
1457−flattenArraysRev (D.Stream step state) = D.Stream step' (OuterLoop state)
1467+concatChunksRev (D.Stream step state) = D.Stream step' (OuterLoop state)
1458
1468
1459 where
1469 where
1460
1470
@@ -1476,6 +1486,11 @@ flattenArraysRev (D.Stream step state) = D.Stream step' (OuterLoop state)
1476 let cur = INDEX_PREV(p,a)
1486 let cur = INDEX_PREV(p,a)
1477 return $ D.Yield x (InnerLoop st contents cur start)
1487 return $ D.Yield x (InnerLoop st contents cur start)
1478
1488
1489+{-# DEPRECATED flattenArraysRev "Please use \"unfoldMany readerRev\" instead." #-}
1490+flattenArraysRev :: forall m a. (MonadIO m, Unbox a)
1491+ => D.Stream m (MutArray a) -> D.Stream m a
1492+flattenArraysRev = concatChunksRev
1493+
1479-------------------------------------------------------------------------------
1494-------------------------------------------------------------------------------
1480-- Unfolds
1495-- Unfolds
1481-------------------------------------------------------------------------------
1496-------------------------------------------------------------------------------
@@ -1915,7 +1930,7 @@ pinnedWriteNAligned align = writeNWith (pinnedNewAligned align)
1915
1930
1916-- | Buffer a stream into a stream of arrays.
1931-- | Buffer a stream into a stream of arrays.
1917--
1932--
1918−-- >>> writeChunks n = Fold.many (MutArray.writeN n) Fold.toStreamK
1933+-- >>> buildChunks n = Fold.many (MutArray.writeN n) Fold.toStreamK
1919--
1934--
1920-- Breaking an array into an array stream can be useful to consume a large
1935-- Breaking an array into an array stream can be useful to consume a large
1921-- array sequentially such that memory of the array is released incrementatlly.
1936-- array sequentially such that memory of the array is released incrementatlly.
@@ -1924,10 +1939,15 @@ pinnedWriteNAligned align = writeNWith (pinnedNewAligned align)
1924--
1939--
1925-- /Unimplemented/
1940-- /Unimplemented/
1926--
1941--
1927−{-# INLINE_NORMAL writeChunks #-}
1942+{-# INLINE_NORMAL buildChunks #-}
1943+buildChunks :: (MonadIO m, Unbox a) =>
1944+ Int -> Fold m a (StreamK n (MutArray a))
1945+buildChunks n = FL.many (writeN n) FL.toStreamK
1946+
1947+{-# DEPRECATED writeChunks "Please use buildChunks instead." #-}
1928writeChunks :: (MonadIO m, Unbox a) =>
1948writeChunks :: (MonadIO m, Unbox a) =>
1929 Int -> Fold m a (StreamK n (MutArray a))
1949 Int -> Fold m a (StreamK n (MutArray a))
1930−writeChunks n = FL.many (writeN n) FL.toStreamK
1950+writeChunks = buildChunks
1931
1951
1932{-# INLINE_NORMAL writeWithAs #-}
1952{-# INLINE_NORMAL writeWithAs #-}
1933writeWithAs :: forall m a. (MonadIO m, Unbox a)
1953writeWithAs :: forall m a. (MonadIO m, Unbox a)
@@ -1953,7 +1973,7 @@ writeWithAs ps elemCount =
1953 extract = liftIO . rightSize
1973 extract = liftIO . rightSize
1954
1974
1955-- XXX Compare writeWith with fromStreamD which uses an array of streams
1975-- XXX Compare writeWith with fromStreamD which uses an array of streams
1956−-- implementation. We can write this using writeChunks above if that is faster.
1976+-- implementation. We can write this using buildChunks above if that is faster.
1957-- If writeWith is faster then we should use that to implement
1977-- If writeWith is faster then we should use that to implement
1958-- fromStreamD.
1978-- fromStreamD.
1959--
1979--
@@ -1977,7 +1997,7 @@ writeWithAs ps elemCount =
1977--
1997--
1978-- >>> f n = MutArray.writeAppendWith (* 2) (MutArray.new n)
1998-- >>> f n = MutArray.writeAppendWith (* 2) (MutArray.new n)
1979-- >>> writeWith n = Fold.rmapM MutArray.rightSize (f n)
1999-- >>> writeWith n = Fold.rmapM MutArray.rightSize (f n)
1980−-- >>> writeWith n = Fold.rmapM MutArray.fromArrayStreamK (MutArray.writeChunks n)
2000+-- >>> writeWith n = Fold.rmapM MutArray.fromArrayStreamK (MutArray.buildChunks n)
1981--
2001--
1982-- /Pre-release/
2002-- /Pre-release/
1983{-# INLINE_NORMAL writeWith #-}
2003{-# INLINE_NORMAL writeWith #-}
core/src/Streamly/Internal/Data/Stream/Transform.hs+1−1
@@ -1537,7 +1537,7 @@ reverse m = Stream step Nothing
1537{-# INLINE reverseUnbox #-}
1537{-# INLINE reverseUnbox #-}
1538reverseUnbox :: (MonadIO m, Unbox a) => Stream m a -> Stream m a
1538reverseUnbox :: (MonadIO m, Unbox a) => Stream m a -> Stream m a
1539reverseUnbox =
1539reverseUnbox =
1540− A.flattenArraysRev -- unfoldMany A.readRev
1540+ A.concatChunksRev -- unfoldMany A.readerRev
1541 . fromStreamK
1541 . fromStreamK
1542 . K.reverse
1542 . K.reverse
1543 . toStreamK
1543 . toStreamK
src/Streamly/Internal/Data/Stream/IsStream/Common.hs+1−1
@@ -529,7 +529,7 @@ reverse' :: (IsStream t, MonadIO m, Unbox a) => t m a -> t m a
529-- reverse' s = fromStreamD $ D.reverse' $ toStreamD s
529-- reverse' s = fromStreamD $ D.reverse' $ toStreamD s
530reverse' =
530reverse' =
531 fromStreamD
531 fromStreamD
532− . A.flattenArraysRev -- unfoldMany A.readRev
532+ . A.concatChunksRev -- unfoldMany A.readerRev
533 . D.fromStreamK
533 . D.fromStreamK
534 . K.reverse
534 . K.reverse
535 . D.toStreamK
535 . D.toStreamK