@@ -1,12 +1,26 @@
1{-# LANGUAGE UnboxedTuples #-}
2{-# LANGUAGE UnboxedTuples #-}
3−-- Module : Streamly.Internal.Data.MutArray.Mut.Type
4+-- Module : Streamly.Internal.Data.Array.Mut.Type
4-- Copyright : (c) 2020 Composewell Technologies
5-- Copyright : (c) 2020 Composewell Technologies
5-- License : BSD3-3-Clause
6-- License : BSD3-3-Clause
6-- Maintainer : streamly@composewell.com
7-- Maintainer : streamly@composewell.com
7-- Stability : experimental
8-- Stability : experimental
8-- Portability : GHC
9-- Portability : GHC
11+-- Pinned and unpinned mutable array for 'Unboxed' types. Fulfils the following
14+-- * Random access (array)
15+-- * Efficient storage (unboxed)
16+-- * Performance (unboxed access)
17+-- * Performance - in-place operations (mutable)
18+-- * Performance - GC (pinned, mutable)
19+-- * interfacing with OS (pinned)
21+-- Stream and Fold APIs allow easy, efficient and convenient operations on
10-- Mutable arrays and file system files are quite similar, they can grow and
24-- Mutable arrays and file system files are quite similar, they can grow and
11-- their content is mutable. Therefore, both have similar APIs as well. We
25-- their content is mutable. Therefore, both have similar APIs as well. We
12-- strive to keep the API consistent for both. Ideally, you should be able to
26-- strive to keep the API consistent for both. Ideally, you should be able to
@@ -205,10 +219,6 @@ module Streamly.Internal.Data.Array.Mut.Type
209−-- When we use a purely lazy Monad like Identity, we need to force ordering of
210−-- some actions for correctness.
212#include "assert.hs"
222#include "assert.hs"
213#include "inline.hs"
223#include "inline.hs"
214#include "ArrayMacros.h"
224#include "ArrayMacros.h"
@@ -259,13 +269,7 @@ import qualified Prelude
259import Prelude hiding
269import Prelude hiding
260 (length, foldr, read, unlines, splitAt, reverse, truncate)
270 (length, foldr, read, unlines, splitAt, reverse, truncate)
262−-- $setup
272+#include "DocTestDataMutArray.hs"
264−-- >>> import qualified Streamly.Internal.Data.Array.Mut.Type as MutArray
265−-- >>> import qualified Streamly.Internal.Data.Stream as Stream
266−-- >>> import qualified Streamly.Internal.Data.Stream.StreamD as StreamD
267−-- >>> import qualified Streamly.Internal.Data.Fold as Fold
268−-- >>> import qualified Streamly.Internal.Data.Fold.Type as Fold
270-------------------------------------------------------------------------------
274-------------------------------------------------------------------------------
271-- Foreign helpers
275-- Foreign helpers
@@ -305,14 +309,12 @@ memcmp p1 p2 len = do
306-- $arrayNotes
310-- $arrayNotes
308−-- We can use an 'Unboxed' constraint in the MutArray type and the constraint can
312+-- We can use an 'Unboxed' constraint in the MutArray type and the constraint
309−-- be automatically provided to a function that pattern matches on the MutArray
313+-- can be automatically provided to a function that pattern matches on the
310−-- type. However, it has huge performance cost, so we do not use it.
314+-- MutArray type. However, it has huge performance cost, so we do not use it.
311-- Investigate a GHC improvement possiblity.
315-- Investigate a GHC improvement possiblity.
313−-- XXX Rename the fields to better names.
315−-- | An unboxed, pinned mutable array. An array is created with a given length
317+-- | An unboxed mutable array. An array is created with a given length
316-- and capacity. Length is the number of valid elements in the array. Capacity
318-- and capacity. Length is the number of valid elements in the array. Capacity
317-- is the maximum number of elements that the array can be expanded to without
319-- is the maximum number of elements that the array can be expanded to without
318-- having to reallocate the memory.
320-- having to reallocate the memory.
@@ -1520,7 +1522,8 @@ toListFB c n MutArray{..} = go arrStart
1521-- XXX Monadic foldr/build fusion?
1523-- XXX Monadic foldr/build fusion?
1522-- Reference: https://www.researchgate.net/publication/220676509_Monadic_augment_and_generalised_short_cut_fusion
1524-- Reference: https://www.researchgate.net/publication/220676509_Monadic_augment_and_generalised_short_cut_fusion
1523−-- | Convert an 'MutArray' into a list.
1526+-- | Convert a 'MutArray' into a list.
1525{-# INLINE toList #-}
1528{-# INLINE toList #-}
1526toList :: forall m a. (MonadIO m, Unbox a) => MutArray a -> m [a]
1529toList :: forall m a. (MonadIO m, Unbox a) => MutArray a -> m [a]
@@ -1813,7 +1816,7 @@ writeRevN :: forall m a. (MonadIO m, Unbox a) => Int -> Fold m a (MutArray a)
1813writeRevN = writeRevNWith newPinned
1816writeRevN = writeRevNWith newPinned
1815-- | @writeNAligned align n@ folds a maximum of @n@ elements from the input
1818-- | @writeNAligned align n@ folds a maximum of @n@ elements from the input
1816−-- stream to an 'MutArray' aligned to the given size.
1819+-- stream to a 'MutArray' aligned to the given size.
1818-- >>> writeNAligned align = MutArray.writeNWith (MutArray.newAlignedPinned align)
1821-- >>> writeNAligned align = MutArray.writeNWith (MutArray.newAlignedPinned align)
1819-- >>> writeNAligned align n = MutArray.writeAppendN n (MutArray.newAlignedPinned align n)
1822-- >>> writeNAligned align n = MutArray.writeAppendN n (MutArray.newAlignedPinned align n)
@@ -1911,7 +1914,7 @@ write = writeWith (allocBytesToElemCount (undefined :: a) arrayChunkBytes)
1912-- | Use the 'writeN' fold instead.
1915-- | Use the 'writeN' fold instead.
1914−-- >>> fromStreamDN n = StreamD.fold (MutArray.writeN n)
1917+-- >>> fromStreamDN n = Stream.fold (MutArray.writeN n)
1916{-# INLINE_NORMAL fromStreamDN #-}
1919{-# INLINE_NORMAL fromStreamDN #-}
1917fromStreamDN :: forall m a. (MonadIO m, Unbox a)
1920fromStreamDN :: forall m a. (MonadIO m, Unbox a)
@@ -1928,7 +1931,7 @@ fromStreamDN limit str = do
1928 liftIO $ pokeWith arrContents ptr x
1931 liftIO $ pokeWith arrContents ptr x
1929 return $ INDEX_NEXT(ptr,a)
1932 return $ INDEX_NEXT(ptr,a)
1931−-- | Create an 'MutArray' from the first N elements of a list. The array is
1934+-- | Create a 'MutArray' from the first N elements of a list. The array is
1932-- allocated to size N, if the list terminates before N elements then the
1935-- allocated to size N, if the list terminates before N elements then the
1933-- array may hold less than N elements.
1936-- array may hold less than N elements.
@@ -1978,7 +1981,7 @@ fromArrayStreamK as = do
1978fromStreamD :: (MonadIO m, Unbox a) => D.Stream m a -> m (MutArray a)
1981fromStreamD :: (MonadIO m, Unbox a) => D.Stream m a -> m (MutArray a)
1979fromStreamD m = arrayStreamKFromStreamD m >>= fromArrayStreamK
1982fromStreamD m = arrayStreamKFromStreamD m >>= fromArrayStreamK
1981−-- | Create an 'MutArray' from a list. The list must be of finite size.
1984+-- | Create a 'MutArray' from a list. The list must be of finite size.
1983{-# INLINE fromList #-}
1986{-# INLINE fromList #-}
1984fromList :: (MonadIO m, Unbox a) => [a] -> m (MutArray a)
1987fromList :: (MonadIO m, Unbox a) => [a] -> m (MutArray a)