@@ -40,22 +40,22 @@ module Streamly.Internal.Data.MutArray.Type
4040
4141 -- ** Casting
4242 , cast
43− , castUnsafe -- XXX unsafeCast
43+ , castUnsafe
4444 , asBytes
45− , unsafePinnedAsPtr
46− , unsafeAsPtr
45+ , asPtrUnsafe
46+ , asUnpinnedPtrUnsafe
4747
4848 -- ** Construction
49− , empty
49+ , nil
5050
5151 -- *** New
5252 -- | New arrays are always empty arrays with some reserve capacity to
5353 -- extend the length without reallocating.
54− , emptyOf
55− , newArrayWith -- emptyAlignedWith
56− , pinnedEmptyOf
57− , pinnedNewAligned -- XXX not required
58− -- , new -- uninitialized array of specified length
54+ , new
55+ , newArrayWith
56+ , pinnedNew
57+ , pinnedNewBytes
58+ , pinnedNewAligned
5959
6060 -- *** Cloning
6161 , clone
@@ -63,25 +63,27 @@ module Streamly.Internal.Data.MutArray.Type
6363
6464 -- *** Slicing
6565 -- | Get a subarray without copying
66− , getSliceUnsafe -- XXX unsafeGetSlice
66+ , getSliceUnsafe
6767 , getSlice
6868 , splitAt -- XXX should be able to express using getSlice
6969 , breakOn
7070
7171 -- *** Stream Folds
7272 , ArrayUnsafe (..)
73− , unsafeCreateOfWith
74− , unsafeCreateOf
75− , unsafePinnedCreateOf
76− , pinnedCreateOf
77− , createOfWith
78− , createOf
79− , revCreateOf
80−
81− , pinnedCreate
82− , createWith
83− , create
84− -- , revCreate
73+ , writeNWithUnsafe
74+ , writeNWith
75+ , writeNUnsafe
76+ , pinnedWriteNUnsafe
77+ , writeN
78+ , pinnedWriteN
79+ , pinnedWriteNAligned
80+
81+ , writeWith
82+ , write
83+ , pinnedWrite
84+
85+ , writeRevN
86+ -- , writeRev
8587
8688 -- *** From containers
8789 , fromListN
@@ -97,11 +99,11 @@ module Streamly.Internal.Data.MutArray.Type
9799 , fromByteStr#
98100 , fromPtrN
99101 , fromChunksK
100− , fromChunksRealloced -- fromSmallChunks
102+ , fromChunksRealloced
101103
102104 -- ** Random writes
103105 , putIndex
104− , putIndexUnsafe -- XXX unsafePutIndex
106+ , putIndexUnsafe
105107 , putIndices
106108 -- , putFromThenTo
107109 -- , putFrom -- start writing at the given position
@@ -109,7 +111,7 @@ module Streamly.Internal.Data.MutArray.Type
109111 -- , putFromTo
110112 -- , putFromRev
111113 -- , putUptoRev
112− , modifyIndexUnsafe -- XXX unsafeModifyIndex
114+ , modifyIndexUnsafe
113115 , modifyIndex
114116 , modifyIndices
115117 , modify
@@ -120,9 +122,9 @@ module Streamly.Internal.Data.MutArray.Type
120122
121123 -- *** Indexing
122124 , getIndex
123− , getIndexUnsafe -- XXX unsafeGetIndex
125+ , getIndexUnsafe
124126 -- , getFromThenTo
125− , getIndexRev -- getRevIndex?
127+ , getIndexRev
126128 , indexReader
127129 , indexReaderWith
128130
@@ -193,20 +195,20 @@ module Streamly.Internal.Data.MutArray.Type
193195 , snoc
194196 , snocLinear
195197 , snocMay
196− , snocUnsafe -- XXX unsafeSnoc
198+ , snocUnsafe
197199
198200 -- *** Appending streams
199− , unsafeAppendN
200− , appendN
201− , appendWith
202− , append
201+ , writeAppendNUnsafe
202+ , writeAppendN
203+ , writeAppendWith
204+ , writeAppend
203205
204206 -- *** Appending arrays
205207 , spliceCopy
206208 , spliceWith
207209 , splice
208210 , spliceExp
209− , spliceUnsafe -- XXX unsafeSplice
211+ , spliceUnsafe
210212 -- , putSlice
211213 -- , appendSlice
212214 -- , appendSliceFrom
@@ -214,12 +216,12 @@ module Streamly.Internal.Data.MutArray.Type
214216 -- ** Serialization using Unbox
215217 , pokeAppend
216218 , pokeAppendMay
217− , pokeSkipUnsafe -- unsafePokeSkip
219+ , pokeSkipUnsafe
218220
219221 -- ** Deserialization using Unbox
220222 , peekUncons
221− , peekUnconsUnsafe -- unsafePeekUncons
222− , peekSkipUnsafe -- unsafePeekSkip
223+ , peekUnconsUnsafe
224+ , peekSkipUnsafe
223225
224226 -- Arrays of arrays
225227 -- We can add dimensionality parameter to the array type to get
@@ -242,8 +244,6 @@ module Streamly.Internal.Data.MutArray.Type
242244
243245 -- *** Concat
244246 -- | Append the arrays in a stream to form a stream of elements.
245− , concatWith
246− , concatRevWith
247247 , concat
248248 , concatRev
249249
@@ -251,12 +251,9 @@ module Streamly.Internal.Data.MutArray.Type
251251 -- | Append the arrays in a stream to form a stream of larger arrays.
252252 , SpliceState (..)
253253 , pCompactLE
254− , pPinnedCompactLE
255− , compactLeAs
254+ , rCompactLE
256255 , fCompactGE
257− , fPinnedCompactGE
258256 , lCompactGE
259− , lPinnedCompactGE
260257 , compactGE
261258 , compactEQ
262259
@@ -267,7 +264,6 @@ module Streamly.Internal.Data.MutArray.Type
267264 , c_memchr
268265
269266 -- * Deprecated
270− , asPtrUnsafe
271267 , writeChunks
272268 , flattenArrays
273269 , flattenArraysRev
@@ -279,25 +275,6 @@ module Streamly.Internal.Data.MutArray.Type
279275 , getIndicesWith
280276 , resize
281277 , resizeExp
282− , nil
283− , new
284− , pinnedNew
285− , pinnedNewBytes
286− , writeAppendNUnsafe
287− , writeAppendN
288− , writeAppendWith
289− , writeAppend
290− , writeNWithUnsafe
291− , writeNWith
292− , writeNUnsafe
293− , pinnedWriteNUnsafe
294− , writeN
295− , pinnedWriteN
296− , pinnedWriteNAligned -- XXX not required
297− , writeWith
298− , write
299− , pinnedWrite
300− , writeRevN
301278 )
302279where
303280
@@ -479,8 +456,6 @@ isPinned MutArray{..} = Unboxed.isPinned arrContents
479456-- we need more than that. See stg_pinnedNewAlignedByteArrayzh and
480457-- allocatePinned in GHC source.
481458
482−-- XXX Rename to emptyAlignedWith, alignSize should be first arg.
483−
484459-- | @newArrayWith allocator alignment count@ allocates a new array of zero
485460-- length and with a capacity to hold @count@ elements, using @allocator
486461-- size alignment@ as the memory allocator function.
@@ -504,25 +479,12 @@ newArrayWith alloc alignSize count = do
504479 , arrBound = size
505480 }
506481
507−-- For arrays "nil" sounds a bit odd. empty is better. The only problem with
508−-- empty is that it is also used by the Alternative type class. But assuming we
509−-- will mostly import the Array module qualified this should be fine.
510−
511−-- | Create an empty array.
512−empty ::
513−#ifdef DEVBUILD
514− Unbox a =>
515−#endif
516− MutArray a
517−empty = MutArray Unboxed.empty 0 0 0
518−
519−{-# DEPRECATED nil "Please use empty instead." #-}
520482nil ::
521483#ifdef DEVBUILD
522484 Unbox a =>
523485#endif
524486 MutArray a
525−nil = empty
487+nil = MutArray Unboxed.nil 0 0 0
526488
527489{-# INLINE newBytesAs #-}
528490newBytesAs :: MonadIO m =>
@@ -545,7 +507,6 @@ newBytesAs ps bytes = do
545507--
546508-- /Pre-release/
547509{-# INLINE pinnedNewBytes #-}
548−{-# DEPRECATED pinnedNewBytes "Please use pinnedEmptyOf with appropriate calculation" #-}
549510pinnedNewBytes :: MonadIO m =>
550511#ifdef DEVBUILD
551512 Unbox a =>
@@ -570,31 +531,21 @@ newAs ps =
570531 (error "new: alignment is not used in unpinned arrays.")
571532
572533-- XXX can unaligned allocation be more efficient when alignment is not needed?
573−
574−-- | Allocates a pinned array of zero length but growable to the specified
575−-- capacity without reallocation.
576−{-# INLINE pinnedEmptyOf #-}
577−pinnedEmptyOf :: forall m a. (MonadIO m, Unbox a) => Int -> m (MutArray a)
578−pinnedEmptyOf = newAs Pinned
579−
580−-- XXX Deprecate in major
581−-- {-# DEPRECATED pinnedNew "Please use pinnedEmptyOf instead." #-}
534+--
535+-- | Allocates an empty pinned array that can hold 'count' items. The memory of
536+-- the array is uninitialized and the allocation is aligned as per the 'Unboxed'
537+-- instance of the type.
538+--
582539{-# INLINE pinnedNew #-}
583540pinnedNew :: forall m a. (MonadIO m, Unbox a) => Int -> m (MutArray a)
584−pinnedNew = pinnedEmptyOf
541+pinnedNew = newAs Pinned
585542
586−-- | Allocates an unpinned array of zero length but growable to the specified
587−-- capacity without reallocation.
543+-- | Allocates an empty unpinned array that can hold 'count' items. The memory
544+-- of the array is uninitialized.
588545--
589−{-# INLINE emptyOf #-}
590−emptyOf :: (MonadIO m, Unbox a) => Int -> m (MutArray a)
591−emptyOf = newAs Unpinned
592−
593−-- XXX Deprecate in major
594−-- {-# DEPRECATED new "Please use emptyOf instead." #-}
595546{-# INLINE new #-}
596547new :: (MonadIO m, Unbox a) => Int -> m (MutArray a)
597−new = emptyOf
548+new = newAs Unpinned
598549
599550-------------------------------------------------------------------------------
600551-- Random writes
@@ -842,15 +793,15 @@ roundDownTo elemSize size = size - (size `mod` elemSize)
842793-- Since this is not inlined Unboxed consrraint leads to dictionary passing
843794-- which complicates some inspection tests.
844795--
845−{-# NOINLINE reallocExplicitAs #-}
846−reallocExplicitAs :: PinnedState -> Int -> Int -> MutArray a -> IO (MutArray a)
847−reallocExplicitAs ps elemSize newCapacityInBytes MutArray{..} = do
796+{-# NOINLINE reallocExplicit #-}
797+reallocExplicit :: Int -> Int -> MutArray a -> IO (MutArray a)
798+reallocExplicit elemSize newCapacityInBytes MutArray{..} = do
848799 assertM(arrEnd <= arrBound)
849800
850801 -- Allocate new array
851802 let newCapMaxInBytes = roundUpLargeArray newCapacityInBytes
852803 contents <-
853− if ps == Pinned
804+ if Unboxed.isPinned arrContents
854805 then Unboxed.pinnedNew newCapMaxInBytes
855806 else Unboxed.new newCapMaxInBytes
856807 let !(MutByteArray mbarrFrom#) = arrContents
@@ -890,12 +841,7 @@ reallocExplicitAs ps elemSize newCapacityInBytes MutArray{..} = do
890841-- If the original array is pinned, the newly allocated array is also pinned.
891842{-# INLINABLE realloc #-}
892843realloc :: forall m a. (MonadIO m, Unbox a) => Int -> MutArray a -> m (MutArray a)
893−realloc bytes arr =
894− let ps =
895− if isPinned arr
896− then Pinned
897− else Unpinned
898− in liftIO $ reallocExplicitAs ps (SIZE_OF(a)) bytes arr
844+realloc bytes arr = liftIO $ reallocExplicit (SIZE_OF(a)) bytes arr
899845
900846-- | @reallocWith label capSizer minIncrBytes array@. The label is used
901847-- in error messages and the capSizer is used to determine the capacity of the
@@ -944,7 +890,6 @@ grow nElems arr@MutArray{..} = do
944890 else realloc req arr
945891
946892{-# DEPRECATED resize "Please use grow instead." #-}
947−{-# INLINE resize #-}
948893resize :: forall m a. (MonadIO m, Unbox a) =>
949894 Int -> MutArray a -> m (MutArray a)
950895resize = grow
@@ -968,7 +913,6 @@ growExp nElems arr@MutArray{..} = do
968913 else realloc req1 arr
969914
970915{-# DEPRECATED resizeExp "Please use growExp instead." #-}
971−{-# INLINE resizeExp #-}
972916resizeExp :: forall m a. (MonadIO m, Unbox a) =>
973917 Int -> MutArray a -> m (MutArray a)
974918resizeExp = growExp
@@ -1302,7 +1246,6 @@ indexReaderWith liftio (D.Stream stepi sti) = Unfold step inject
13021246 D.Stop -> return D.Stop
13031247
13041248{-# DEPRECATED getIndicesWith "Please use indexReaderWith instead." #-}
1305−{-# INLINE getIndicesWith #-}
13061249getIndicesWith :: (Monad m, Unbox a) =>
13071250 (forall b. IO b -> m b) -> D.Stream m Int -> Unfold m (MutArray a) a
13081251getIndicesWith = indexReaderWith
@@ -1318,7 +1261,6 @@ indexReader = indexReaderWith liftIO
13181261
13191262-- XXX DO NOT REMOVE, change the signature to use Stream instead of unfold
13201263{-# DEPRECATED getIndices "Please use indexReader instead." #-}
1321−{-# INLINE getIndices #-}
13221264getIndices :: (MonadIO m, Unbox a) => Stream m Int -> Unfold m (MutArray a) a
13231265getIndices = indexReader
13241266
@@ -1615,7 +1557,7 @@ chunksOfAs ps n (D.Stream step state) =
16151557--
16161558-- Same as the following but may be more efficient:
16171559--
1618−-- >>> chunksOf n = Stream.foldMany (MutArray.createOf n)
1560+-- >>> chunksOf n = Stream.foldMany (MutArray.writeN n)
16191561--
16201562-- /Pre-release/
16211563{-# INLINE_NORMAL chunksOf #-}
@@ -1624,14 +1566,14 @@ chunksOf :: forall m a. (MonadIO m, Unbox a)
16241566-- XXX the idiomatic implementation leads to large regression in the D.reverse'
16251567-- benchmark. It seems it has difficulty producing optimized code when
16261568-- converting to StreamK. Investigate GHC optimizations.
1627−-- chunksOf n = D.foldMany (createOf n)
1569+-- chunksOf n = D.foldMany (writeN n)
16281570chunksOf = chunksOfAs Unpinned
16291571
16301572-- | Like 'chunksOf' but creates pinned arrays.
16311573{-# INLINE_NORMAL pinnedChunksOf #-}
16321574pinnedChunksOf :: forall m a. (MonadIO m, Unbox a)
16331575 => Int -> D.Stream m a -> D.Stream m (MutArray a)
1634−-- pinnedChunksOf n = D.foldMany (pinnedCreateOf n)
1576+-- pinnedChunksOf n = D.foldMany (pinnedWriteN n)
16351577pinnedChunksOf = chunksOfAs Pinned
16361578
16371579-- | When we are buffering a stream of unknown size into an array we do not
@@ -1664,10 +1606,16 @@ data FlattenState s contents a =
16641606 OuterLoop s
16651607 | InnerLoop s contents !Int !Int
16661608
1667−{-# INLINE_NORMAL concatWith #-}
1668−concatWith :: forall m a. (Monad m, Unbox a)
1669− => (forall b. IO b -> m b) -> D.Stream m (MutArray a) -> D.Stream m a
1670−concatWith liftio (D.Stream step state) = D.Stream step' (OuterLoop state)
1609+-- | Use the "reader" unfold instead.
1610+--
1611+-- @concat = unfoldMany reader@
1612+--
1613+-- We can try this if there are any fusion issues in the unfold.
1614+--
1615+{-# INLINE_NORMAL concat #-}
1616+concat :: forall m a. (MonadIO m, Unbox a)
1617+ => D.Stream m (MutArray a) -> D.Stream m a
1618+concat (D.Stream step state) = D.Stream step' (OuterLoop state)
16711619
16721620 where
16731621
@@ -1684,30 +1632,24 @@ concatWith liftio (D.Stream step state) = D.Stream step' (OuterLoop state)
16841632 return $ D.Skip $ OuterLoop st
16851633
16861634 step' _ (InnerLoop st contents p end) = do
1687− x <- liftio $ peekAt p contents
1635+ x <- liftIO $ peekAt p contents
16881636 return $ D.Yield x (InnerLoop st contents (INDEX_NEXT(p,a)) end)
16891637
1690−-- | Use the "reader" unfold instead.
1691−--
1692−-- @concat = unfoldMany reader@
1693−--
1694−-- We can try this if there are any fusion issues in the unfold.
1695−--
1696−{-# INLINE_NORMAL concat #-}
1697−concat :: forall m a. (MonadIO m, Unbox a)
1698− => D.Stream m (MutArray a) -> D.Stream m a
1699−concat = concatWith liftIO
1700−
17011638{-# DEPRECATED flattenArrays "Please use \"unfoldMany reader\" instead." #-}
1702−{-# INLINE flattenArrays #-}
17031639flattenArrays :: forall m a. (MonadIO m, Unbox a)
17041640 => D.Stream m (MutArray a) -> D.Stream m a
17051641flattenArrays = concat
17061642
1707−{-# INLINE_NORMAL concatRevWith #-}
1708−concatRevWith :: forall m a. (Monad m, Unbox a)
1709− => (forall b. IO b -> m b) -> D.Stream m (MutArray a) -> D.Stream m a
1710−concatRevWith liftio (D.Stream step state) = D.Stream step' (OuterLoop state)
1643+-- | Use the "readerRev" unfold instead.
1644+--
1645+-- @concat = unfoldMany readerRev@
1646+--
1647+-- We can try this if there are any fusion issues in the unfold.
1648+--
1649+{-# INLINE_NORMAL concatRev #-}
1650+concatRev :: forall m a. (MonadIO m, Unbox a)
1651+ => D.Stream m (MutArray a) -> D.Stream m a
1652+concatRev (D.Stream step state) = D.Stream step' (OuterLoop state)
17111653
17121654 where
17131655
@@ -1725,23 +1667,11 @@ concatRevWith liftio (D.Stream step state) = D.Stream step' (OuterLoop state)
17251667 return $ D.Skip $ OuterLoop st
17261668
17271669 step' _ (InnerLoop st contents p start) = do
1728− x <- liftio $ peekAt p contents
1670+ x <- liftIO $ peekAt p contents
17291671 let cur = INDEX_PREV(p,a)
17301672 return $ D.Yield x (InnerLoop st contents cur start)
17311673
1732−-- | Use the "readerRev" unfold instead.
1733−--
1734−-- @concat = unfoldMany readerRev@
1735−--
1736−-- We can try this if there are any fusion issues in the unfold.
1737−--
1738−{-# INLINE_NORMAL concatRev #-}
1739−concatRev :: forall m a. (MonadIO m, Unbox a)
1740− => D.Stream m (MutArray a) -> D.Stream m a
1741−concatRev = concatRevWith liftIO
1742−
17431674{-# DEPRECATED flattenArraysRev "Please use \"unfoldMany readerRev\" instead." #-}
1744−{-# INLINE flattenArraysRev #-}
17451675flattenArraysRev :: forall m a. (MonadIO m, Unbox a)
17461676 => D.Stream m (MutArray a) -> D.Stream m a
17471677flattenArraysRev = concatRev
@@ -1973,7 +1903,7 @@ foldr f z arr = D.foldr f z $ read arr
19731903
19741904-- XXX Keep the bound intact to not lose any free space? Perf impact?
19751905
1976−-- | @unsafeAppendN n arr@ appends up to @n@ input items to the supplied
1906+-- | @writeAppendNUnsafe n arr@ appends up to @n@ input items to the supplied
19771907-- array.
19781908--
19791909-- Unsafe: Do not drive the fold beyond @n@ elements, it will lead to memory
@@ -1982,12 +1912,13 @@ foldr f z arr = D.foldr f z $ read arr
19821912-- Any free space left in the array after appending @n@ elements is lost.
19831913--
19841914-- /Internal/
1985−{-# INLINE_NORMAL unsafeAppendN #-}
1986−unsafeAppendN :: forall m a. (MonadIO m, Unbox a) =>
1915+{-# INLINE_NORMAL writeAppendNUnsafe #-}
1916+writeAppendNUnsafe :: forall m a. (MonadIO m, Unbox a) =>
19871917 Int
19881918 -> m (MutArray a)
19891919 -> Fold m a (MutArray a)
1990−unsafeAppendN n action = fmap fromArrayUnsafe $ FL.foldlM' step initial
1920+writeAppendNUnsafe n action =
1921+ fmap fromArrayUnsafe $ FL.foldlM' step initial
19911922
19921923 where
19931924
@@ -2000,7 +1931,7 @@ unsafeAppendN n action = fmap fromArrayUnsafe $ FL.foldlM' step initial
20001931 -- otherwise we lose that space.
20011932 arr1 <-
20021933 if free < needed
2003− then noinline reallocWith "unsafeAppendN" (+ needed) needed arr
1934+ then noinline reallocWith "writeAppendNUnsafeWith" (+ needed) needed arr
20041935 else return arr
20051936 return $ toArrayUnsafe arr1
20061937
@@ -2008,51 +1939,30 @@ unsafeAppendN n action = fmap fromArrayUnsafe $ FL.foldlM' step initial
20081939 liftIO $ pokeAt end contents x
20091940 return $ ArrayUnsafe contents start (INDEX_NEXT(end,a))
20101941
2011−{-# DEPRECATED writeAppendNUnsafe "Please use unsafeAppendN instead." #-}
2012−{-# INLINE writeAppendNUnsafe #-}
2013−writeAppendNUnsafe :: forall m a. (MonadIO m, Unbox a) =>
2014− Int
2015− -> m (MutArray a)
2016− -> Fold m a (MutArray a)
2017−writeAppendNUnsafe = unsafeAppendN
2018−
20191942-- | Append @n@ elements to an existing array. Any free space left in the array
20201943-- after appending @n@ elements is lost.
20211944--
2022−-- >>> appendN n initial = Fold.take n (MutArray.unsafeAppendN n initial)
1945+-- >>> writeAppendN n initial = Fold.take n (MutArray.writeAppendNUnsafe n initial)
20231946--
2024−{-# INLINE_NORMAL appendN #-}
2025−appendN :: forall m a. (MonadIO m, Unbox a) =>
2026− Int -> m (MutArray a) -> Fold m a (MutArray a)
2027−appendN n initial = FL.take n (unsafeAppendN n initial)
2028−
2029−-- XXX Deprecate in major
2030−-- {-# DEPRECATED writeAppendN "Please use appendN instead." #-}
2031−{-# INLINE writeAppendN #-}
1947+{-# INLINE_NORMAL writeAppendN #-}
20321948writeAppendN :: forall m a. (MonadIO m, Unbox a) =>
20331949 Int -> m (MutArray a) -> Fold m a (MutArray a)
2034−writeAppendN = appendN
1950+writeAppendN n initial = FL.take n (writeAppendNUnsafe n initial)
20351951
2036−-- | @appendWith realloc action@ mutates the array generated by @action@ to
1952+-- | @writeAppendWith realloc action@ mutates the array generated by @action@ to
20371953-- append the input stream. If there is no reserved space available in the
20381954-- array it is reallocated to a size in bytes determined by @realloc oldSize@,
20391955-- where @oldSize@ is the current size of the array in bytes.
20401956--
20411957-- Note that the returned array may be a mutated version of original array.
20421958--
2043−-- >>> appendWith sizer = Fold.foldlM' (MutArray.snocWith sizer)
1959+-- >>> writeAppendWith sizer = Fold.foldlM' (MutArray.snocWith sizer)
20441960--
20451961-- /Pre-release/
2046−{-# INLINE appendWith #-}
2047−appendWith :: forall m a. (MonadIO m, Unbox a) =>
2048− (Int -> Int) -> m (MutArray a) -> Fold m a (MutArray a)
2049−appendWith sizer = FL.foldlM' (snocWith sizer)
2050−
2051−{-# DEPRECATED writeAppendWith "Please use appendWith instead." #-}
20521962{-# INLINE writeAppendWith #-}
20531963writeAppendWith :: forall m a. (MonadIO m, Unbox a) =>
20541964 (Int -> Int) -> m (MutArray a) -> Fold m a (MutArray a)
2055−writeAppendWith = appendWith
1965+writeAppendWith sizer = FL.foldlM' (snocWith sizer)
20561966
20571967-- | @append action@ mutates the array generated by @action@ to append the
20581968-- input stream. If there is no reserved space available in the array it is
@@ -2060,33 +1970,26 @@ writeAppendWith = appendWith
20601970--
20611971-- Note that the returned array may be a mutated version of original array.
20621972--
2063−-- >>> append = MutArray.appendWith (* 2)
1973+-- >>> writeAppend = MutArray.writeAppendWith (* 2)
20641974--
2065−{-# INLINE append #-}
2066−append :: forall m a. (MonadIO m, Unbox a) =>
2067− m (MutArray a) -> Fold m a (MutArray a)
2068−append = appendWith (* 2)
2069−
2070−-- XXX Deprecate in major
2071−-- {-# DEPRECATED writeAppend "Please use append instead." #-}
20721975{-# INLINE writeAppend #-}
20731976writeAppend :: forall m a. (MonadIO m, Unbox a) =>
20741977 m (MutArray a) -> Fold m a (MutArray a)
2075−writeAppend = append
1978+writeAppend = writeAppendWith (* 2)
20761979
20771980-- XXX We can carry bound as well in the state to make sure we do not lose the
20781981-- remaining capacity. Need to check perf impact.
20791982--
2080−-- | Like 'unsafeCreateOf' but takes a new array allocator @alloc size@
2081−-- function as argument.
1983+-- | Like 'writeNUnsafe' but takes a new array allocator @alloc size@ function
1984+-- as argument.
20821985--
2083−-- >>> unsafeCreateOfWith alloc n = MutArray.unsafeAppendN (alloc n) n
1986+-- >>> writeNWithUnsafe alloc n = MutArray.writeAppendNUnsafe (alloc n) n
20841987--
20851988-- /Pre-release/
2086−{-# INLINE_NORMAL unsafeCreateOfWith #-}
2087−unsafeCreateOfWith :: forall m a. (MonadIO m, Unbox a)
1989+{-# INLINE_NORMAL writeNWithUnsafe #-}
1990+writeNWithUnsafe :: forall m a. (MonadIO m, Unbox a)
20881991 => (Int -> m (MutArray a)) -> Int -> Fold m a (MutArray a)
2089−unsafeCreateOfWith alloc n = fromArrayUnsafe <$> FL.foldlM' step initial
1992+writeNWithUnsafe alloc n = fromArrayUnsafe <$> FL.foldlM' step initial
20901993
20911994 where
20921995
@@ -2097,64 +2000,40 @@ unsafeCreateOfWith alloc n = fromArrayUnsafe <$> FL.foldlM' step initial
20972000 return
20982001 $ ArrayUnsafe contents start (INDEX_NEXT(end,a))
20992002
2100−{-# DEPRECATED writeNWithUnsafe "Please use unsafeCreateOfWith instead." #-}
2101−{-# INLINE writeNWithUnsafe #-}
2102−writeNWithUnsafe :: forall m a. (MonadIO m, Unbox a)
2103− => (Int -> m (MutArray a)) -> Int -> Fold m a (MutArray a)
2104−writeNWithUnsafe = unsafeCreateOfWith
2105−
21062003{-# INLINE_NORMAL writeNUnsafeAs #-}
21072004writeNUnsafeAs :: forall m a. (MonadIO m, Unbox a)
21082005 => PinnedState -> Int -> Fold m a (MutArray a)
2109−writeNUnsafeAs ps = unsafeCreateOfWith (newAs ps)
2006+writeNUnsafeAs ps = writeNWithUnsafe (newAs ps)
21102007
2111−-- | Like 'createOf' but does not check the array bounds when writing. The fold
2008+-- | Like 'writeN' but does not check the array bounds when writing. The fold
21122009-- driver must not call the step function more than 'n' times otherwise it will
21132010-- corrupt the memory and crash. This function exists mainly because any
21142011-- conditional in the step function blocks fusion causing 10x performance
21152012-- slowdown.
21162013--
2117−-- >>> unsafeCreateOf = MutArray.unsafeCreateOfWith MutArray.emptyOf
2014+-- >>> writeNUnsafe = MutArray.writeNWithUnsafe MutArray.new
21182015--
2119−{-# INLINE_NORMAL unsafeCreateOf #-}
2120−unsafeCreateOf :: forall m a. (MonadIO m, Unbox a)
2121− => Int -> Fold m a (MutArray a)
2122−unsafeCreateOf = writeNUnsafeAs Unpinned
2123−
2124−{-# DEPRECATED writeNUnsafe "Please use unsafeCreateOf instead." #-}
2125−{-# INLINE writeNUnsafe #-}
2016+{-# INLINE_NORMAL writeNUnsafe #-}
21262017writeNUnsafe :: forall m a. (MonadIO m, Unbox a)
21272018 => Int -> Fold m a (MutArray a)
2128−writeNUnsafe = unsafeCreateOf
2129−
2130−-- | Like 'unsafeCreateOf' but creates a pinned array.
2131−{-# INLINE_NORMAL unsafePinnedCreateOf #-}
2132−unsafePinnedCreateOf :: forall m a. (MonadIO m, Unbox a)
2133− => Int -> Fold m a (MutArray a)
2134−unsafePinnedCreateOf = writeNUnsafeAs Pinned
2019+writeNUnsafe = writeNUnsafeAs Unpinned
21352020
2136−{-# DEPRECATED pinnedWriteNUnsafe "Please use unsafePinnedCreateOf instead." #-}
2137−{-# INLINE pinnedWriteNUnsafe #-}
2021+-- | Like 'writeNUnsafe' but creates a pinned array.
2022+{-# INLINE_NORMAL pinnedWriteNUnsafe #-}
21382023pinnedWriteNUnsafe :: forall m a. (MonadIO m, Unbox a)
21392024 => Int -> Fold m a (MutArray a)
2140−pinnedWriteNUnsafe = unsafePinnedCreateOf
2025+pinnedWriteNUnsafe = writeNUnsafeAs Pinned
21412026
2142−-- | @createOfWith alloc n@ folds a maximum of @n@ elements into an array
2027+-- | @writeNWith alloc n@ folds a maximum of @n@ elements into an array
21432028-- allocated using the @alloc@ function.
21442029--
2145−-- >>> createOfWith alloc n = Fold.take n (MutArray.unsafeCreateOfWith alloc n)
2146−-- >>> createOfWith alloc n = MutArray.appendN (alloc n) n
2030+-- >>> writeNWith alloc n = Fold.take n (MutArray.writeNWithUnsafe alloc n)
2031+-- >>> writeNWith alloc n = MutArray.writeAppendN (alloc n) n
21472032--
2148−{-# INLINE_NORMAL createOfWith #-}
2149−createOfWith :: forall m a. (MonadIO m, Unbox a)
2150− => (Int -> m (MutArray a)) -> Int -> Fold m a (MutArray a)
2151−createOfWith alloc n = FL.take n (unsafeCreateOfWith alloc n)
2152−
2153−-- {-# DEPRECATED writeNWith "Please use createOfWith instead." #-}
2154−{-# INLINE writeNWith #-}
2033+{-# INLINE_NORMAL writeNWith #-}
21552034writeNWith :: forall m a. (MonadIO m, Unbox a)
21562035 => (Int -> m (MutArray a)) -> Int -> Fold m a (MutArray a)
2157−writeNWith = createOfWith
2036+writeNWith alloc n = FL.take n (writeNWithUnsafe alloc n)
21582037
21592038{-# INLINE_NORMAL writeNAs #-}
21602039writeNAs ::
@@ -2162,42 +2041,28 @@ writeNAs ::
21622041 => PinnedState
21632042 -> Int
21642043 -> Fold m a (MutArray a)
2165−writeNAs ps = createOfWith (newAs ps)
2044+writeNAs ps = writeNWith (newAs ps)
21662045
2167−-- | @createOf n@ folds a maximum of @n@ elements from the input stream to an
2046+-- | @writeN n@ folds a maximum of @n@ elements from the input stream to an
21682047-- 'MutArray'.
21692048--
2170−-- >>> createOf = MutArray.createOfWith MutArray.new
2171−-- >>> createOf n = Fold.take n (MutArray.unsafeCreateOf n)
2172−-- >>> createOf n = MutArray.appendN n (MutArray.emptyOf n)
2049+-- >>> writeN = MutArray.writeNWith MutArray.new
2050+-- >>> writeN n = Fold.take n (MutArray.writeNUnsafe n)
2051+-- >>> writeN n = MutArray.writeAppendN n (MutArray.new n)
21732052--
2174−{-# INLINE_NORMAL createOf #-}
2175−createOf :: forall m a. (MonadIO m, Unbox a) => Int -> Fold m a (MutArray a)
2176−createOf = writeNAs Unpinned
2177−
2178−-- XXX Deprecate in major
2179−-- {-# DEPRECATED writeN "Please use createOf instead." #-}
2180−{-# INLINE writeN #-}
2053+{-# INLINE_NORMAL writeN #-}
21812054writeN :: forall m a. (MonadIO m, Unbox a) => Int -> Fold m a (MutArray a)
2182−writeN = createOf
2183−
2184−-- | Like 'createOf' but creates a pinned array.
2185−{-# INLINE_NORMAL pinnedCreateOf #-}
2186−pinnedCreateOf ::
2187− forall m a. (MonadIO m, Unbox a)
2188− => Int
2189− -> Fold m a (MutArray a)
2190−pinnedCreateOf = writeNAs Pinned
2055+writeN = writeNAs Unpinned
21912056
2192−{-# DEPRECATED pinnedWriteN "Please use pinnedCreateOf instead." #-}
2193−{-# INLINE pinnedWriteN #-}
2057+-- | Like 'writeN' but creates a pinned array.
2058+{-# INLINE_NORMAL pinnedWriteN #-}
21942059pinnedWriteN ::
21952060 forall m a. (MonadIO m, Unbox a)
21962061 => Int
21972062 -> Fold m a (MutArray a)
2198−pinnedWriteN = pinnedCreateOf
2063+pinnedWriteN = writeNAs Pinned
21992064
2200−-- | Like unsafeCreateOfWith but writes the array in reverse order.
2065+-- | Like writeNWithUnsafe but writes the array in reverse order.
22012066--
22022067-- /Internal/
22032068{-# INLINE_NORMAL writeRevNWithUnsafe #-}
@@ -2218,7 +2083,7 @@ writeRevNWithUnsafe alloc n = fromArrayUnsafe <$> FL.foldlM' step initial
22182083 return
22192084 $ ArrayUnsafe contents ptr end
22202085
2221−-- | Like createOfWith but writes the array in reverse order.
2086+-- | Like writeNWith but writes the array in reverse order.
22222087--
22232088-- /Internal/
22242089{-# INLINE_NORMAL writeRevNWith #-}
@@ -2226,36 +2091,31 @@ writeRevNWith :: forall m a. (MonadIO m, Unbox a)
22262091 => (Int -> m (MutArray a)) -> Int -> Fold m a (MutArray a)
22272092writeRevNWith alloc n = FL.take n (writeRevNWithUnsafe alloc n)
22282093
2229−-- | Like 'createOf' but writes the array in reverse order.
2094+-- | Like writeN but writes the array in reverse order.
22302095--
22312096-- /Pre-release/
2232−{-# INLINE_NORMAL revCreateOf #-}
2233−revCreateOf :: forall m a. (MonadIO m, Unbox a) => Int -> Fold m a (MutArray a)
2234−revCreateOf = writeRevNWith new
2235−
2236−{-# DEPRECATED writeRevN "Please use revCreateOf instead." #-}
2237−{-# INLINE writeRevN #-}
2097+{-# INLINE_NORMAL writeRevN #-}
22382098writeRevN :: forall m a. (MonadIO m, Unbox a) => Int -> Fold m a (MutArray a)
2239−writeRevN = revCreateOf
2099+writeRevN = writeRevNWith new
22402100
22412101-- | @pinnedWriteNAligned align n@ folds a maximum of @n@ elements from the
22422102-- input stream to a 'MutArray' aligned to the given size.
22432103--
2244−-- >>> pinnedWriteNAligned align = MutArray.createOfWith (MutArray.pinnedNewAligned align)
2245−-- >>> pinnedWriteNAligned align n = MutArray.appendN n (MutArray.pinnedNewAligned align n)
2104+-- >>> pinnedWriteNAligned align = MutArray.writeNWith (MutArray.pinnedNewAligned align)
2105+-- >>> pinnedWriteNAligned align n = MutArray.writeAppendN n (MutArray.pinnedNewAligned align n)
22462106--
22472107-- /Pre-release/
22482108--
22492109{-# INLINE_NORMAL pinnedWriteNAligned #-}
22502110pinnedWriteNAligned :: forall m a. (MonadIO m, Unbox a)
22512111 => Int -> Int -> Fold m a (MutArray a)
2252−pinnedWriteNAligned align = createOfWith (pinnedNewAligned align)
2112+pinnedWriteNAligned align = writeNWith (pinnedNewAligned align)
22532113
22542114-- XXX Buffer to a list instead?
22552115
22562116-- | Buffer a stream into a stream of arrays.
22572117--
2258−-- >>> buildChunks n = Fold.many (MutArray.createOf n) Fold.toStreamK
2118+-- >>> buildChunks n = Fold.many (MutArray.writeN n) Fold.toStreamK
22592119--
22602120-- Breaking an array into an array stream can be useful to consume a large
22612121-- array sequentially such that memory of the array is released incrementatlly.
@@ -2267,10 +2127,9 @@ pinnedWriteNAligned align = createOfWith (pinnedNewAligned align)
22672127{-# INLINE_NORMAL buildChunks #-}
22682128buildChunks :: (MonadIO m, Unbox a) =>
22692129 Int -> Fold m a (StreamK n (MutArray a))
2270−buildChunks n = FL.many (createOf n) FL.toStreamK
2130+buildChunks n = FL.many (writeN n) FL.toStreamK
22712131
22722132{-# DEPRECATED writeChunks "Please use buildChunks instead." #-}
2273−{-# INLINE writeChunks #-}
22742133writeChunks :: (MonadIO m, Unbox a) =>
22752134 Int -> Fold m a (StreamK n (MutArray a))
22762135writeChunks = buildChunks
@@ -2278,29 +2137,29 @@ writeChunks = buildChunks
22782137{-# INLINE_NORMAL writeWithAs #-}
22792138writeWithAs :: forall m a. (MonadIO m, Unbox a)
22802139 => PinnedState -> Int -> Fold m a (MutArray a)
2281−-- writeWithAs ps n = FL.rmapM rightSize $ appendWith (* 2) (newAs ps n)
2140+-- writeWithAs ps n = FL.rmapM rightSize $ writeAppendWith (* 2) (newAs ps n)
22822141writeWithAs ps elemCount =
22832142 FL.rmapM extract $ FL.foldlM' step initial
22842143
22852144 where
22862145
22872146 initial = do
2288− when (elemCount < 0) $ error "createWith: elemCount is negative"
2147+ when (elemCount < 0) $ error "writeWith: elemCount is negative"
22892148 newAs ps elemCount
22902149
22912150 step arr@(MutArray _ start end bound) x
22922151 | INDEX_NEXT(end,a) > bound = do
22932152 let oldSize = end - start
22942153 newSize = max (oldSize * 2) 1
2295− arr1 <- liftIO $ reallocExplicitAs ps (SIZE_OF(a)) newSize arr
2154+ arr1 <- liftIO $ reallocExplicit (SIZE_OF(a)) newSize arr
22962155 snocUnsafe arr1 x
22972156 step arr x = snocUnsafe arr x
22982157
22992158 extract = liftIO . rightSize
23002159
2301−-- XXX Compare createWith with fromStreamD which uses an array of streams
2160+-- XXX Compare writeWith with fromStreamD which uses an array of streams
23022161-- implementation. We can write this using buildChunks above if that is faster.
2303−-- If createWith is faster then we should use that to implement
2162+-- If writeWith is faster then we should use that to implement
23042163-- fromStreamD.
23052164--
23062165-- XXX The realloc based implementation needs to make one extra copy if we use
@@ -2315,56 +2174,39 @@ writeWithAs ps elemCount =
23152174-- XXX check if GHC's memory allocator is efficient enough. We can try the C
23162175-- malloc to compare against.
23172176
2318−-- | @createWith minCount@ folds the whole input to a single array. The array
2177+-- | @writeWith minCount@ folds the whole input to a single array. The array
23192178-- starts at a size big enough to hold minCount elements, the size is doubled
23202179-- every time the array needs to be grown.
23212180--
23222181-- /Caution! Do not use this on infinite streams./
23232182--
2324−-- >>> f n = MutArray.appendWith (* 2) (MutArray.emptyOf n)
2325−-- >>> createWith n = Fold.rmapM MutArray.rightSize (f n)
2326−-- >>> createWith n = Fold.rmapM MutArray.fromChunksK (MutArray.buildChunks n)
2183+-- >>> f n = MutArray.writeAppendWith (* 2) (MutArray.new n)
2184+-- >>> writeWith n = Fold.rmapM MutArray.rightSize (f n)
2185+-- >>> writeWith n = Fold.rmapM MutArray.fromChunksK (MutArray.buildChunks n)
23272186--
23282187-- /Pre-release/
2329−{-# INLINE_NORMAL createWith #-}
2330−createWith :: forall m a. (MonadIO m, Unbox a)
2331− => Int -> Fold m a (MutArray a)
2332−-- createWith n = FL.rmapM rightSize $ appendWith (* 2) (emptyOf n)
2333−createWith = writeWithAs Unpinned
2334−
2335−{-# DEPRECATED writeWith "Please use createWith instead." #-}
2336−{-# INLINE writeWith #-}
2188+{-# INLINE_NORMAL writeWith #-}
23372189writeWith :: forall m a. (MonadIO m, Unbox a)
23382190 => Int -> Fold m a (MutArray a)
2339−writeWith = createWith
2191+-- writeWith n = FL.rmapM rightSize $ writeAppendWith (* 2) (new n)
2192+writeWith = writeWithAs Unpinned
23402193
23412194-- | Fold the whole input to a single array.
23422195--
2343−-- Same as 'createWith' using an initial array size of 'arrayChunkBytes' bytes
2196+-- Same as 'writeWith' using an initial array size of 'arrayChunkBytes' bytes
23442197-- rounded up to the element size.
23452198--
23462199-- /Caution! Do not use this on infinite streams./
23472200--
2348−{-# INLINE create #-}
2349−create :: forall m a. (MonadIO m, Unbox a) => Fold m a (MutArray a)
2350−create = createWith (allocBytesToElemCount (undefined :: a) arrayChunkBytes)
2351−
2352−-- XXX Deprecate in major
2353−-- {-# DEPRECATED write "Please use create instead." #-}
23542201{-# INLINE write #-}
23552202write :: forall m a. (MonadIO m, Unbox a) => Fold m a (MutArray a)
2356−write = create
2203+write = writeWith (allocBytesToElemCount (undefined :: a) arrayChunkBytes)
23572204
2358−-- | Like 'create' but creates a pinned array.
2359−{-# INLINE pinnedCreate #-}
2360−pinnedCreate :: forall m a. (MonadIO m, Unbox a) => Fold m a (MutArray a)
2361−pinnedCreate =
2362− writeWithAs Pinned (allocBytesToElemCount (undefined :: a) arrayChunkBytes)
2363−
2364−{-# DEPRECATED pinnedWrite "Please use pinnedCreate instead." #-}
2205+-- | Like 'write' but creates a pinned array.
23652206{-# INLINE pinnedWrite #-}
23662207pinnedWrite :: forall m a. (MonadIO m, Unbox a) => Fold m a (MutArray a)
2367−pinnedWrite = pinnedCreate
2208+pinnedWrite =
2209+ writeWithAs Pinned (allocBytesToElemCount (undefined :: a) arrayChunkBytes)
23682210
23692211-------------------------------------------------------------------------------
23702212-- construct from streams, known size
@@ -2384,18 +2226,17 @@ fromStreamDNAs ps limit str = do
23842226 liftIO $ pokeAt ptr arrContents x
23852227 return $ INDEX_NEXT(ptr,a)
23862228
2387−-- | Use the 'createOf' fold instead.
2229+-- | Use the 'writeN' fold instead.
23882230--
2389−-- >>> fromStreamN n = Stream.fold (MutArray.createOf n)
2231+-- >>> fromStreamN n = Stream.fold (MutArray.writeN n)
23902232--
23912233{-# INLINE_NORMAL fromStreamN #-}
23922234fromStreamN :: forall m a. (MonadIO m, Unbox a)
23932235 => Int -> D.Stream m a -> m (MutArray a)
2394−-- fromStreamDN n = D.fold (createOf n)
2236+-- fromStreamDN n = D.fold (writeN n)
23952237fromStreamN = fromStreamDNAs Unpinned
23962238
23972239{-# DEPRECATED fromStreamDN "Please use fromStreamN instead." #-}
2398−{-# INLINE fromStreamDN #-}
23992240fromStreamDN :: forall m a. (MonadIO m, Unbox a)
24002241 => Int -> D.Stream m a -> m (MutArray a)
24012242fromStreamDN = fromStreamN
@@ -2418,20 +2259,20 @@ pinnedFromListN n xs = fromStreamDNAs Pinned n $ D.fromList xs
24182259-- /Pre-release/
24192260{-# INLINE fromListRevN #-}
24202261fromListRevN :: (MonadIO m, Unbox a) => Int -> [a] -> m (MutArray a)
2421−fromListRevN n xs = D.fold (revCreateOf n) $ D.fromList xs
2262+fromListRevN n xs = D.fold (writeRevN n) $ D.fromList xs
24222263
24232264-- | Convert a pure stream in Identity monad to a mutable array.
24242265{-# INLINABLE fromPureStreamN #-}
24252266fromPureStreamN :: (MonadIO m, Unbox a) =>
24262267 Int -> Stream Identity a -> m (MutArray a)
24272268fromPureStreamN n xs =
2428− D.fold (createOf n) $ D.morphInner (return . runIdentity) xs
2269+ D.fold (writeN n) $ D.morphInner (return . runIdentity) xs
24292270
24302271-- | Convert a pure stream in Identity monad to a mutable array.
24312272{-# INLINABLE fromPureStream #-}
24322273fromPureStream :: (MonadIO m, Unbox a) => Stream Identity a -> m (MutArray a)
24332274fromPureStream xs =
2434− D.fold create $ D.morphInner (return . runIdentity) xs
2275+ D.fold write $ D.morphInner (return . runIdentity) xs
24352276
24362277{-# INLINABLE fromPtrN #-}
24372278fromPtrN :: MonadIO m => Int -> Ptr Word8 -> m (MutArray Word8)
@@ -2440,7 +2281,7 @@ fromPtrN len addr = do
24402281 -- XXX We can implement a stream copy in a similar way by streaming Word64
24412282 -- first and then remaining Word8.
24422283 arr <- new len
2443− _ <- unsafeAsPtr arr
2284+ _ <- asUnpinnedPtrUnsafe arr
24442285 (\ptr -> liftIO $ c_memcpy ptr addr (fromIntegral len))
24452286 return (arr {arrEnd = len})
24462287
@@ -2456,7 +2297,7 @@ fromByteStr# addr = do
24562297 len <- liftIO $ c_strlen (Ptr addr)
24572298 let lenInt = fromIntegral len
24582299 arr <- new lenInt
2459− _ <- unsafeAsPtr arr (\ptr -> liftIO $ c_memcpy ptr (Ptr addr) len)
2300+ _ <- asUnpinnedPtrUnsafe arr (\ptr -> liftIO $ c_memcpy ptr (Ptr addr) len)
24602301 return (arr {arrEnd = lenInt})
24612302
24622303-------------------------------------------------------------------------------
@@ -2515,7 +2356,6 @@ fromChunksK :: (Unbox a, MonadIO m) =>
25152356fromChunksK = fromChunkskAs Unpinned
25162357
25172358{-# DEPRECATED fromArrayStreamK "Please use fromChunksK instead." #-}
2518−{-# INLINE fromArrayStreamK #-}
25192359fromArrayStreamK :: (Unbox a, MonadIO m) =>
25202360 StreamK m (MutArray a) -> m (MutArray a)
25212361fromArrayStreamK = fromChunksK
@@ -2527,7 +2367,7 @@ fromStreamDAs ps m =
25272367 arrayStreamKFromStreamDAs Unpinned m >>= fromChunkskAs ps
25282368
25292369-- | Create an 'Array' from a stream. This is useful when we want to create a
2530−-- single array from a stream of unknown size. 'createOf' is at least twice
2370+-- single array from a stream of unknown size. 'writeN' is at least twice
25312371-- as efficient when the size is already known.
25322372--
25332373-- Note that if the input stream is too large memory allocation for the array
@@ -2539,19 +2379,19 @@ fromStreamDAs ps m =
25392379fromStream :: (MonadIO m, Unbox a) => Stream m a -> m (MutArray a)
25402380fromStream = fromStreamDAs Unpinned
25412381
2542−-- fromStream (Stream m) = P.fold create m
2382+-- fromStream (Stream m) = P.fold write m
25432383-- CAUTION: a very large number (millions) of arrays can degrade performance
25442384-- due to GC overhead because we need to buffer the arrays before we flatten
25452385-- all the arrays.
25462386--
2547−-- XXX Compare if this is faster or "fold create".
2387+-- XXX Compare if this is faster or "fold write".
25482388--
25492389-- | We could take the approach of doubling the memory allocation on each
25502390-- overflow. This would result in more or less the same amount of copying as in
25512391-- the chunking approach. However, if we have to shrink in the end then it may
25522392-- result in an extra copy of the entire data.
25532393--
2554−-- >>> fromStreamD = StreamD.fold MutArray.create
2394+-- >>> fromStreamD = StreamD.fold MutArray.write
25552395--
25562396{-# INLINE fromStreamD #-}
25572397{-# DEPRECATED fromStreamD "Please use fromStream instead." #-}
@@ -2581,10 +2421,6 @@ fromListRev xs = fromListRevN (Prelude.length xs) xs
25812421-- Cloning
25822422-------------------------------------------------------------------------------
25832423
2584−-- Arrays are aligned on 64-bit boundaries. The fastest way to copy an array is
2585−-- to unsafeCast it to Word64, read it, write it to Word64 array and unsafeCast
2586−-- it again. We can use SIMD read/write as well.
2587−
25882424{-# INLINE cloneAs #-}
25892425cloneAs ::
25902426 ( MonadIO m
@@ -2676,7 +2512,7 @@ spliceWith :: forall m a. (MonadIO m, Unbox a) =>
26762512 (Int -> Int -> Int) -> MutArray a -> MutArray a -> m (MutArray a)
26772513spliceWith sizer dst@(MutArray _ start end bound) src = do
26782514{-
2679− let f = appendWith (`sizer` byteLength src) (return dst)
2515+ let f = writeAppendWith (`sizer` byteLength src) (return dst)
26802516 in D.fold f (toStreamD src)
26812517-}
26822518 assert (end <= bound) (return ())
@@ -2744,7 +2580,7 @@ splitOn predicate arr =
27442580{-# INLINE breakOn #-}
27452581breakOn :: MonadIO m
27462582 => Word8 -> MutArray Word8 -> m (MutArray Word8, Maybe (MutArray Word8))
2747−breakOn sep arr@MutArray{..} = unsafeAsPtr arr $ \p -> liftIO $ do
2583+breakOn sep arr@MutArray{..} = asUnpinnedPtrUnsafe arr $ \p -> liftIO $ do
27482584 -- XXX We do not need memchr here, we can use a Haskell equivalent.
27492585 -- Need efficient stream based primitives that work on Word64.
27502586 loc <- c_memchr p sep (fromIntegral $ byteLength arr)
@@ -2767,41 +2603,33 @@ breakOn sep arr@MutArray{..} = unsafeAsPtr arr $ \p -> liftIO $ do
27672603 }
27682604 )
27692605
2770−-- | Like 'splitAt' but does not check whether the index is valid.
2771−--
2772−{-# INLINE unsafeSplitAt #-}
2773−unsafeSplitAt :: forall a. Unbox a =>
2774− Int -> MutArray a -> (MutArray a, MutArray a)
2775−unsafeSplitAt i MutArray{..} =
2776− let off = i * SIZE_OF(a)
2777− p = arrStart + off
2778− in ( MutArray
2779− { arrContents = arrContents
2780− , arrStart = arrStart
2781− , arrEnd = p
2782− , arrBound = p
2783− }
2784− , MutArray
2785− { arrContents = arrContents
2786− , arrStart = p
2787− , arrEnd = arrEnd
2788− , arrBound = arrBound
2789− }
2790− )
2791−
27922606-- | Create two slices of an array without copying the original array. The
27932607-- specified index @i@ is the first index of the second slice.
27942608--
27952609{-# INLINE splitAt #-}
27962610splitAt :: forall a. Unbox a => Int -> MutArray a -> (MutArray a, MutArray a)
2797−splitAt i arr =
2611+splitAt i arr@MutArray{..} =
27982612 let maxIndex = length arr - 1
27992613 in if i < 0
28002614 then error "sliceAt: negative array index"
28012615 else if i > maxIndex
28022616 then error $ "sliceAt: specified array index " ++ show i
28032617 ++ " is beyond the maximum index " ++ show maxIndex
2804− else unsafeSplitAt i arr
2618+ else let off = i * SIZE_OF(a)
2619+ p = arrStart + off
2620+ in ( MutArray
2621+ { arrContents = arrContents
2622+ , arrStart = arrStart
2623+ , arrEnd = p
2624+ , arrBound = p
2625+ }
2626+ , MutArray
2627+ { arrContents = arrContents
2628+ , arrStart = p
2629+ , arrEnd = arrEnd
2630+ , arrBound = arrBound
2631+ }
2632+ )
28052633
28062634-------------------------------------------------------------------------------
28072635-- Casting
@@ -2851,13 +2679,9 @@ cast arr =
28512679-- guarantees that unsafe calls will be performed in the calling thread. Making
28522680-- it safe to pass heap-allocated objects to unsafe functions.
28532681
2854−-- XXX Should we just name it asPtr, the unsafety is implicit for any pointer
2682+-- Should we just name it asPtr, the unsafety is implicit for any pointer
28552683-- operations. And we are safe from Haskell perspective because we will be
28562684-- pinning the memory.
2857−--
2858−-- XXX we cannot pass the length of the ptr here as in some cases it may not be
2859−-- available e.g. a null terminated C string. However, we can create another
2860−-- flavor of the API e.g. asPtrN.
28612685
28622686-- | Use a @MutArray a@ as @Ptr a@. This is useful when we want to pass an
28632687-- array as a pointer to some operating system call or to a "safe" FFI call.
@@ -2875,21 +2699,16 @@ cast arr =
28752699--
28762700-- /Pre-release/
28772701--
2878−{-# INLINE unsafePinnedAsPtr #-}
2879−unsafePinnedAsPtr :: MonadIO m => MutArray a -> (Ptr a -> m b) -> m b
2880−unsafePinnedAsPtr arr f =
2881− Unboxed.unsafePinnedAsPtr
2882− (arrContents arr) (\ptr -> f (ptr `plusPtr` arrStart arr))
2883−
2884−{-# DEPRECATED asPtrUnsafe "Please use unsafePinnedAsPtr instead." #-}
28852702{-# INLINE asPtrUnsafe #-}
28862703asPtrUnsafe :: MonadIO m => MutArray a -> (Ptr a -> m b) -> m b
2887−asPtrUnsafe = unsafePinnedAsPtr
2704+asPtrUnsafe arr f =
2705+ Unboxed.asPtrUnsafe
2706+ (arrContents arr) (\ptr -> f (ptr `plusPtr` arrStart arr))
28882707
2889−{-# INLINE unsafeAsPtr #-}
2890−unsafeAsPtr :: MonadIO m => MutArray a -> (Ptr a -> m b) -> m b
2891−unsafeAsPtr arr f =
2892− Unboxed.unsafeAsPtr
2708+{-# INLINE asUnpinnedPtrUnsafe #-}
2709+asUnpinnedPtrUnsafe :: MonadIO m => MutArray a -> (Ptr a -> m b) -> m b
2710+asUnpinnedPtrUnsafe arr f =
2711+ Unboxed.asUnpinnedPtrUnsafe
28932712 (arrContents arr) (\ptr -> f (ptr `plusPtr` arrStart arr))
28942713
28952714-------------------------------------------------------------------------------
@@ -2951,11 +2770,19 @@ byteEq arr1 arr2 = fmap (EQ ==) $ byteCmp arr1 arr2
29512770-- can leave some memory unused. They can split the last array to fit it
29522771-- exactly in the space.
29532772
2954−{-# INLINE_NORMAL pCompactLeAs #-}
2955−pCompactLeAs ::
2773+-- | Parser @pCompactLE maxElems@ coalesces adjacent arrays in the input stream
2774+-- only if the combined size would be less than or equal to @maxElems@
2775+-- elements. Note that it won't split an array if the original array is already
2776+-- larger than maxElems.
2777+--
2778+-- @maxElems@ must be greater than 0.
2779+--
2780+-- /Internal/
2781+{-# INLINE_NORMAL pCompactLE #-}
2782+pCompactLE ::
29562783 forall m a. (MonadIO m, Unbox a)
2957− => PinnedState -> Int -> Parser (MutArray a) m (MutArray a)
2958−pCompactLeAs ps maxElems = Parser step initial extract
2784+ => Int -> Parser (MutArray a) m (MutArray a)
2785+pCompactLE maxElems = Parser step initial extract
29592786
29602787 where
29612788
@@ -2986,8 +2813,7 @@ pCompactLeAs ps maxElems = Parser step initial extract
29862813 else do
29872814 buf1 <-
29882815 if byteCapacity buf < maxBytes
2989− then liftIO $ reallocExplicitAs
2990− ps (SIZE_OF(a)) maxBytes buf
2816+ then realloc maxBytes buf
29912817 else return buf
29922818 buf2 <- spliceUnsafe buf1 arr
29932819 return $ Parser.Partial 0 (Just buf2)
@@ -2995,30 +2821,6 @@ pCompactLeAs ps maxElems = Parser step initial extract
29952821 extract Nothing = return $ Parser.Done 0 nil
29962822 extract (Just buf) = return $ Parser.Done 0 buf
29972823
2998−-- | Parser @pCompactLE maxElems@ coalesces adjacent arrays in the input stream
2999−-- only if the combined size would be less than or equal to @maxElems@
3000−-- elements. Note that it won't split an array if the original array is already
3001−-- larger than maxElems.
3002−--
3003−-- @maxElems@ must be greater than 0.
3004−--
3005−-- Generates unpinned arrays irrespective of the pinning status of input
3006−-- arrays.
3007−--
3008−-- /Internal/
3009−{-# INLINE pCompactLE #-}
3010−pCompactLE ::
3011− forall m a. (MonadIO m, Unbox a)
3012− => Int -> Parser (MutArray a) m (MutArray a)
3013−pCompactLE = pCompactLeAs Unpinned
3014−
3015−-- | Pinned version of 'pCompactLE'.
3016−{-# INLINE pPinnedCompactLE #-}
3017−pPinnedCompactLE ::
3018− forall m a. (MonadIO m, Unbox a)
3019− => Int -> Parser (MutArray a) m (MutArray a)
3020−pPinnedCompactLE = pCompactLeAs Pinned
3021−
30222824data SpliceState s arr
30232825 = SpliceInitial s
30242826 | SpliceBuffering s arr
@@ -3030,10 +2832,18 @@ data SpliceState s arr
30302832-- immutable array never has additional space so a new array is allocated
30312833-- instead of mutating it.
30322834
3033−{-# INLINE_NORMAL compactLeAs #-}
3034−compactLeAs :: forall m a. (MonadIO m, Unbox a)
3035− => PinnedState -> Int -> D.Stream m (MutArray a) -> D.Stream m (MutArray a)
3036−compactLeAs ps maxElems (D.Stream step state) =
2835+-- | Scan @rCompactLE maxElems@ coalesces adjacent arrays in the input stream
2836+-- only if the combined size would be less than or equal to @maxElems@
2837+-- elements. Note that it won't split an array if the original array is already
2838+-- larger than maxElems.
2839+--
2840+-- @maxElems@ must be greater than 0.
2841+--
2842+-- /Internal/
2843+{-# INLINE_NORMAL rCompactLE #-}
2844+rCompactLE :: forall m a. (MonadIO m, Unbox a)
2845+ => Int -> D.Stream m (MutArray a) -> D.Stream m (MutArray a)
2846+rCompactLE maxElems (D.Stream step state) =
30372847 D.Stream step' (SpliceInitial state)
30382848
30392849 where
@@ -3069,8 +2879,7 @@ compactLeAs ps maxElems (D.Stream step state) =
30692879 D.Skip (SpliceYielding buf (SpliceBuffering s arr))
30702880 else do
30712881 buf1 <- if byteCapacity buf < maxBytes
3072− then liftIO $ reallocExplicitAs
3073− ps (SIZE_OF(a)) maxBytes buf
2882+ then realloc maxBytes buf
30742883 else return buf
30752884 buf2 <- spliceUnsafe buf1 arr
30762885 return $ D.Skip (SpliceBuffering s buf2)
@@ -3081,12 +2890,14 @@ compactLeAs ps maxElems (D.Stream step state) =
30812890
30822891 step' _ (SpliceYielding arr next) = return $ D.Yield arr next
30832892
3084−
3085−{-# INLINE_NORMAL fCompactGeAs #-}
3086−fCompactGeAs ::
2893+-- | Fold @fCompactGE minElems@ coalesces adjacent arrays in the input stream
2894+-- until the size becomes greater than or equal to @minElems@.
2895+--
2896+{-# INLINE_NORMAL fCompactGE #-}
2897+fCompactGE ::
30872898 forall m a. (MonadIO m, Unbox a)
3088− => PinnedState -> Int -> FL.Fold m (MutArray a) (MutArray a)
3089−fCompactGeAs ps minElems = Fold step initial extract extract
2899+ => Int -> FL.Fold m (MutArray a) (MutArray a)
2900+fCompactGE minElems = Fold step initial extract extract
30902901
30912902 where
30922903
@@ -3114,8 +2925,7 @@ fCompactGeAs ps minElems = Fold step initial extract extract
31142925 let len = byteLength buf + byteLength arr
31152926 buf1 <-
31162927 if byteCapacity buf < len
3117− then liftIO $ reallocExplicitAs
3118− ps (SIZE_OF(a)) (max minBytes len) buf
2928+ then realloc (max len minBytes) buf
31192929 else return buf
31202930 buf2 <- spliceUnsafe buf1 arr
31212931 if len >= minBytes
@@ -3125,30 +2935,15 @@ fCompactGeAs ps minElems = Fold step initial extract extract
31252935 extract Nothing = return nil
31262936 extract (Just buf) = return buf
31272937
3128−-- | Fold @fCompactGE minElems@ coalesces adjacent arrays in the input stream
3129−-- until the size becomes greater than or equal to @minElems@.
2938+-- | Like 'compactGE' but for transforming folds instead of stream.
31302939--
3131−-- Generates unpinned arrays irrespective of the pinning status of input
3132−-- arrays.
3133−{-# INLINE fCompactGE #-}
3134−fCompactGE ::
3135− forall m a. (MonadIO m, Unbox a)
3136− => Int -> FL.Fold m (MutArray a) (MutArray a)
3137−fCompactGE = fCompactGeAs Unpinned
3138−
3139−-- | Pinned version of 'fCompactGE'.
3140−{-# INLINE fPinnedCompactGE #-}
3141−fPinnedCompactGE ::
3142− forall m a. (MonadIO m, Unbox a)
3143− => Int -> FL.Fold m (MutArray a) (MutArray a)
3144−fPinnedCompactGE = fCompactGeAs Pinned
3145−
3146−{-# INLINE_NORMAL lCompactGeAs #-}
3147−lCompactGeAs :: forall m a. (MonadIO m, Unbox a)
3148− => PinnedState -> Int -> Fold m (MutArray a) () -> Fold m (MutArray a) ()
3149−-- The fold version turns out to be a little bit slower.
3150−-- lCompactGeAs ps n = FL.many (fCompactGeAs ps n)
3151−lCompactGeAs ps minElems (Fold step1 initial1 _ final1) =
2940+-- >>> lCompactGE n = Fold.many (MutArray.fCompactGE n)
2941+--
2942+{-# INLINE_NORMAL lCompactGE #-}
2943+lCompactGE :: forall m a. (MonadIO m, Unbox a)
2944+ => Int -> Fold m (MutArray a) () -> Fold m (MutArray a) ()
2945+-- lCompactGE n = Fold.many (fCompactGE n)
2946+lCompactGE minElems (Fold step1 initial1 _ final1) =
31522947 Fold step initial extract final
31532948
31542949 where
@@ -3186,8 +2981,7 @@ lCompactGeAs ps minElems (Fold step1 initial1 _ final1) =
31862981 step (Tuple' (Just buf) r1) arr = do
31872982 let len = byteLength buf + byteLength arr
31882983 buf1 <- if byteCapacity buf < len
3189− then liftIO $ reallocExplicitAs
3190− ps (SIZE_OF(a)) (max minBytes len) buf
2984+ then realloc (max minBytes len) buf
31912985 else return buf
31922986 buf2 <- spliceUnsafe buf1 arr
31932987 runInner len r1 buf2
@@ -3207,23 +3001,6 @@ lCompactGeAs ps minElems (Fold step1 initial1 _ final1) =
32073001 FL.Partial rr -> final1 rr
32083002 FL.Done _ -> return ()
32093003
3210−-- | Like 'compactGE' but for transforming folds instead of stream.
3211−--
3212−-- >>> lCompactGE n = Fold.many (MutArray.fCompactGE n)
3213−--
3214−-- Generates unpinned arrays irrespective of the pinning status of input
3215−-- arrays.
3216−{-# INLINE lCompactGE #-}
3217−lCompactGE :: forall m a. (MonadIO m, Unbox a)
3218− => Int -> Fold m (MutArray a) () -> Fold m (MutArray a) ()
3219−lCompactGE = lCompactGeAs Unpinned
3220−
3221−-- | Pinned version of 'lCompactGE'.
3222−{-# INLINE lPinnedCompactGE #-}
3223−lPinnedCompactGE :: forall m a. (MonadIO m, Unbox a)
3224− => Int -> Fold m (MutArray a) () -> Fold m (MutArray a) ()
3225−lPinnedCompactGE = lCompactGeAs Pinned
3226−
32273004-- | @compactGE n stream@ coalesces adjacent arrays in the @stream@ until
32283005-- the size becomes greater than or equal to @n@.
32293006--