@@ -77,14 +77,11 @@ import Prelude hiding (null, last, (!!), read, concat, unlines)
7777
7878import Streamly.Data.Fold (Fold)
7979import Streamly.Internal.Data.Array.Type (Array(..))
80−import Streamly.Internal.Data.Array.Mut.Type (MutArray)
8180import Streamly.Internal.Data.Fold.Chunked (ChunkFold(..))
8281import Streamly.Internal.Data.Parser (ParseError(..))
8382import Streamly.Internal.Data.Stream (Stream)
8483import Streamly.Internal.Data.StreamK (StreamK, fromStream, toStream)
8584import Streamly.Internal.Data.SVar.Type (adaptState, defState)
86−import Streamly.Internal.Data.Array.Mut.Type
87− (allocBytesToElemCount)
8885import Streamly.Internal.Data.Tuple.Strict (Tuple'(..))
8986
9087import qualified Streamly.Data.Fold as FL
@@ -467,6 +464,8 @@ splitAtArrayListRev n ls
467464-- Fold to a single Array
468465-------------------------------------------------------------------------------
469466
467+{-
468+
470469-- XXX Both of these implementations of splicing seem to perform equally well.
471470-- We need to perform benchmarks over a range of sizes though.
472471
@@ -476,7 +475,9 @@ splitAtArrayListRev n ls
476475spliceArraysLenUnsafe :: (MonadIO m, Unbox a)
477476 => Int -> Stream m (MutArray a) -> m (MutArray a)
478477spliceArraysLenUnsafe 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+ arr <- undefined
480481 D.foldlM' MA.spliceUnsafe (return arr) buffered
481482
482483{-# INLINE _spliceArrays #-}
@@ -485,7 +486,9 @@ _spliceArrays :: (MonadIO m, Unbox a)
485486_spliceArrays s = do
486487 buffered <- D.foldr K.cons K.nil s
487488 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.
491+ arr <- undefined
489492 final <- D.foldlM' writeArr (return arr) (toStream buffered)
490493 return $ A.unsafeFreeze final
491494
@@ -502,15 +505,22 @@ _spliceArraysBuffered s = do
502505 A.unsafeFreeze <$>
503506 spliceArraysLenUnsafe len (fmap A.unsafeThaw (toStream buffered))
504507
508+-}
509+
505510{-# INLINE spliceArraysRealloced #-}
506511spliceArraysRealloced :: forall m a. (MonadIO m, Unbox a)
507512 => Stream m (Array a) -> m (Array a)
508513spliceArraysRealloced s = do
509− let n = allocBytesToElemCount (undefined :: a) (4 * 1024)
510− idst = liftIO $ MA.pinnedNew n
511−
512− arr <- D.foldlM' MA.spliceExp idst (fmap A.unsafeThaw s)
513− liftIO $ A.unsafeFreeze <$> MA.rightSize arr
514+ res <- D.uncons s
515+ case res of
516+ Just (a, strm) -> do
517+ arr <-
518+ D.foldlM'
519+ MA.spliceExp
520+ (pure (A.unsafeThaw a))
521+ (fmap A.unsafeThaw strm)
522+ liftIO $ A.unsafeFreeze <$> MA.rightSize arr
523+ Nothing -> pure A.nil
514524
515525-- XXX This should just be "fold A.write"
516526--