@@ -77,14 +77,11 @@ import Prelude hiding (null, last, (!!), read, concat, unlines)
78import Streamly.Data.Fold (Fold)
78import Streamly.Data.Fold (Fold)
79import Streamly.Internal.Data.Array.Type (Array(..))
79import Streamly.Internal.Data.Array.Type (Array(..))
80−import Streamly.Internal.Data.Array.Mut.Type (MutArray)
81import Streamly.Internal.Data.Fold.Chunked (ChunkFold(..))
80import Streamly.Internal.Data.Fold.Chunked (ChunkFold(..))
82import Streamly.Internal.Data.Parser (ParseError(..))
81import Streamly.Internal.Data.Parser (ParseError(..))
83import Streamly.Internal.Data.Stream (Stream)
82import Streamly.Internal.Data.Stream (Stream)
84import Streamly.Internal.Data.StreamK (StreamK, fromStream, toStream)
83import Streamly.Internal.Data.StreamK (StreamK, fromStream, toStream)
85import Streamly.Internal.Data.SVar.Type (adaptState, defState)
84import Streamly.Internal.Data.SVar.Type (adaptState, defState)
86−import Streamly.Internal.Data.Array.Mut.Type
87− (allocBytesToElemCount)
88import Streamly.Internal.Data.Tuple.Strict (Tuple'(..))
85import Streamly.Internal.Data.Tuple.Strict (Tuple'(..))
90import qualified Streamly.Data.Fold as FL
87import qualified Streamly.Data.Fold as FL
@@ -467,6 +464,8 @@ splitAtArrayListRev n ls
467-- Fold to a single Array
464-- Fold to a single Array
468-------------------------------------------------------------------------------
465-------------------------------------------------------------------------------
470-- XXX Both of these implementations of splicing seem to perform equally well.
469-- XXX Both of these implementations of splicing seem to perform equally well.
471-- We need to perform benchmarks over a range of sizes though.
470-- We need to perform benchmarks over a range of sizes though.
@@ -476,7 +475,9 @@ splitAtArrayListRev n ls
476spliceArraysLenUnsafe :: (MonadIO m, Unbox a)
475spliceArraysLenUnsafe :: (MonadIO m, Unbox a)
477 => Int -> Stream m (MutArray a) -> m (MutArray a)
476 => Int -> Stream m (MutArray a) -> m (MutArray a)
478spliceArraysLenUnsafe len buffered = do
477spliceArraysLenUnsafe len buffered = do
479− arr <- liftIO $ MA.pinnedNew len
478+ -- XXX The new array's pinned state should depend on the first element
479+ -- of the stream. We should uncons the stream and do the required.
480 D.foldlM' MA.spliceUnsafe (return arr) buffered
481 D.foldlM' MA.spliceUnsafe (return arr) buffered
482{-# INLINE _spliceArrays #-}
483{-# INLINE _spliceArrays #-}
@@ -485,7 +486,9 @@ _spliceArrays :: (MonadIO m, Unbox a)
485_spliceArrays s = do
486_spliceArrays s = do
486 buffered <- D.foldr K.cons K.nil s
487 buffered <- D.foldr K.cons K.nil s
487 len <- K.fold FL.sum (fmap Array.length buffered)
488 len <- K.fold FL.sum (fmap Array.length buffered)
488− arr <- liftIO $ MA.pinnedNew len
489+ -- XXX The new array's pinned state should depend on the first element
490+ -- of the stream. We should uncons the stream and do the required.
489 final <- D.foldlM' writeArr (return arr) (toStream buffered)
492 final <- D.foldlM' writeArr (return arr) (toStream buffered)
490 return $ A.unsafeFreeze final
493 return $ A.unsafeFreeze final
@@ -502,15 +505,22 @@ _spliceArraysBuffered s = do
502 A.unsafeFreeze <$>
505 A.unsafeFreeze <$>
503 spliceArraysLenUnsafe len (fmap A.unsafeThaw (toStream buffered))
506 spliceArraysLenUnsafe len (fmap A.unsafeThaw (toStream buffered))
505{-# INLINE spliceArraysRealloced #-}
510{-# INLINE spliceArraysRealloced #-}
506spliceArraysRealloced :: forall m a. (MonadIO m, Unbox a)
511spliceArraysRealloced :: forall m a. (MonadIO m, Unbox a)
507 => Stream m (Array a) -> m (Array a)
512 => Stream m (Array a) -> m (Array a)
508spliceArraysRealloced s = do
513spliceArraysRealloced s = do
509− let n = allocBytesToElemCount (undefined :: a) (4 * 1024)
514+ res <- D.uncons s
510− idst = liftIO $ MA.pinnedNew n
515+ case res of
511−
516+ Just (a, strm) -> do
520+ (pure (A.unsafeThaw a))
512− arr <- D.foldlM' MA.spliceExp idst (fmap A.unsafeThaw s)
521+ (fmap A.unsafeThaw strm)
513− liftIO $ A.unsafeFreeze <$> MA.rightSize arr
522+ liftIO $ A.unsafeFreeze <$> MA.rightSize arr
523+ Nothing -> pure A.nil
515-- XXX This should just be "fold A.write"
525-- XXX This should just be "fold A.write"