on master help sign in
Rename toArrayMinChunk to writeWith
commit3b827052b09dfab0d0c2e669be7512df61953983 authorHarendra Kumar <harendra@composewell.com> dateSun 10 Oct 2021, 20:26 (5 years ago) parent0d6a0aa
3 files changed+19−30
src/Streamly/Internal/Data/Array/Foreign.hs+3−2 src/Streamly/Internal/Data/Array/Foreign/Mut/Type.hs+10−10 src/Streamly/Internal/Data/Array/Foreign/Type.hs+6−18
src/Streamly/Internal/Data/Array/Foreign.hs+3−2
@@ -456,7 +456,8 @@ runPipe :: (MonadIO m, Storable a, Storable b)
456456runPipe f arr = P.runPipe (toArrayMinChunk (length arr)) $ f (A.read arr)
457457-}
458458
459−-- XXX Use byteLength instead of length.
459+-- XXX For transformations that cannot change the number of elements e.g. "map"
460+-- we can use a predetermined array length.
460461--
461462-- | Transform an array into another array using a stream transformation
462463-- operation.
@@ -466,7 +467,7 @@ runPipe f arr = P.runPipe (toArrayMinChunk (length arr)) $ f (A.read arr)
466467streamTransform :: forall m a b. (MonadIO m, Storable a, Storable b)
467468 => (SerialT m a -> SerialT m b) -> Array a -> m (Array b)
468469streamTransform f arr =
469− P.fold (A.toArrayMinChunk (alignment (undefined :: a)) (length arr))
470+ P.fold (A.writeWith (alignment (undefined :: a)) (length arr))
470471 $ getSerialT $ f (A.toStream arr)
471472
472473-------------------------------------------------------------------------------
src/Streamly/Internal/Data/Array/Foreign/Mut/Type.hs+10−10
@@ -56,7 +56,7 @@ module Streamly.Internal.Data.Array.Foreign.Mut.Type
5656 , cmp
5757
5858 -- * Composable Folds
59− , toArrayMinChunk
59+ , writeWith
6060 , writeNAllocWith
6161 , writeN
6262 , writeNUnsafe
@@ -927,9 +927,9 @@ writeChunks :: (MonadIO m, Storable a) =>
927927 Int -> Fold m a (K.Stream n (Array a))
928928writeChunks n = FL.many (writeN n) FL.toStreamK
929929
930−-- XXX Compare toArrayMinChunk with fromStreamD which uses an array of streams
930+-- XXX Compare writeWith with fromStreamD which uses an array of streams
931931-- implementation. We can write this using writeChunks above if that is faster.
932−-- If toArrayMinChunk is faster then we should use that to implement
932+-- If writeWith is faster then we should use that to implement
933933-- fromStreamD.
934934--
935935-- XXX The realloc based implementation needs to make one extra copy if we use
@@ -944,11 +944,11 @@ writeChunks n = FL.many (writeN n) FL.toStreamK
944944-- XXX check if GHC's memory allocator is efficient enough. We can try the C
945945-- malloc to compare against.
946946
947−{-# INLINE_NORMAL toArrayMinChunk #-}
948−toArrayMinChunk :: forall m a. (MonadIO m, Storable a)
947+{-# INLINE_NORMAL writeWith #-}
948+writeWith :: forall m a. (MonadIO m, Storable a)
949949 => Int -> Int -> Fold m a (Array a)
950−-- toArrayMinChunk n = FL.rmapM spliceArrays $ toArraysOf n
951−toArrayMinChunk alignSize elemCount =
950+-- writeWith n = FL.rmapM spliceArrays $ toArraysOf n
951+writeWith alignSize elemCount =
952952 FL.rmapM extract $ FL.foldlM' step initial
953953
954954 where
@@ -958,7 +958,7 @@ toArrayMinChunk alignSize elemCount =
958958 return $ Array start (end `plusPtr` sizeOf (undefined :: a)) bound
959959
960960 initial = do
961− when (elemCount < 0) $ error "toArrayMinChunk: elemCount is negative"
961+ when (elemCount < 0) $ error "writeWith: elemCount is negative"
962962 liftIO $ newArrayAligned alignSize elemCount
963963 step arr@(Array start end bound) x
964964 | end `plusPtr` sizeOf (undefined :: a) > bound = do
@@ -980,7 +980,7 @@ toArrayMinChunk alignSize elemCount =
980980-- @since 0.7.0
981981{-# INLINE write #-}
982982write :: forall m a. (MonadIO m, Storable a) => Fold m a (Array a)
983−write = toArrayMinChunk (alignment (undefined :: a))
983+write = writeWith (alignment (undefined :: a))
984984 (bytesToElemCount (undefined :: a)
985985 (mkChunkSize 1024))
986986
@@ -995,7 +995,7 @@ write = toArrayMinChunk (alignment (undefined :: a))
995995writeAligned :: forall m a. (MonadIO m, Storable a)
996996 => Int -> Fold m a (Array a)
997997writeAligned alignSize =
998− toArrayMinChunk alignSize
998+ writeWith alignSize
999999 (bytesToElemCount (undefined :: a)
10001000 (mkChunkSize 1024))
10011001
src/Streamly/Internal/Data/Array/Foreign/Type.hs+6−18
@@ -57,7 +57,7 @@ module Streamly.Internal.Data.Array.Foreign.Type
5757 , toList
5858
5959 -- * Folds
60− , toArrayMinChunk
60+ , writeWith
6161 , writeN
6262 , writeNUnsafe
6363 , MA.ArrayUnsafe (..)
@@ -550,24 +550,12 @@ writeNUnsafe :: forall m a. (MonadIO m, Storable a)
550550 => Int -> Fold m a (Array a)
551551writeNUnsafe n = unsafeFreeze <$> MA.writeNUnsafe n
552552
553−-- XXX The realloc based implementation needs to make one extra copy if we use
554−-- shrinkToFit. On the other hand, the stream of arrays implementation may
555−-- buffer the array chunk pointers in memory but it does not have to shrink as
556−-- we know the exact size in the end. However, memory copying does not seems to
557−-- be as expensive as the allocations. Therefore, we need to reduce the number
558−-- of allocations instead. Also, the size of allocations matters, right sizing
559−-- an allocation even at the cost of copying sems to help. Should be measured
560−-- on a big stream with heavy calls to toArray to see the effect.
561−--
562−-- XXX check if GHC's memory allocator is efficient enough. We can try the C
563−-- malloc to compare against.
564−
565−{-# INLINE_NORMAL toArrayMinChunk #-}
566−toArrayMinChunk :: forall m a. (MonadIO m, Storable a)
553+{-# INLINE_NORMAL writeWith #-}
554+writeWith :: forall m a. (MonadIO m, Storable a)
567555 => Int -> Int -> Fold m a (Array a)
568−-- toArrayMinChunk n = FL.rmapM spliceArrays $ toArraysOf n
569−toArrayMinChunk alignSize elemCount =
570− unsafeFreeze <$> MA.toArrayMinChunk alignSize elemCount
556+-- writeWith n = FL.rmapM spliceArrays $ toArraysOf n
557+writeWith alignSize elemCount =
558+ unsafeFreeze <$> MA.writeWith alignSize elemCount
571559
572560-- | Fold the whole input to a single array.
573561--