on master help sign in
Rename bufferChunks, writeChunks, flattenArrays
commitff40cf0e7ac99f5a866809108db3a2134d7d410a authorHarendra Kumar <harendra@composewell.com> dateSat 30 Dec 2023, 05:56 (3 years ago) parent191abeb
4 files changed+69−31
core/src/Streamly/Internal/Data/Array/Type.hs+31−13 core/src/Streamly/Internal/Data/MutArray/Type.hs+36−16 core/src/Streamly/Internal/Data/Stream/Transform.hs+1−1 src/Streamly/Internal/Data/Stream/IsStream/Common.hs+1−1
core/src/Streamly/Internal/Data/Array/Type.hs+31−13
@@ -91,12 +91,15 @@ module Streamly.Internal.Data.Array.Type
9191 -- ** Streams of arrays
9292 , chunksOf
9393 , pinnedChunksOf
94− , bufferChunks
95− , flattenArrays
96− , flattenArraysRev
94+ , buildChunks
95+ , concatChunks
96+ , concatChunksRev
9797
9898 -- ** Deprecated
9999 , unsafeIndex
100+ , bufferChunks
101+ , flattenArrays
102+ , flattenArraysRev
100103 )
101104where
102105
@@ -348,10 +351,15 @@ pinnedClone = fmap unsafeFreeze . MA.pinnedClone . unsafeThaw
348351-- Streams of arrays
349352-------------------------------------------------------------------------------
350353
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." #-}
352360bufferChunks :: (MonadIO m, Unbox a) =>
353361 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
355363
356364-- | @chunksOf n stream@ groups the elements in the input stream into arrays of
357365-- @n@ elements each.
@@ -372,27 +380,37 @@ pinnedChunksOf :: forall m a. (MonadIO m, Unbox a)
372380 => Int -> D.Stream m a -> D.Stream m (Array a)
373381pinnedChunksOf n str = D.map unsafeFreeze $ MA.pinnedChunksOf n str
374382
375−-- | Use the "read" unfold instead.
383+-- | Use the "reader" unfold instead.
376384--
377−-- @flattenArrays = unfoldMany read@
385+-- @concatChunks = unfoldMany reader@
378386--
379387-- We can try this if there are any fusion issues in the unfold.
380388--
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." #-}
382395flattenArrays :: forall m a. (MonadIO m, Unbox a)
383396 => D.Stream m (Array a) -> D.Stream m a
384−flattenArrays = MA.flattenArrays . D.map unsafeThaw
397+flattenArrays = concatChunks
385398
386−-- | Use the "readRev" unfold instead.
399+-- | Use the "readerRev" unfold instead.
387400--
388−-- @flattenArrays = unfoldMany readRev@
401+-- @concatChunksRev = unfoldMany readerRev@
389402--
390403-- We can try this if there are any fusion issues in the unfold.
391404--
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." #-}
393411flattenArraysRev :: forall m a. (MonadIO m, Unbox a)
394412 => D.Stream m (Array a) -> D.Stream m a
395−flattenArraysRev = MA.flattenArraysRev . D.map unsafeThaw
413+flattenArraysRev = concatChunksRev
396414
397415-- Drops the separator byte
398416{-# INLINE breakOn #-}
core/src/Streamly/Internal/Data/MutArray/Type.hs+36−16
@@ -197,11 +197,11 @@ module Streamly.Internal.Data.MutArray.Type
197197 -- *** Construct from streams
198198 , chunksOf
199199 , pinnedChunksOf
200− , writeChunks
200+ , buildChunks
201201
202202 -- *** Eliminate to streams
203− , flattenArrays
204− , flattenArraysRev
203+ , concatChunks
204+ , concatChunksRev
205205
206206 -- *** Construct from arrays
207207 -- get chunks without copying
@@ -230,6 +230,11 @@ module Streamly.Internal.Data.MutArray.Type
230230 , memcpy
231231 , memcmp
232232 , c_memchr
233+
234+ -- * Deprecated
235+ , writeChunks
236+ , flattenArrays
237+ , flattenArraysRev
233238 )
234239where
235240
@@ -1418,14 +1423,14 @@ data FlattenState s contents a =
14181423
14191424-- | Use the "reader" unfold instead.
14201425--
1421−-- @flattenArrays = unfoldMany reader@
1426+-- @concatChunks = unfoldMany reader@
14221427--
14231428-- We can try this if there are any fusion issues in the unfold.
14241429--
1425−{-# INLINE_NORMAL flattenArrays #-}
1426−flattenArrays :: forall m a. (MonadIO m, Unbox a)
1430+{-# INLINE_NORMAL concatChunks #-}
1431+concatChunks :: forall m a. (MonadIO m, Unbox a)
14271432 => 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)
14291434
14301435 where
14311436
@@ -1445,16 +1450,21 @@ flattenArrays (D.Stream step state) = D.Stream step' (OuterLoop state)
14451450 x <- liftIO $ peekAt p contents
14461451 return $ D.Yield x (InnerLoop st contents (INDEX_NEXT(p,a)) end)
14471452
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+
14481458-- | Use the "readerRev" unfold instead.
14491459--
1450−-- @flattenArrays = unfoldMany readerRev@
1460+-- @concatChunks = unfoldMany readerRev@
14511461--
14521462-- We can try this if there are any fusion issues in the unfold.
14531463--
1454−{-# INLINE_NORMAL flattenArraysRev #-}
1455−flattenArraysRev :: forall m a. (MonadIO m, Unbox a)
1464+{-# INLINE_NORMAL concatChunksRev #-}
1465+concatChunksRev :: forall m a. (MonadIO m, Unbox a)
14561466 => 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)
14581468
14591469 where
14601470
@@ -1476,6 +1486,11 @@ flattenArraysRev (D.Stream step state) = D.Stream step' (OuterLoop state)
14761486 let cur = INDEX_PREV(p,a)
14771487 return $ D.Yield x (InnerLoop st contents cur start)
14781488
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+
14791494-------------------------------------------------------------------------------
14801495-- Unfolds
14811496-------------------------------------------------------------------------------
@@ -1915,7 +1930,7 @@ pinnedWriteNAligned align = writeNWith (pinnedNewAligned align)
19151930
19161931-- | Buffer a stream into a stream of arrays.
19171932--
1918−-- >>> writeChunks n = Fold.many (MutArray.writeN n) Fold.toStreamK
1933+-- >>> buildChunks n = Fold.many (MutArray.writeN n) Fold.toStreamK
19191934--
19201935-- Breaking an array into an array stream can be useful to consume a large
19211936-- array sequentially such that memory of the array is released incrementatlly.
@@ -1924,10 +1939,15 @@ pinnedWriteNAligned align = writeNWith (pinnedNewAligned align)
19241939--
19251940-- /Unimplemented/
19261941--
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." #-}
19281948writeChunks :: (MonadIO m, Unbox a) =>
19291949 Int -> Fold m a (StreamK n (MutArray a))
1930−writeChunks n = FL.many (writeN n) FL.toStreamK
1950+writeChunks = buildChunks
19311951
19321952{-# INLINE_NORMAL writeWithAs #-}
19331953writeWithAs :: forall m a. (MonadIO m, Unbox a)
@@ -1953,7 +1973,7 @@ writeWithAs ps elemCount =
19531973 extract = liftIO . rightSize
19541974
19551975-- 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.
19571977-- If writeWith is faster then we should use that to implement
19581978-- fromStreamD.
19591979--
@@ -1977,7 +1997,7 @@ writeWithAs ps elemCount =
19771997--
19781998-- >>> f n = MutArray.writeAppendWith (* 2) (MutArray.new n)
19791999-- >>> 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)
19812001--
19822002-- /Pre-release/
19832003{-# INLINE_NORMAL writeWith #-}
core/src/Streamly/Internal/Data/Stream/Transform.hs+1−1
@@ -1537,7 +1537,7 @@ reverse m = Stream step Nothing
15371537{-# INLINE reverseUnbox #-}
15381538reverseUnbox :: (MonadIO m, Unbox a) => Stream m a -> Stream m a
15391539reverseUnbox =
1540− A.flattenArraysRev -- unfoldMany A.readRev
1540+ A.concatChunksRev -- unfoldMany A.readerRev
15411541 . fromStreamK
15421542 . K.reverse
15431543 . 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
529529-- reverse' s = fromStreamD $ D.reverse' $ toStreamD s
530530reverse' =
531531 fromStreamD
532− . A.flattenArraysRev -- unfoldMany A.readRev
532+ . A.concatChunksRev -- unfoldMany A.readerRev
533533 . D.fromStreamK
534534 . K.reverse
535535 . D.toStreamK