on master help sign in
Rename toArrayMinChunk to writeWith
3b82705Harendra Kumar
src/Streamly/Internal/Data/Array/Foreign.hs+3−2
@@ -456,7 +456,8 @@ runPipe :: (MonadIO m, Storable a, Storable b)
456runPipe f arr = P.runPipe (toArrayMinChunk (length arr)) $ f (A.read arr)
456runPipe f arr = P.runPipe (toArrayMinChunk (length arr)) $ f (A.read arr)
457-}
457-}
458
458
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.
460--
461--
461-- | Transform an array into another array using a stream transformation
462-- | Transform an array into another array using a stream transformation
462-- operation.
463-- operation.
@@ -466,7 +467,7 @@ runPipe f arr = P.runPipe (toArrayMinChunk (length arr)) $ f (A.read arr)
466streamTransform :: forall m a b. (MonadIO m, Storable a, Storable b)
467streamTransform :: forall m a b. (MonadIO m, Storable a, Storable b)
467 => (SerialT m a -> SerialT m b) -> Array a -> m (Array b)
468 => (SerialT m a -> SerialT m b) -> Array a -> m (Array b)
468streamTransform f arr =
469streamTransform f arr =
469− P.fold (A.toArrayMinChunk (alignment (undefined :: a)) (length arr))
470+ P.fold (A.writeWith (alignment (undefined :: a)) (length arr))
470 $ getSerialT $ f (A.toStream arr)
471 $ getSerialT $ f (A.toStream arr)
471
472
472-------------------------------------------------------------------------------
473-------------------------------------------------------------------------------
src/Streamly/Internal/Data/Array/Foreign/Mut/Type.hs+10−10
@@ -56,7 +56,7 @@ module Streamly.Internal.Data.Array.Foreign.Mut.Type
56 , cmp
56 , cmp
57
57
58 -- * Composable Folds
58 -- * Composable Folds
59− , toArrayMinChunk
59+ , writeWith
60 , writeNAllocWith
60 , writeNAllocWith
61 , writeN
61 , writeN
62 , writeNUnsafe
62 , writeNUnsafe
@@ -927,9 +927,9 @@ writeChunks :: (MonadIO m, Storable a) =>
927 Int -> Fold m a (K.Stream n (Array a))
927 Int -> Fold m a (K.Stream n (Array a))
928writeChunks n = FL.many (writeN n) FL.toStreamK
928writeChunks n = FL.many (writeN n) FL.toStreamK
929
929
930−-- XXX Compare toArrayMinChunk with fromStreamD which uses an array of streams
930+-- XXX Compare writeWith with fromStreamD which uses an array of streams
931-- implementation. We can write this using writeChunks above if that is faster.
931-- 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
933-- fromStreamD.
933-- fromStreamD.
934--
934--
935-- XXX The realloc based implementation needs to make one extra copy if we use
935-- 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
944-- XXX check if GHC's memory allocator is efficient enough. We can try the C
944-- XXX check if GHC's memory allocator is efficient enough. We can try the C
945-- malloc to compare against.
945-- malloc to compare against.
946
946
947−{-# INLINE_NORMAL toArrayMinChunk #-}
947+{-# INLINE_NORMAL writeWith #-}
948−toArrayMinChunk :: forall m a. (MonadIO m, Storable a)
948+writeWith :: forall m a. (MonadIO m, Storable a)
949 => Int -> Int -> Fold m a (Array a)
949 => Int -> Int -> Fold m a (Array a)
950−-- toArrayMinChunk n = FL.rmapM spliceArrays $ toArraysOf n
950+-- writeWith n = FL.rmapM spliceArrays $ toArraysOf n
951−toArrayMinChunk alignSize elemCount =
951+writeWith alignSize elemCount =
952 FL.rmapM extract $ FL.foldlM' step initial
952 FL.rmapM extract $ FL.foldlM' step initial
953
953
954 where
954 where
@@ -958,7 +958,7 @@ toArrayMinChunk alignSize elemCount =
958 return $ Array start (end `plusPtr` sizeOf (undefined :: a)) bound
958 return $ Array start (end `plusPtr` sizeOf (undefined :: a)) bound
959
959
960 initial = do
960 initial = do
961− when (elemCount < 0) $ error "toArrayMinChunk: elemCount is negative"
961+ when (elemCount < 0) $ error "writeWith: elemCount is negative"
962 liftIO $ newArrayAligned alignSize elemCount
962 liftIO $ newArrayAligned alignSize elemCount
963 step arr@(Array start end bound) x
963 step arr@(Array start end bound) x
964 | end `plusPtr` sizeOf (undefined :: a) > bound = do
964 | end `plusPtr` sizeOf (undefined :: a) > bound = do
@@ -980,7 +980,7 @@ toArrayMinChunk alignSize elemCount =
980-- @since 0.7.0
980-- @since 0.7.0
981{-# INLINE write #-}
981{-# INLINE write #-}
982write :: forall m a. (MonadIO m, Storable a) => Fold m a (Array a)
982write :: forall m a. (MonadIO m, Storable a) => Fold m a (Array a)
983−write = toArrayMinChunk (alignment (undefined :: a))
983+write = writeWith (alignment (undefined :: a))
984 (bytesToElemCount (undefined :: a)
984 (bytesToElemCount (undefined :: a)
985 (mkChunkSize 1024))
985 (mkChunkSize 1024))
986
986
@@ -995,7 +995,7 @@ write = toArrayMinChunk (alignment (undefined :: a))
995writeAligned :: forall m a. (MonadIO m, Storable a)
995writeAligned :: forall m a. (MonadIO m, Storable a)
996 => Int -> Fold m a (Array a)
996 => Int -> Fold m a (Array a)
997writeAligned alignSize =
997writeAligned alignSize =
998− toArrayMinChunk alignSize
998+ writeWith alignSize
999 (bytesToElemCount (undefined :: a)
999 (bytesToElemCount (undefined :: a)
1000 (mkChunkSize 1024))
1000 (mkChunkSize 1024))
1001
1001
src/Streamly/Internal/Data/Array/Foreign/Type.hs+6−18
@@ -57,7 +57,7 @@ module Streamly.Internal.Data.Array.Foreign.Type
57 , toList
57 , toList
58
58
59 -- * Folds
59 -- * Folds
60− , toArrayMinChunk
60+ , writeWith
61 , writeN
61 , writeN
62 , writeNUnsafe
62 , writeNUnsafe
63 , MA.ArrayUnsafe (..)
63 , MA.ArrayUnsafe (..)
@@ -550,24 +550,12 @@ writeNUnsafe :: forall m a. (MonadIO m, Storable a)
550 => Int -> Fold m a (Array a)
550 => Int -> Fold m a (Array a)
551writeNUnsafe n = unsafeFreeze <$> MA.writeNUnsafe n
551writeNUnsafe n = unsafeFreeze <$> MA.writeNUnsafe n
552
552
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 #-}
553+{-# INLINE_NORMAL writeWith #-}
566−toArrayMinChunk :: forall m a. (MonadIO m, Storable a)
554+writeWith :: forall m a. (MonadIO m, Storable a)
567 => Int -> Int -> Fold m a (Array a)
555 => Int -> Int -> Fold m a (Array a)
568−-- toArrayMinChunk n = FL.rmapM spliceArrays $ toArraysOf n
556+-- writeWith n = FL.rmapM spliceArrays $ toArraysOf n
569−toArrayMinChunk alignSize elemCount =
557+writeWith alignSize elemCount =
570− unsafeFreeze <$> MA.toArrayMinChunk alignSize elemCount
558+ unsafeFreeze <$> MA.writeWith alignSize elemCount
571
559
572-- | Fold the whole input to a single array.
560-- | Fold the whole input to a single array.
573--
561--