on master help sign in
Use spliceCopy instead of splice for immutable arrays
73c112dHarendra Kumar
core/src/Streamly/Internal/Data/Array/Type.hs+20−2
@@ -44,6 +44,7 @@ module Streamly.Internal.Data.Array.Type
44 , fromByteStr#
44 , fromByteStr#
45 , fromByteStr
45 , fromByteStr
46 , fromPtrN
46 , fromPtrN
47+ , fromArrayStreamK
47
48
48 -- ** Split
49 -- ** Split
49 , breakOn
50 , breakOn
@@ -117,6 +118,7 @@ import GHC.Ptr (Ptr(..))
117import Streamly.Internal.Data.MutArray.Type (MutArray(..), MutByteArray)
118import Streamly.Internal.Data.MutArray.Type (MutArray(..), MutByteArray)
118import Streamly.Internal.Data.Fold.Type (Fold(..))
119import Streamly.Internal.Data.Fold.Type (Fold(..))
119import Streamly.Internal.Data.Stream.Type (Stream)
120import Streamly.Internal.Data.Stream.Type (Stream)
121+import Streamly.Internal.Data.StreamK.Type (StreamK)
120import Streamly.Internal.Data.Unbox (Unbox(..))
122import Streamly.Internal.Data.Unbox (Unbox(..))
121import Streamly.Internal.Data.Unfold.Type (Unfold(..))
123import Streamly.Internal.Data.Unfold.Type (Unfold(..))
122import Text.Read (readPrec)
124import Text.Read (readPrec)
@@ -262,9 +264,9 @@ isPinned = MA.isPinned . unsafeThaw
262-- would make a copy on every splice operation, instead use the
264-- would make a copy on every splice operation, instead use the
263-- 'fromArrayStreamK' operation to combine n immutable arrays.
265-- 'fromArrayStreamK' operation to combine n immutable arrays.
264{-# INLINE splice #-}
266{-# INLINE splice #-}
265−splice :: (MonadIO m, Unbox a) => Array a -> Array a -> m (Array a)
267+splice :: MonadIO m => Array a -> Array a -> m (Array a)
266splice arr1 arr2 =
268splice arr1 arr2 =
267− unsafeFreeze <$> MA.splice (unsafeThaw arr1) (unsafeThaw arr2)
269+ unsafeFreeze <$> MA.spliceCopy (unsafeThaw arr1) (unsafeThaw arr2)
268
270
269-- | Create an 'Array' from the first N elements of a list. The array is
271-- | Create an 'Array' from the first N elements of a list. The array is
270-- allocated to size N, if the list terminates before N elements then the
272-- allocated to size N, if the list terminates before N elements then the
@@ -663,6 +665,16 @@ fromByteStr# addr = unsafePerformIO $ fmap unsafeFreeze (MA.fromByteStr# addr)
663fromByteStr :: Ptr Word8 -> Array Word8
665fromByteStr :: Ptr Word8 -> Array Word8
664fromByteStr (Ptr addr#) = fromByteStr# addr#
666fromByteStr (Ptr addr#) = fromByteStr# addr#
665
667
668+-- XXX implement fromArrayStream/fromArrayList instead?
669+
670+-- | Convert an array stream to an array. Note that this requires peak memory
671+-- that is double the size of the array stream.
672+--
673+{-# INLINE fromArrayStreamK #-}
674+fromArrayStreamK :: (Unbox a, MonadIO m) => StreamK m (Array a) -> m (Array a)
675+fromArrayStreamK stream =
676+ fmap unsafeFreeze $ MA.fromArrayStreamK $ fmap unsafeThaw stream
677+
666-------------------------------------------------------------------------------
678-------------------------------------------------------------------------------
667-- Instances
679-- Instances
668-------------------------------------------------------------------------------
680-------------------------------------------------------------------------------
@@ -815,6 +827,12 @@ instance Foldable Array where
815-- Semigroup and Monoid
827-- Semigroup and Monoid
816-------------------------------------------------------------------------------
828-------------------------------------------------------------------------------
817
829
830+-- XXX Deprecate and remove the Semigroup and Monoid instances because of
831+-- potential misuse chances.
832+
833+-- | This should not be used for combining many or N arrays as it would copy
834+-- the two arrays everytime to a new array. For coalescing multiple arrays use
835+-- 'fromArrayStreamK' instead.
818instance Unbox a => Semigroup (Array a) where
836instance Unbox a => Semigroup (Array a) where
819 arr1 <> arr2 = unsafePerformIO $ splice arr1 arr2
837 arr1 <> arr2 = unsafePerformIO $ splice arr1 arr2
820
838
core/src/Streamly/Internal/Data/MutArray/Type.hs+12−4
@@ -2087,21 +2087,29 @@ arrayStreamKLength as = K.foldl' (+) 0 (K.map length as)
2087-- | Convert an array stream to an array. Note that this requires peak memory
2087-- | Convert an array stream to an array. Note that this requires peak memory
2088-- that is double the size of the array stream.
2088-- that is double the size of the array stream.
2089--
2089--
2090−{-# INLINE fromArrayStreamK #-}
2090+{-# INLINE fromArrayStreamKAs #-}
2091−fromArrayStreamK :: (Unbox a, MonadIO m) =>
2091+fromArrayStreamKAs :: (Unbox a, MonadIO m) =>
2092 PinnedState -> StreamK m (MutArray a) -> m (MutArray a)
2092 PinnedState -> StreamK m (MutArray a) -> m (MutArray a)
2093−fromArrayStreamK ps as = do
2093+fromArrayStreamKAs ps as = do
2094 len <- arrayStreamKLength as
2094 len <- arrayStreamKLength as
2095 arr <- newAs ps len
2095 arr <- newAs ps len
2096 -- XXX is StreamK fold faster or StreamD fold?
2096 -- XXX is StreamK fold faster or StreamD fold?
2097 K.foldlM' spliceUnsafe (pure arr) as
2097 K.foldlM' spliceUnsafe (pure arr) as
2098 -- fromStreamDN len $ D.unfoldMany reader $ D.fromStreamK as
2098 -- fromStreamDN len $ D.unfoldMany reader $ D.fromStreamK as
2099
2099
2100+-- | Convert an array stream to an array. Note that this requires peak memory
2101+-- that is double the size of the array stream.
2102+--
2103+{-# INLINE fromArrayStreamK #-}
2104+fromArrayStreamK :: (Unbox a, MonadIO m) =>
2105+ StreamK m (MutArray a) -> m (MutArray a)
2106+fromArrayStreamK = fromArrayStreamKAs Unpinned
2107+
2100{-# INLINE fromStreamDAs #-}
2108{-# INLINE fromStreamDAs #-}
2101fromStreamDAs ::
2109fromStreamDAs ::
2102 (MonadIO m, Unbox a) => PinnedState -> D.Stream m a -> m (MutArray a)
2110 (MonadIO m, Unbox a) => PinnedState -> D.Stream m a -> m (MutArray a)
2103fromStreamDAs ps m =
2111fromStreamDAs ps m =
2104− arrayStreamKFromStreamDAs Unpinned m >>= fromArrayStreamK ps
2112+ arrayStreamKFromStreamDAs Unpinned m >>= fromArrayStreamKAs ps
2105
2113
2106-- CAUTION: a very large number (millions) of arrays can degrade performance
2114-- CAUTION: a very large number (millions) of arrays can degrade performance
2107-- due to GC overhead because we need to buffer the arrays before we flatten
2115-- due to GC overhead because we need to buffer the arrays before we flatten