@@ -40,22 +40,22 @@ module Streamly.Internal.Data.MutArray.Type
41 -- ** Casting
41 -- ** Casting
43− , castUnsafe
43+ , castUnsafe -- XXX unsafeCast
45− , asPtrUnsafe
45+ , asPtrUnsafe -- XXX rename to unsafePinnedAsPtr?
46− , asUnpinnedPtrUnsafe
46+ , asUnpinnedPtrUnsafe -- XXX rename to unsafeAsPtr
48 -- ** Construction
48 -- ** Construction
51 -- *** New
51 -- *** New
52 -- | New arrays are always empty arrays with some reserve capacity to
52 -- | New arrays are always empty arrays with some reserve capacity to
53 -- extend the length without reallocating.
53 -- extend the length without reallocating.
55− , newArrayWith
55+ , newArrayWith -- emptyAlignedWith
56− , pinnedNew
56+ , pinnedEmptyOf
58− , pinnedNewAligned
57+ , pinnedNewAligned -- XXX not required
58+ -- , new -- uninitialized array of specified length
60 -- *** Cloning
60 -- *** Cloning
@@ -63,27 +63,25 @@ module Streamly.Internal.Data.MutArray.Type
64 -- *** Slicing
64 -- *** Slicing
65 -- | Get a subarray without copying
65 -- | Get a subarray without copying
66− , getSliceUnsafe
66+ , getSliceUnsafe -- XXX unsafeGetSlice
67 , getSlice
67 , getSlice
68 , splitAt -- XXX should be able to express using getSlice
68 , splitAt -- XXX should be able to express using getSlice
71 -- *** Stream Folds
71 -- *** Stream Folds
72 , ArrayUnsafe (..)
72 , ArrayUnsafe (..)
73− , writeNWithUnsafe
73+ , unsafeCreateOfWith
74− , writeNWith
74+ , unsafeCreateOf
75− , writeNUnsafe
75+ , unsafePinnedCreateOf
76− , pinnedWriteNUnsafe
76+ , pinnedCreateOf
77− , writeN
77+ , createOfWith
78− , pinnedWriteN
78+ , createOf
79− , pinnedWriteNAligned
79+ , revCreateOf
81− , writeWith
81+ , pinnedCreate
82− , write
82+ , createWith
83− , pinnedWrite
83+ , create
88 -- *** From containers
86 -- *** From containers
89 , fromListN
87 , fromListN
@@ -103,7 +101,7 @@ module Streamly.Internal.Data.MutArray.Type
104 -- ** Random writes
102 -- ** Random writes
105 , putIndex
103 , putIndex
106− , putIndexUnsafe
104+ , putIndexUnsafe -- XXX unsafePutIndex
107 , putIndices
105 , putIndices
108 -- , putFromThenTo
106 -- , putFromThenTo
109 -- , putFrom -- start writing at the given position
107 -- , putFrom -- start writing at the given position
@@ -111,7 +109,7 @@ module Streamly.Internal.Data.MutArray.Type
111 -- , putFromTo
109 -- , putFromTo
112 -- , putFromRev
110 -- , putFromRev
113 -- , putUptoRev
111 -- , putUptoRev
114− , modifyIndexUnsafe
112+ , modifyIndexUnsafe -- XXX unsafeModifyIndex
115 , modifyIndex
113 , modifyIndex
116 , modifyIndices
114 , modifyIndices
@@ -122,9 +120,9 @@ module Streamly.Internal.Data.MutArray.Type
123 -- *** Indexing
121 -- *** Indexing
124 , getIndex
122 , getIndex
125− , getIndexUnsafe
123+ , getIndexUnsafe -- XXX unsafeGetIndex
126 -- , getFromThenTo
124 -- , getFromThenTo
127− , getIndexRev
125+ , getIndexRev -- getRevIndex?
128 , indexReader
126 , indexReader
129 , indexReaderWith
127 , indexReaderWith
@@ -195,20 +193,20 @@ module Streamly.Internal.Data.MutArray.Type
196 , snocLinear
194 , snocLinear
197 , snocMay
195 , snocMay
198− , snocUnsafe
196+ , snocUnsafe -- XXX unsafeSnoc
200 -- *** Appending streams
198 -- *** Appending streams
201− , writeAppendNUnsafe
199+ , unsafeAppendN
202− , writeAppendN
200+ , appendN
203− , writeAppendWith
201+ , appendWith
204− , writeAppend
202+ , append
206 -- *** Appending arrays
204 -- *** Appending arrays
207 , spliceCopy
205 , spliceCopy
208 , spliceWith
206 , spliceWith
210 , spliceExp
208 , spliceExp
211− , spliceUnsafe
209+ , spliceUnsafe -- XXX unsafeSplice
212 -- , putSlice
210 -- , putSlice
213 -- , appendSlice
211 -- , appendSlice
214 -- , appendSliceFrom
212 -- , appendSliceFrom
@@ -275,6 +273,25 @@ module Streamly.Internal.Data.MutArray.Type
275 , getIndicesWith
273 , getIndicesWith
277 , resizeExp
275 , resizeExp
280+ , writeAppendNUnsafe
287+ , pinnedWriteNUnsafe
290+ , pinnedWriteNAligned -- XXX not required
@@ -456,6 +473,8 @@ isPinned MutArray{..} = Unboxed.isPinned arrContents
456-- we need more than that. See stg_pinnedNewAlignedByteArrayzh and
473-- we need more than that. See stg_pinnedNewAlignedByteArrayzh and
457-- allocatePinned in GHC source.
474-- allocatePinned in GHC source.
476+-- XXX Rename to emptyAlignedWith, alignSize should be first arg.
459-- | @newArrayWith allocator alignment count@ allocates a new array of zero
478-- | @newArrayWith allocator alignment count@ allocates a new array of zero
460-- length and with a capacity to hold @count@ elements, using @allocator
479-- length and with a capacity to hold @count@ elements, using @allocator
461-- size alignment@ as the memory allocator function.
480-- size alignment@ as the memory allocator function.
@@ -479,12 +498,25 @@ newArrayWith alloc alignSize count = do
479 , arrBound = size
498 , arrBound = size
501+-- For arrays "nil" sounds a bit odd. empty is better. The only problem with
502+-- empty is that it is also used by the Alternative type class. But assuming we
503+-- will mostly import the Array module qualified this should be fine.
505+-- | Create an empty array.
511+empty = MutArray Unboxed.empty 0 0 0
513+{-# DEPRECATED nil "Please use empty instead." #-}
483#ifdef DEVBUILD
515#ifdef DEVBUILD
484 Unbox a =>
516 Unbox a =>
486 MutArray a
518 MutArray a
487−nil = MutArray Unboxed.nil 0 0 0
519+nil = empty
489{-# INLINE newBytesAs #-}
521{-# INLINE newBytesAs #-}
490newBytesAs :: MonadIO m =>
522newBytesAs :: MonadIO m =>
@@ -507,6 +539,7 @@ newBytesAs ps bytes = do
508-- /Pre-release/
540-- /Pre-release/
509{-# INLINE pinnedNewBytes #-}
541{-# INLINE pinnedNewBytes #-}
542+{-# DEPRECATED pinnedNewBytes "Please use pinnedEmptyOf with appropriate calculation" #-}
510pinnedNewBytes :: MonadIO m =>
543pinnedNewBytes :: MonadIO m =>
511#ifdef DEVBUILD
544#ifdef DEVBUILD
512 Unbox a =>
545 Unbox a =>
@@ -531,21 +564,31 @@ newAs ps =
531 (error "new: alignment is not used in unpinned arrays.")
564 (error "new: alignment is not used in unpinned arrays.")
533-- XXX can unaligned allocation be more efficient when alignment is not needed?
566-- XXX can unaligned allocation be more efficient when alignment is not needed?
535−-- | Allocates an empty pinned array that can hold 'count' items. The memory of
568+-- | Allocates a pinned array of zero length but growable to the specified
536−-- the array is uninitialized and the allocation is aligned as per the 'Unboxed'
569+-- capacity without reallocation.
537−-- instance of the type.
570+{-# INLINE pinnedEmptyOf #-}
538−--
571+pinnedEmptyOf :: forall m a. (MonadIO m, Unbox a) => Int -> m (MutArray a)
572+pinnedEmptyOf = newAs Pinned
574+-- XXX Deprecate in major
575+-- {-# DEPRECATED pinnedNew "Please use pinnedEmptyOf instead." #-}
539{-# INLINE pinnedNew #-}
576{-# INLINE pinnedNew #-}
540pinnedNew :: forall m a. (MonadIO m, Unbox a) => Int -> m (MutArray a)
577pinnedNew :: forall m a. (MonadIO m, Unbox a) => Int -> m (MutArray a)
541−pinnedNew = newAs Pinned
578+pinnedNew = pinnedEmptyOf
543−-- | Allocates an empty unpinned array that can hold 'count' items. The memory
580+-- | Allocates an unpinned array of zero length but growable to the specified
544−-- of the array is uninitialized.
581+-- capacity without reallocation.
583+{-# INLINE emptyOf #-}
584+emptyOf :: (MonadIO m, Unbox a) => Int -> m (MutArray a)
585+emptyOf = newAs Unpinned
587+-- XXX Deprecate in major
588+-- {-# DEPRECATED new "Please use emptyOf instead." #-}
546{-# INLINE new #-}
589{-# INLINE new #-}
547new :: (MonadIO m, Unbox a) => Int -> m (MutArray a)
590new :: (MonadIO m, Unbox a) => Int -> m (MutArray a)
548−new = newAs Unpinned
591+new = emptyOf
550-------------------------------------------------------------------------------
593-------------------------------------------------------------------------------
551-- Random writes
594-- Random writes
@@ -890,6 +933,7 @@ grow nElems arr@MutArray{..} = do
890 else realloc req arr
933 else realloc req arr
892{-# DEPRECATED resize "Please use grow instead." #-}
935{-# DEPRECATED resize "Please use grow instead." #-}
936+{-# INLINE resize #-}
893resize :: forall m a. (MonadIO m, Unbox a) =>
937resize :: forall m a. (MonadIO m, Unbox a) =>
894 Int -> MutArray a -> m (MutArray a)
938 Int -> MutArray a -> m (MutArray a)
895resize = grow
939resize = grow
@@ -913,6 +957,7 @@ growExp nElems arr@MutArray{..} = do
913 else realloc req1 arr
957 else realloc req1 arr
915{-# DEPRECATED resizeExp "Please use growExp instead." #-}
959{-# DEPRECATED resizeExp "Please use growExp instead." #-}
960+{-# INLINE resizeExp #-}
916resizeExp :: forall m a. (MonadIO m, Unbox a) =>
961resizeExp :: forall m a. (MonadIO m, Unbox a) =>
917 Int -> MutArray a -> m (MutArray a)
962 Int -> MutArray a -> m (MutArray a)
918resizeExp = growExp
963resizeExp = growExp
@@ -1246,6 +1291,7 @@ indexReaderWith liftio (D.Stream stepi sti) = Unfold step inject
1246 D.Stop -> return D.Stop
1291 D.Stop -> return D.Stop
1248{-# DEPRECATED getIndicesWith "Please use indexReaderWith instead." #-}
1293{-# DEPRECATED getIndicesWith "Please use indexReaderWith instead." #-}
1294+{-# INLINE getIndicesWith #-}
1249getIndicesWith :: (Monad m, Unbox a) =>
1295getIndicesWith :: (Monad m, Unbox a) =>
1250 (forall b. IO b -> m b) -> D.Stream m Int -> Unfold m (MutArray a) a
1296 (forall b. IO b -> m b) -> D.Stream m Int -> Unfold m (MutArray a) a
1251getIndicesWith = indexReaderWith
1297getIndicesWith = indexReaderWith
@@ -1261,6 +1307,7 @@ indexReader = indexReaderWith liftIO
1262-- XXX DO NOT REMOVE, change the signature to use Stream instead of unfold
1308-- XXX DO NOT REMOVE, change the signature to use Stream instead of unfold
1263{-# DEPRECATED getIndices "Please use indexReader instead." #-}
1309{-# DEPRECATED getIndices "Please use indexReader instead." #-}
1310+{-# INLINE getIndices #-}
1264getIndices :: (MonadIO m, Unbox a) => Stream m Int -> Unfold m (MutArray a) a
1311getIndices :: (MonadIO m, Unbox a) => Stream m Int -> Unfold m (MutArray a) a
1265getIndices = indexReader
1312getIndices = indexReader
@@ -1557,7 +1604,7 @@ chunksOfAs ps n (D.Stream step state) =
1558-- Same as the following but may be more efficient:
1605-- Same as the following but may be more efficient:
1560−-- >>> chunksOf n = Stream.foldMany (MutArray.writeN n)
1607+-- >>> chunksOf n = Stream.foldMany (MutArray.createOf n)
1562-- /Pre-release/
1609-- /Pre-release/
1563{-# INLINE_NORMAL chunksOf #-}
1610{-# INLINE_NORMAL chunksOf #-}
@@ -1566,14 +1613,14 @@ chunksOf :: forall m a. (MonadIO m, Unbox a)
1566-- XXX the idiomatic implementation leads to large regression in the D.reverse'
1613-- XXX the idiomatic implementation leads to large regression in the D.reverse'
1567-- benchmark. It seems it has difficulty producing optimized code when
1614-- benchmark. It seems it has difficulty producing optimized code when
1568-- converting to StreamK. Investigate GHC optimizations.
1615-- converting to StreamK. Investigate GHC optimizations.
1569−-- chunksOf n = D.foldMany (writeN n)
1616+-- chunksOf n = D.foldMany (createOf n)
1570chunksOf = chunksOfAs Unpinned
1617chunksOf = chunksOfAs Unpinned
1572-- | Like 'chunksOf' but creates pinned arrays.
1619-- | Like 'chunksOf' but creates pinned arrays.
1573{-# INLINE_NORMAL pinnedChunksOf #-}
1620{-# INLINE_NORMAL pinnedChunksOf #-}
1574pinnedChunksOf :: forall m a. (MonadIO m, Unbox a)
1621pinnedChunksOf :: forall m a. (MonadIO m, Unbox a)
1575 => Int -> D.Stream m a -> D.Stream m (MutArray a)
1622 => Int -> D.Stream m a -> D.Stream m (MutArray a)
1576−-- pinnedChunksOf n = D.foldMany (pinnedWriteN n)
1623+-- pinnedChunksOf n = D.foldMany (pinnedCreateOf n)
1577pinnedChunksOf = chunksOfAs Pinned
1624pinnedChunksOf = chunksOfAs Pinned
1579-- | When we are buffering a stream of unknown size into an array we do not
1626-- | When we are buffering a stream of unknown size into an array we do not
@@ -1636,6 +1683,7 @@ concat (D.Stream step state) = D.Stream step' (OuterLoop state)
1636 return $ D.Yield x (InnerLoop st contents (INDEX_NEXT(p,a)) end)
1683 return $ D.Yield x (InnerLoop st contents (INDEX_NEXT(p,a)) end)
1638{-# DEPRECATED flattenArrays "Please use \"unfoldMany reader\" instead." #-}
1685{-# DEPRECATED flattenArrays "Please use \"unfoldMany reader\" instead." #-}
1686+{-# INLINE flattenArrays #-}
1639flattenArrays :: forall m a. (MonadIO m, Unbox a)
1687flattenArrays :: forall m a. (MonadIO m, Unbox a)
1640 => D.Stream m (MutArray a) -> D.Stream m a
1688 => D.Stream m (MutArray a) -> D.Stream m a
1641flattenArrays = concat
1689flattenArrays = concat
@@ -1672,6 +1720,7 @@ concatRev (D.Stream step state) = D.Stream step' (OuterLoop state)
1672 return $ D.Yield x (InnerLoop st contents cur start)
1720 return $ D.Yield x (InnerLoop st contents cur start)
1674{-# DEPRECATED flattenArraysRev "Please use \"unfoldMany readerRev\" instead." #-}
1722{-# DEPRECATED flattenArraysRev "Please use \"unfoldMany readerRev\" instead." #-}
1723+{-# INLINE flattenArraysRev #-}
1675flattenArraysRev :: forall m a. (MonadIO m, Unbox a)
1724flattenArraysRev :: forall m a. (MonadIO m, Unbox a)
1676 => D.Stream m (MutArray a) -> D.Stream m a
1725 => D.Stream m (MutArray a) -> D.Stream m a
1677flattenArraysRev = concatRev
1726flattenArraysRev = concatRev
@@ -1903,7 +1952,7 @@ foldr f z arr = D.foldr f z $ read arr
1904-- XXX Keep the bound intact to not lose any free space? Perf impact?
1953-- XXX Keep the bound intact to not lose any free space? Perf impact?
1906−-- | @writeAppendNUnsafe n arr@ appends up to @n@ input items to the supplied
1955+-- | @unsafeAppendN n arr@ appends up to @n@ input items to the supplied
1907-- array.
1956-- array.
1909-- Unsafe: Do not drive the fold beyond @n@ elements, it will lead to memory
1958-- Unsafe: Do not drive the fold beyond @n@ elements, it will lead to memory
@@ -1912,13 +1961,12 @@ foldr f z arr = D.foldr f z $ read arr
1912-- Any free space left in the array after appending @n@ elements is lost.
1961-- Any free space left in the array after appending @n@ elements is lost.
1914-- /Internal/
1963-- /Internal/
1915−{-# INLINE_NORMAL writeAppendNUnsafe #-}
1964+{-# INLINE_NORMAL unsafeAppendN #-}
1916−writeAppendNUnsafe :: forall m a. (MonadIO m, Unbox a) =>
1965+unsafeAppendN :: forall m a. (MonadIO m, Unbox a) =>
1918 -> m (MutArray a)
1967 -> m (MutArray a)
1919 -> Fold m a (MutArray a)
1968 -> Fold m a (MutArray a)
1920−writeAppendNUnsafe n action =
1921− fmap fromArrayUnsafe $ FL.foldlM' step initial
1969+unsafeAppendN n action = fmap fromArrayUnsafe $ FL.foldlM' step initial
@@ -1931,7 +1979,7 @@ writeAppendNUnsafe n action =
1931 -- otherwise we lose that space.
1979 -- otherwise we lose that space.
1933 if free < needed
1981 if free < needed
1934− then noinline reallocWith "writeAppendNUnsafeWith" (+ needed) needed arr
1982+ then noinline reallocWith "unsafeAppendN" (+ needed) needed arr
1935 else return arr
1983 else return arr
1936 return $ toArrayUnsafe arr1
1984 return $ toArrayUnsafe arr1
@@ -1939,30 +1987,51 @@ writeAppendNUnsafe n action =
1939 liftIO $ pokeAt end contents x
1987 liftIO $ pokeAt end contents x
1940 return $ ArrayUnsafe contents start (INDEX_NEXT(end,a))
1988 return $ ArrayUnsafe contents start (INDEX_NEXT(end,a))
1990+{-# DEPRECATED writeAppendNUnsafe "Please use unsafeAppendN instead." #-}
1991+{-# INLINE writeAppendNUnsafe #-}
1992+writeAppendNUnsafe :: forall m a. (MonadIO m, Unbox a) =>
1995+ -> Fold m a (MutArray a)
1996+writeAppendNUnsafe = unsafeAppendN
1942-- | Append @n@ elements to an existing array. Any free space left in the array
1998-- | Append @n@ elements to an existing array. Any free space left in the array
1943-- after appending @n@ elements is lost.
1999-- after appending @n@ elements is lost.
1945−-- >>> writeAppendN n initial = Fold.take n (MutArray.writeAppendNUnsafe n initial)
2001+-- >>> appendN n initial = Fold.take n (MutArray.unsafeAppendN n initial)
1947−{-# INLINE_NORMAL writeAppendN #-}
2003+{-# INLINE_NORMAL appendN #-}
2004+appendN :: forall m a. (MonadIO m, Unbox a) =>
2005+ Int -> m (MutArray a) -> Fold m a (MutArray a)
2006+appendN n initial = FL.take n (unsafeAppendN n initial)
2008+-- XXX Deprecate in major
2009+-- {-# DEPRECATED writeAppendN "Please use appendN instead." #-}
2010+{-# INLINE writeAppendN #-}
1948writeAppendN :: forall m a. (MonadIO m, Unbox a) =>
2011writeAppendN :: forall m a. (MonadIO m, Unbox a) =>
1949 Int -> m (MutArray a) -> Fold m a (MutArray a)
2012 Int -> m (MutArray a) -> Fold m a (MutArray a)
1950−writeAppendN n initial = FL.take n (writeAppendNUnsafe n initial)
2013+writeAppendN = appendN
1952−-- | @writeAppendWith realloc action@ mutates the array generated by @action@ to
2015+-- | @appendWith realloc action@ mutates the array generated by @action@ to
1953-- append the input stream. If there is no reserved space available in the
2016-- append the input stream. If there is no reserved space available in the
1954-- array it is reallocated to a size in bytes determined by @realloc oldSize@,
2017-- array it is reallocated to a size in bytes determined by @realloc oldSize@,
1955-- where @oldSize@ is the current size of the array in bytes.
2018-- where @oldSize@ is the current size of the array in bytes.
1957-- Note that the returned array may be a mutated version of original array.
2020-- Note that the returned array may be a mutated version of original array.
1959−-- >>> writeAppendWith sizer = Fold.foldlM' (MutArray.snocWith sizer)
2022+-- >>> appendWith sizer = Fold.foldlM' (MutArray.snocWith sizer)
1961-- /Pre-release/
2024-- /Pre-release/
2025+{-# INLINE appendWith #-}
2026+appendWith :: forall m a. (MonadIO m, Unbox a) =>
2027+ (Int -> Int) -> m (MutArray a) -> Fold m a (MutArray a)
2028+appendWith sizer = FL.foldlM' (snocWith sizer)
2030+{-# DEPRECATED writeAppendWith "Please use appendWith instead." #-}
1962{-# INLINE writeAppendWith #-}
2031{-# INLINE writeAppendWith #-}
1963writeAppendWith :: forall m a. (MonadIO m, Unbox a) =>
2032writeAppendWith :: forall m a. (MonadIO m, Unbox a) =>
1964 (Int -> Int) -> m (MutArray a) -> Fold m a (MutArray a)
2033 (Int -> Int) -> m (MutArray a) -> Fold m a (MutArray a)
1965−writeAppendWith sizer = FL.foldlM' (snocWith sizer)
2034+writeAppendWith = appendWith
1967-- | @append action@ mutates the array generated by @action@ to append the
2036-- | @append action@ mutates the array generated by @action@ to append the
1968-- input stream. If there is no reserved space available in the array it is
2037-- input stream. If there is no reserved space available in the array it is
@@ -1970,26 +2039,33 @@ writeAppendWith sizer = FL.foldlM' (snocWith sizer)
1971-- Note that the returned array may be a mutated version of original array.
2040-- Note that the returned array may be a mutated version of original array.
1973−-- >>> writeAppend = MutArray.writeAppendWith (* 2)
2042+-- >>> append = MutArray.appendWith (* 2)
2044+{-# INLINE append #-}
2045+append :: forall m a. (MonadIO m, Unbox a) =>
2046+ m (MutArray a) -> Fold m a (MutArray a)
2047+append = appendWith (* 2)
2049+-- XXX Deprecate in major
2050+-- {-# DEPRECATED writeAppend "Please use append instead." #-}
1975{-# INLINE writeAppend #-}
2051{-# INLINE writeAppend #-}
1976writeAppend :: forall m a. (MonadIO m, Unbox a) =>
2052writeAppend :: forall m a. (MonadIO m, Unbox a) =>
1977 m (MutArray a) -> Fold m a (MutArray a)
2053 m (MutArray a) -> Fold m a (MutArray a)
1978−writeAppend = writeAppendWith (* 2)
2054+writeAppend = append
1980-- XXX We can carry bound as well in the state to make sure we do not lose the
2056-- XXX We can carry bound as well in the state to make sure we do not lose the
1981-- remaining capacity. Need to check perf impact.
2057-- remaining capacity. Need to check perf impact.
1983−-- | Like 'writeNUnsafe' but takes a new array allocator @alloc size@ function
2059+-- | Like 'unsafeCreateOf' but takes a new array allocator @alloc size@
1984−-- as argument.
2060+-- function as argument.
1986−-- >>> writeNWithUnsafe alloc n = MutArray.writeAppendNUnsafe (alloc n) n
2062+-- >>> unsafeCreateOfWith alloc n = MutArray.unsafeAppendN (alloc n) n
1988-- /Pre-release/
2064-- /Pre-release/
1989−{-# INLINE_NORMAL writeNWithUnsafe #-}
2065+{-# INLINE_NORMAL unsafeCreateOfWith #-}
1990−writeNWithUnsafe :: forall m a. (MonadIO m, Unbox a)
2066+unsafeCreateOfWith :: forall m a. (MonadIO m, Unbox a)
1991 => (Int -> m (MutArray a)) -> Int -> Fold m a (MutArray a)
2067 => (Int -> m (MutArray a)) -> Int -> Fold m a (MutArray a)
1992−writeNWithUnsafe alloc n = fromArrayUnsafe <$> FL.foldlM' step initial
2068+unsafeCreateOfWith alloc n = fromArrayUnsafe <$> FL.foldlM' step initial
@@ -2000,40 +2076,64 @@ writeNWithUnsafe alloc n = fromArrayUnsafe <$> FL.foldlM' step initial
2001 $ ArrayUnsafe contents start (INDEX_NEXT(end,a))
2077 $ ArrayUnsafe contents start (INDEX_NEXT(end,a))
2079+{-# DEPRECATED writeNWithUnsafe "Please use unsafeCreateOfWith instead." #-}
2080+{-# INLINE writeNWithUnsafe #-}
2081+writeNWithUnsafe :: forall m a. (MonadIO m, Unbox a)
2082+ => (Int -> m (MutArray a)) -> Int -> Fold m a (MutArray a)
2083+writeNWithUnsafe = unsafeCreateOfWith
2003{-# INLINE_NORMAL writeNUnsafeAs #-}
2085{-# INLINE_NORMAL writeNUnsafeAs #-}
2004writeNUnsafeAs :: forall m a. (MonadIO m, Unbox a)
2086writeNUnsafeAs :: forall m a. (MonadIO m, Unbox a)
2005 => PinnedState -> Int -> Fold m a (MutArray a)
2087 => PinnedState -> Int -> Fold m a (MutArray a)
2006−writeNUnsafeAs ps = writeNWithUnsafe (newAs ps)
2088+writeNUnsafeAs ps = unsafeCreateOfWith (newAs ps)
2008−-- | Like 'writeN' but does not check the array bounds when writing. The fold
2090+-- | Like 'createOf' but does not check the array bounds when writing. The fold
2009-- driver must not call the step function more than 'n' times otherwise it will
2091-- driver must not call the step function more than 'n' times otherwise it will
2010-- corrupt the memory and crash. This function exists mainly because any
2092-- corrupt the memory and crash. This function exists mainly because any
2011-- conditional in the step function blocks fusion causing 10x performance
2093-- conditional in the step function blocks fusion causing 10x performance
2012-- slowdown.
2094-- slowdown.
2014−-- >>> writeNUnsafe = MutArray.writeNWithUnsafe MutArray.new
2096+-- >>> unsafeCreateOf = MutArray.unsafeCreateOfWith MutArray.emptyOf
2098+{-# INLINE_NORMAL unsafeCreateOf #-}
2099+unsafeCreateOf :: forall m a. (MonadIO m, Unbox a)
2100+ => Int -> Fold m a (MutArray a)
2101+unsafeCreateOf = writeNUnsafeAs Unpinned
2103+{-# DEPRECATED writeNUnsafe "Please use unsafeCreateOf instead." #-}
2016−{-# INLINE_NORMAL writeNUnsafe #-}
2104+{-# INLINE writeNUnsafe #-}
2017writeNUnsafe :: forall m a. (MonadIO m, Unbox a)
2105writeNUnsafe :: forall m a. (MonadIO m, Unbox a)
2018 => Int -> Fold m a (MutArray a)
2106 => Int -> Fold m a (MutArray a)
2019−writeNUnsafe = writeNUnsafeAs Unpinned
2107+writeNUnsafe = unsafeCreateOf
2021−-- | Like 'writeNUnsafe' but creates a pinned array.
2109+-- | Like 'unsafeCreateOf' but creates a pinned array.
2110+{-# INLINE_NORMAL unsafePinnedCreateOf #-}
2111+unsafePinnedCreateOf :: forall m a. (MonadIO m, Unbox a)
2112+ => Int -> Fold m a (MutArray a)
2113+unsafePinnedCreateOf = writeNUnsafeAs Pinned
2115+{-# DEPRECATED pinnedWriteNUnsafe "Please use unsafePinnedCreateOf instead." #-}
2022−{-# INLINE_NORMAL pinnedWriteNUnsafe #-}
2116+{-# INLINE pinnedWriteNUnsafe #-}
2023pinnedWriteNUnsafe :: forall m a. (MonadIO m, Unbox a)
2117pinnedWriteNUnsafe :: forall m a. (MonadIO m, Unbox a)
2024 => Int -> Fold m a (MutArray a)
2118 => Int -> Fold m a (MutArray a)
2025−pinnedWriteNUnsafe = writeNUnsafeAs Pinned
2119+pinnedWriteNUnsafe = unsafePinnedCreateOf
2027−-- | @writeNWith alloc n@ folds a maximum of @n@ elements into an array
2121+-- | @createOfWith alloc n@ folds a maximum of @n@ elements into an array
2028-- allocated using the @alloc@ function.
2122-- allocated using the @alloc@ function.
2030−-- >>> writeNWith alloc n = Fold.take n (MutArray.writeNWithUnsafe alloc n)
2124+-- >>> createOfWith alloc n = Fold.take n (MutArray.unsafeCreateOfWith alloc n)
2031−-- >>> writeNWith alloc n = MutArray.writeAppendN (alloc n) n
2125+-- >>> createOfWith alloc n = MutArray.appendN (alloc n) n
2033−{-# INLINE_NORMAL writeNWith #-}
2127+{-# INLINE_NORMAL createOfWith #-}
2128+createOfWith :: forall m a. (MonadIO m, Unbox a)
2129+ => (Int -> m (MutArray a)) -> Int -> Fold m a (MutArray a)
2130+createOfWith alloc n = FL.take n (unsafeCreateOfWith alloc n)
2132+-- {-# DEPRECATED writeNWith "Please use createOfWith instead." #-}
2133+{-# INLINE writeNWith #-}
2034writeNWith :: forall m a. (MonadIO m, Unbox a)
2134writeNWith :: forall m a. (MonadIO m, Unbox a)
2035 => (Int -> m (MutArray a)) -> Int -> Fold m a (MutArray a)
2135 => (Int -> m (MutArray a)) -> Int -> Fold m a (MutArray a)
2036−writeNWith alloc n = FL.take n (writeNWithUnsafe alloc n)
2136+writeNWith = createOfWith
2038{-# INLINE_NORMAL writeNAs #-}
2138{-# INLINE_NORMAL writeNAs #-}
2039writeNAs ::
2139writeNAs ::
@@ -2041,28 +2141,42 @@ writeNAs ::
2041 => PinnedState
2141 => PinnedState
2043 -> Fold m a (MutArray a)
2143 -> Fold m a (MutArray a)
2044−writeNAs ps = writeNWith (newAs ps)
2144+writeNAs ps = createOfWith (newAs ps)
2046−-- | @writeN n@ folds a maximum of @n@ elements from the input stream to an
2146+-- | @createOf n@ folds a maximum of @n@ elements from the input stream to an
2047-- 'MutArray'.
2147-- 'MutArray'.
2049−-- >>> writeN = MutArray.writeNWith MutArray.new
2149+-- >>> createOf = MutArray.createOfWith MutArray.new
2050−-- >>> writeN n = Fold.take n (MutArray.writeNUnsafe n)
2150+-- >>> createOf n = Fold.take n (MutArray.unsafeCreateOf n)
2051−-- >>> writeN n = MutArray.writeAppendN n (MutArray.new n)
2151+-- >>> createOf n = MutArray.appendN n (MutArray.emptyOf n)
2053−{-# INLINE_NORMAL writeN #-}
2153+{-# INLINE_NORMAL createOf #-}
2154+createOf :: forall m a. (MonadIO m, Unbox a) => Int -> Fold m a (MutArray a)
2155+createOf = writeNAs Unpinned
2157+-- XXX Deprecate in major
2158+-- {-# DEPRECATED writeN "Please use createOf instead." #-}
2159+{-# INLINE writeN #-}
2054writeN :: forall m a. (MonadIO m, Unbox a) => Int -> Fold m a (MutArray a)
2160writeN :: forall m a. (MonadIO m, Unbox a) => Int -> Fold m a (MutArray a)
2055−writeN = writeNAs Unpinned
2161+writeN = createOf
2057−-- | Like 'writeN' but creates a pinned array.
2163+-- | Like 'createOf' but creates a pinned array.
2164+{-# INLINE_NORMAL pinnedCreateOf #-}
2166+ forall m a. (MonadIO m, Unbox a)
2168+ -> Fold m a (MutArray a)
2169+pinnedCreateOf = writeNAs Pinned
2171+{-# DEPRECATED pinnedWriteN "Please use pinnedCreateOf instead." #-}
2058−{-# INLINE_NORMAL pinnedWriteN #-}
2172+{-# INLINE pinnedWriteN #-}
2059pinnedWriteN ::
2173pinnedWriteN ::
2060 forall m a. (MonadIO m, Unbox a)
2174 forall m a. (MonadIO m, Unbox a)
2062 -> Fold m a (MutArray a)
2176 -> Fold m a (MutArray a)
2063−pinnedWriteN = writeNAs Pinned
2177+pinnedWriteN = pinnedCreateOf
2065−-- | Like writeNWithUnsafe but writes the array in reverse order.
2179+-- | Like unsafeCreateOfWith but writes the array in reverse order.
2067-- /Internal/
2181-- /Internal/
2068{-# INLINE_NORMAL writeRevNWithUnsafe #-}
2182{-# INLINE_NORMAL writeRevNWithUnsafe #-}
@@ -2083,7 +2197,7 @@ writeRevNWithUnsafe alloc n = fromArrayUnsafe <$> FL.foldlM' step initial
2084 $ ArrayUnsafe contents ptr end
2198 $ ArrayUnsafe contents ptr end
2086−-- | Like writeNWith but writes the array in reverse order.
2200+-- | Like createOfWith but writes the array in reverse order.
2088-- /Internal/
2202-- /Internal/
2089{-# INLINE_NORMAL writeRevNWith #-}
2203{-# INLINE_NORMAL writeRevNWith #-}
@@ -2091,31 +2205,36 @@ writeRevNWith :: forall m a. (MonadIO m, Unbox a)
2091 => (Int -> m (MutArray a)) -> Int -> Fold m a (MutArray a)
2205 => (Int -> m (MutArray a)) -> Int -> Fold m a (MutArray a)
2092writeRevNWith alloc n = FL.take n (writeRevNWithUnsafe alloc n)
2206writeRevNWith alloc n = FL.take n (writeRevNWithUnsafe alloc n)
2094−-- | Like writeN but writes the array in reverse order.
2208+-- | Like 'createOf' but writes the array in reverse order.
2096-- /Pre-release/
2210-- /Pre-release/
2097−{-# INLINE_NORMAL writeRevN #-}
2211+{-# INLINE_NORMAL revCreateOf #-}
2212+revCreateOf :: forall m a. (MonadIO m, Unbox a) => Int -> Fold m a (MutArray a)
2213+revCreateOf = writeRevNWith new
2215+{-# DEPRECATED writeRevN "Please use revCreateOf instead." #-}
2216+{-# INLINE writeRevN #-}
2098writeRevN :: forall m a. (MonadIO m, Unbox a) => Int -> Fold m a (MutArray a)
2217writeRevN :: forall m a. (MonadIO m, Unbox a) => Int -> Fold m a (MutArray a)
2099−writeRevN = writeRevNWith new
2218+writeRevN = revCreateOf
2101-- | @pinnedWriteNAligned align n@ folds a maximum of @n@ elements from the
2220-- | @pinnedWriteNAligned align n@ folds a maximum of @n@ elements from the
2102-- input stream to a 'MutArray' aligned to the given size.
2221-- input stream to a 'MutArray' aligned to the given size.
2104−-- >>> pinnedWriteNAligned align = MutArray.writeNWith (MutArray.pinnedNewAligned align)
2223+-- >>> pinnedWriteNAligned align = MutArray.createOfWith (MutArray.pinnedNewAligned align)
2105−-- >>> pinnedWriteNAligned align n = MutArray.writeAppendN n (MutArray.pinnedNewAligned align n)
2224+-- >>> pinnedWriteNAligned align n = MutArray.appendN n (MutArray.pinnedNewAligned align n)
2107-- /Pre-release/
2226-- /Pre-release/
2109{-# INLINE_NORMAL pinnedWriteNAligned #-}
2228{-# INLINE_NORMAL pinnedWriteNAligned #-}
2110pinnedWriteNAligned :: forall m a. (MonadIO m, Unbox a)
2229pinnedWriteNAligned :: forall m a. (MonadIO m, Unbox a)
2111 => Int -> Int -> Fold m a (MutArray a)
2230 => Int -> Int -> Fold m a (MutArray a)
2112−pinnedWriteNAligned align = writeNWith (pinnedNewAligned align)
2231+pinnedWriteNAligned align = createOfWith (pinnedNewAligned align)
2114-- XXX Buffer to a list instead?
2233-- XXX Buffer to a list instead?
2116-- | Buffer a stream into a stream of arrays.
2235-- | Buffer a stream into a stream of arrays.
2118−-- >>> buildChunks n = Fold.many (MutArray.writeN n) Fold.toStreamK
2237+-- >>> buildChunks n = Fold.many (MutArray.createOf n) Fold.toStreamK
2120-- Breaking an array into an array stream can be useful to consume a large
2239-- Breaking an array into an array stream can be useful to consume a large
2121-- array sequentially such that memory of the array is released incrementatlly.
2240-- array sequentially such that memory of the array is released incrementatlly.
@@ -2127,9 +2246,10 @@ pinnedWriteNAligned align = writeNWith (pinnedNewAligned align)
2127{-# INLINE_NORMAL buildChunks #-}
2246{-# INLINE_NORMAL buildChunks #-}
2128buildChunks :: (MonadIO m, Unbox a) =>
2247buildChunks :: (MonadIO m, Unbox a) =>
2129 Int -> Fold m a (StreamK n (MutArray a))
2248 Int -> Fold m a (StreamK n (MutArray a))
2130−buildChunks n = FL.many (writeN n) FL.toStreamK
2249+buildChunks n = FL.many (createOf n) FL.toStreamK
2132{-# DEPRECATED writeChunks "Please use buildChunks instead." #-}
2251{-# DEPRECATED writeChunks "Please use buildChunks instead." #-}
2252+{-# INLINE writeChunks #-}
2133writeChunks :: (MonadIO m, Unbox a) =>
2253writeChunks :: (MonadIO m, Unbox a) =>
2134 Int -> Fold m a (StreamK n (MutArray a))
2254 Int -> Fold m a (StreamK n (MutArray a))
2135writeChunks = buildChunks
2255writeChunks = buildChunks
@@ -2137,14 +2257,14 @@ writeChunks = buildChunks
2137{-# INLINE_NORMAL writeWithAs #-}
2257{-# INLINE_NORMAL writeWithAs #-}
2138writeWithAs :: forall m a. (MonadIO m, Unbox a)
2258writeWithAs :: forall m a. (MonadIO m, Unbox a)
2139 => PinnedState -> Int -> Fold m a (MutArray a)
2259 => PinnedState -> Int -> Fold m a (MutArray a)
2140−-- writeWithAs ps n = FL.rmapM rightSize $ writeAppendWith (* 2) (newAs ps n)
2260+-- writeWithAs ps n = FL.rmapM rightSize $ appendWith (* 2) (newAs ps n)
2141writeWithAs ps elemCount =
2261writeWithAs ps elemCount =
2142 FL.rmapM extract $ FL.foldlM' step initial
2262 FL.rmapM extract $ FL.foldlM' step initial
2146 initial = do
2266 initial = do
2147− when (elemCount < 0) $ error "writeWith: elemCount is negative"
2267+ when (elemCount < 0) $ error "createWith: elemCount is negative"
2148 newAs ps elemCount
2268 newAs ps elemCount
2150 step arr@(MutArray _ start end bound) x
2270 step arr@(MutArray _ start end bound) x
@@ -2157,9 +2277,9 @@ writeWithAs ps elemCount =
2158 extract = liftIO . rightSize
2278 extract = liftIO . rightSize
2160−-- XXX Compare writeWith with fromStreamD which uses an array of streams
2280+-- XXX Compare createWith with fromStreamD which uses an array of streams
2161-- implementation. We can write this using buildChunks above if that is faster.
2281-- implementation. We can write this using buildChunks above if that is faster.
2162−-- If writeWith is faster then we should use that to implement
2282+-- If createWith is faster then we should use that to implement
2163-- fromStreamD.
2283-- fromStreamD.
2165-- XXX The realloc based implementation needs to make one extra copy if we use
2285-- XXX The realloc based implementation needs to make one extra copy if we use
@@ -2174,39 +2294,56 @@ writeWithAs ps elemCount =
2174-- XXX check if GHC's memory allocator is efficient enough. We can try the C
2294-- XXX check if GHC's memory allocator is efficient enough. We can try the C
2175-- malloc to compare against.
2295-- malloc to compare against.
2177−-- | @writeWith minCount@ folds the whole input to a single array. The array
2297+-- | @createWith minCount@ folds the whole input to a single array. The array
2178-- starts at a size big enough to hold minCount elements, the size is doubled
2298-- starts at a size big enough to hold minCount elements, the size is doubled
2179-- every time the array needs to be grown.
2299-- every time the array needs to be grown.
2181-- /Caution! Do not use this on infinite streams./
2301-- /Caution! Do not use this on infinite streams./
2183−-- >>> f n = MutArray.writeAppendWith (* 2) (MutArray.new n)
2303+-- >>> f n = MutArray.appendWith (* 2) (MutArray.emptyOf n)
2184−-- >>> writeWith n = Fold.rmapM MutArray.rightSize (f n)
2304+-- >>> createWith n = Fold.rmapM MutArray.rightSize (f n)
2185−-- >>> writeWith n = Fold.rmapM MutArray.fromChunksK (MutArray.buildChunks n)
2305+-- >>> createWith n = Fold.rmapM MutArray.fromChunksK (MutArray.buildChunks n)
2187-- /Pre-release/
2307-- /Pre-release/
2188−{-# INLINE_NORMAL writeWith #-}
2308+{-# INLINE_NORMAL createWith #-}
2309+createWith :: forall m a. (MonadIO m, Unbox a)
2310+ => Int -> Fold m a (MutArray a)
2311+-- createWith n = FL.rmapM rightSize $ appendWith (* 2) (emptyOf n)
2312+createWith = writeWithAs Unpinned
2314+{-# DEPRECATED writeWith "Please use createWith instead." #-}
2315+{-# INLINE writeWith #-}
2189writeWith :: forall m a. (MonadIO m, Unbox a)
2316writeWith :: forall m a. (MonadIO m, Unbox a)
2190 => Int -> Fold m a (MutArray a)
2317 => Int -> Fold m a (MutArray a)
2191−-- writeWith n = FL.rmapM rightSize $ writeAppendWith (* 2) (new n)
2318+writeWith = createWith
2192−writeWith = writeWithAs Unpinned
2194-- | Fold the whole input to a single array.
2320-- | Fold the whole input to a single array.
2196−-- Same as 'writeWith' using an initial array size of 'arrayChunkBytes' bytes
2322+-- Same as 'createWith' using an initial array size of 'arrayChunkBytes' bytes
2197-- rounded up to the element size.
2323-- rounded up to the element size.
2199-- /Caution! Do not use this on infinite streams./
2325-- /Caution! Do not use this on infinite streams./
2327+{-# INLINE create #-}
2328+create :: forall m a. (MonadIO m, Unbox a) => Fold m a (MutArray a)
2329+create = createWith (allocBytesToElemCount (undefined :: a) arrayChunkBytes)
2331+-- XXX Deprecate in major
2332+-- {-# DEPRECATED write "Please use create instead." #-}
2201{-# INLINE write #-}
2333{-# INLINE write #-}
2202write :: forall m a. (MonadIO m, Unbox a) => Fold m a (MutArray a)
2334write :: forall m a. (MonadIO m, Unbox a) => Fold m a (MutArray a)
2203−write = writeWith (allocBytesToElemCount (undefined :: a) arrayChunkBytes)
2335+write = create
2205−-- | Like 'write' but creates a pinned array.
2337+-- | Like 'create' but creates a pinned array.
2338+{-# INLINE pinnedCreate #-}
2339+pinnedCreate :: forall m a. (MonadIO m, Unbox a) => Fold m a (MutArray a)
2341+ writeWithAs Pinned (allocBytesToElemCount (undefined :: a) arrayChunkBytes)
2343+{-# DEPRECATED pinnedWrite "Please use pinnedCreate instead." #-}
2206{-# INLINE pinnedWrite #-}
2344{-# INLINE pinnedWrite #-}
2207pinnedWrite :: forall m a. (MonadIO m, Unbox a) => Fold m a (MutArray a)
2345pinnedWrite :: forall m a. (MonadIO m, Unbox a) => Fold m a (MutArray a)
2208−pinnedWrite =
2346+pinnedWrite = pinnedCreate
2209− writeWithAs Pinned (allocBytesToElemCount (undefined :: a) arrayChunkBytes)
2211-------------------------------------------------------------------------------
2348-------------------------------------------------------------------------------
2212-- construct from streams, known size
2349-- construct from streams, known size
@@ -2226,17 +2363,18 @@ fromStreamDNAs ps limit str = do
2226 liftIO $ pokeAt ptr arrContents x
2363 liftIO $ pokeAt ptr arrContents x
2227 return $ INDEX_NEXT(ptr,a)
2364 return $ INDEX_NEXT(ptr,a)
2229−-- | Use the 'writeN' fold instead.
2366+-- | Use the 'createOf' fold instead.
2231−-- >>> fromStreamN n = Stream.fold (MutArray.writeN n)
2368+-- >>> fromStreamN n = Stream.fold (MutArray.createOf n)
2233{-# INLINE_NORMAL fromStreamN #-}
2370{-# INLINE_NORMAL fromStreamN #-}
2234fromStreamN :: forall m a. (MonadIO m, Unbox a)
2371fromStreamN :: forall m a. (MonadIO m, Unbox a)
2235 => Int -> D.Stream m a -> m (MutArray a)
2372 => Int -> D.Stream m a -> m (MutArray a)
2236−-- fromStreamDN n = D.fold (writeN n)
2373+-- fromStreamDN n = D.fold (createOf n)
2237fromStreamN = fromStreamDNAs Unpinned
2374fromStreamN = fromStreamDNAs Unpinned
2239{-# DEPRECATED fromStreamDN "Please use fromStreamN instead." #-}
2376{-# DEPRECATED fromStreamDN "Please use fromStreamN instead." #-}
2377+{-# INLINE fromStreamDN #-}
2240fromStreamDN :: forall m a. (MonadIO m, Unbox a)
2378fromStreamDN :: forall m a. (MonadIO m, Unbox a)
2241 => Int -> D.Stream m a -> m (MutArray a)
2379 => Int -> D.Stream m a -> m (MutArray a)
2242fromStreamDN = fromStreamN
2380fromStreamDN = fromStreamN
@@ -2259,20 +2397,20 @@ pinnedFromListN n xs = fromStreamDNAs Pinned n $ D.fromList xs
2259-- /Pre-release/
2397-- /Pre-release/
2260{-# INLINE fromListRevN #-}
2398{-# INLINE fromListRevN #-}
2261fromListRevN :: (MonadIO m, Unbox a) => Int -> [a] -> m (MutArray a)
2399fromListRevN :: (MonadIO m, Unbox a) => Int -> [a] -> m (MutArray a)
2262−fromListRevN n xs = D.fold (writeRevN n) $ D.fromList xs
2400+fromListRevN n xs = D.fold (revCreateOf n) $ D.fromList xs
2264-- | Convert a pure stream in Identity monad to a mutable array.
2402-- | Convert a pure stream in Identity monad to a mutable array.
2265{-# INLINABLE fromPureStreamN #-}
2403{-# INLINABLE fromPureStreamN #-}
2266fromPureStreamN :: (MonadIO m, Unbox a) =>
2404fromPureStreamN :: (MonadIO m, Unbox a) =>
2267 Int -> Stream Identity a -> m (MutArray a)
2405 Int -> Stream Identity a -> m (MutArray a)
2268fromPureStreamN n xs =
2406fromPureStreamN n xs =
2269− D.fold (writeN n) $ D.morphInner (return . runIdentity) xs
2407+ D.fold (createOf n) $ D.morphInner (return . runIdentity) xs
2271-- | Convert a pure stream in Identity monad to a mutable array.
2409-- | Convert a pure stream in Identity monad to a mutable array.
2272{-# INLINABLE fromPureStream #-}
2410{-# INLINABLE fromPureStream #-}
2273fromPureStream :: (MonadIO m, Unbox a) => Stream Identity a -> m (MutArray a)
2411fromPureStream :: (MonadIO m, Unbox a) => Stream Identity a -> m (MutArray a)
2274fromPureStream xs =
2412fromPureStream xs =
2275− D.fold write $ D.morphInner (return . runIdentity) xs
2413+ D.fold create $ D.morphInner (return . runIdentity) xs
2277{-# INLINABLE fromPtrN #-}
2415{-# INLINABLE fromPtrN #-}
2278fromPtrN :: MonadIO m => Int -> Ptr Word8 -> m (MutArray Word8)
2416fromPtrN :: MonadIO m => Int -> Ptr Word8 -> m (MutArray Word8)
@@ -2356,6 +2494,7 @@ fromChunksK :: (Unbox a, MonadIO m) =>
2356fromChunksK = fromChunkskAs Unpinned
2494fromChunksK = fromChunkskAs Unpinned
2358{-# DEPRECATED fromArrayStreamK "Please use fromChunksK instead." #-}
2496{-# DEPRECATED fromArrayStreamK "Please use fromChunksK instead." #-}
2497+{-# INLINE fromArrayStreamK #-}
2359fromArrayStreamK :: (Unbox a, MonadIO m) =>
2498fromArrayStreamK :: (Unbox a, MonadIO m) =>
2360 StreamK m (MutArray a) -> m (MutArray a)
2499 StreamK m (MutArray a) -> m (MutArray a)
2361fromArrayStreamK = fromChunksK
2500fromArrayStreamK = fromChunksK
@@ -2367,7 +2506,7 @@ fromStreamDAs ps m =
2367 arrayStreamKFromStreamDAs Unpinned m >>= fromChunkskAs ps
2506 arrayStreamKFromStreamDAs Unpinned m >>= fromChunkskAs ps
2369-- | Create an 'Array' from a stream. This is useful when we want to create a
2508-- | Create an 'Array' from a stream. This is useful when we want to create a
2370−-- single array from a stream of unknown size. 'writeN' is at least twice
2509+-- single array from a stream of unknown size. 'createOf' is at least twice
2371-- as efficient when the size is already known.
2510-- as efficient when the size is already known.
2373-- Note that if the input stream is too large memory allocation for the array
2512-- Note that if the input stream is too large memory allocation for the array
@@ -2379,19 +2518,19 @@ fromStreamDAs ps m =
2379fromStream :: (MonadIO m, Unbox a) => Stream m a -> m (MutArray a)
2518fromStream :: (MonadIO m, Unbox a) => Stream m a -> m (MutArray a)
2380fromStream = fromStreamDAs Unpinned
2519fromStream = fromStreamDAs Unpinned
2382−-- fromStream (Stream m) = P.fold write m
2521+-- fromStream (Stream m) = P.fold create m
2383-- CAUTION: a very large number (millions) of arrays can degrade performance
2522-- CAUTION: a very large number (millions) of arrays can degrade performance
2384-- due to GC overhead because we need to buffer the arrays before we flatten
2523-- due to GC overhead because we need to buffer the arrays before we flatten
2385-- all the arrays.
2524-- all the arrays.
2387−-- XXX Compare if this is faster or "fold write".
2526+-- XXX Compare if this is faster or "fold create".
2389-- | We could take the approach of doubling the memory allocation on each
2528-- | We could take the approach of doubling the memory allocation on each
2390-- overflow. This would result in more or less the same amount of copying as in
2529-- overflow. This would result in more or less the same amount of copying as in
2391-- the chunking approach. However, if we have to shrink in the end then it may
2530-- the chunking approach. However, if we have to shrink in the end then it may
2392-- result in an extra copy of the entire data.
2531-- result in an extra copy of the entire data.
2394−-- >>> fromStreamD = StreamD.fold MutArray.write
2533+-- >>> fromStreamD = StreamD.fold MutArray.create
2396{-# INLINE fromStreamD #-}
2535{-# INLINE fromStreamD #-}
2397{-# DEPRECATED fromStreamD "Please use fromStream instead." #-}
2536{-# DEPRECATED fromStreamD "Please use fromStream instead." #-}
@@ -2421,6 +2560,10 @@ fromListRev xs = fromListRevN (Prelude.length xs) xs
2421-- Cloning
2560-- Cloning
2422-------------------------------------------------------------------------------
2561-------------------------------------------------------------------------------
2563+-- Arrays are aligned on 64-bit boundaries. The fastest way to copy an array is
2564+-- to unsafeCast it to Word64, read it, write it to Word64 array and unsafeCast
2565+-- it again. We can use SIMD read/write as well.
2424{-# INLINE cloneAs #-}
2567{-# INLINE cloneAs #-}
2425cloneAs ::
2568cloneAs ::
2426 ( MonadIO m
2569 ( MonadIO m
@@ -2512,7 +2655,7 @@ spliceWith :: forall m a. (MonadIO m, Unbox a) =>
2512 (Int -> Int -> Int) -> MutArray a -> MutArray a -> m (MutArray a)
2655 (Int -> Int -> Int) -> MutArray a -> MutArray a -> m (MutArray a)
2513spliceWith sizer dst@(MutArray _ start end bound) src = do
2656spliceWith sizer dst@(MutArray _ start end bound) src = do
2515− let f = writeAppendWith (`sizer` byteLength src) (return dst)
2658+ let f = appendWith (`sizer` byteLength src) (return dst)
2516 in D.fold f (toStreamD src)
2659 in D.fold f (toStreamD src)
2518 assert (end <= bound) (return ())
2661 assert (end <= bound) (return ())
@@ -2687,9 +2830,13 @@ cast arr =
2687-- guarantees that unsafe calls will be performed in the calling thread. Making
2830-- guarantees that unsafe calls will be performed in the calling thread. Making
2688-- it safe to pass heap-allocated objects to unsafe functions.
2831-- it safe to pass heap-allocated objects to unsafe functions.
2690−-- Should we just name it asPtr, the unsafety is implicit for any pointer
2833+-- XXX Should we just name it asPtr, the unsafety is implicit for any pointer
2691-- operations. And we are safe from Haskell perspective because we will be
2834-- operations. And we are safe from Haskell perspective because we will be
2692-- pinning the memory.
2835-- pinning the memory.
2837+-- XXX we cannot pass the length of the ptr here as in some cases it may not be
2838+-- available e.g. a null terminated C string. However, we can create another
2839+-- flavor of the API e.g. asPtrN.
2694-- | Use a @MutArray a@ as @Ptr a@. This is useful when we want to pass an
2841-- | Use a @MutArray a@ as @Ptr a@. This is useful when we want to pass an
2695-- array as a pointer to some operating system call or to a "safe" FFI call.
2842-- array as a pointer to some operating system call or to a "safe" FFI call.