@@ -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
@@ -162,9 +164,9 @@ module Streamly.Internal.Data.MutArray.Type
162164 , blockSize
163165 , arrayChunkBytes
164166 , allocBytesToElemCount
165− , realloc -- this is "resize"
166− , grow
167− , growExp
167+ , realloc
168+ , resize
169+ , resizeExp
168170 , rightSize
169171
170172 -- ** Folding
@@ -193,34 +195,24 @@ 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
213215
214− -- ** Serialization using Unbox
215− , pokeAppend
216− , pokeAppendMay
217− , pokeSkipUnsafe -- unsafePokeSkip
218−
219− -- ** Deserialization using Unbox
220− , peekUncons
221− , peekUnconsUnsafe -- unsafePeekUncons
222− , peekSkipUnsafe -- unsafePeekSkip
223−
224216 -- Arrays of arrays
225217 -- We can add dimensionality parameter to the array type to get
226218 -- multidimensional arrays. Multidimensional arrays would just be a
@@ -242,23 +234,18 @@ module Streamly.Internal.Data.MutArray.Type
242234
243235 -- *** Concat
244236 -- | Append the arrays in a stream to form a stream of elements.
245− , concatWith
246− , concatRevWith
247− , concat
248− , concatRev
237+ , concatChunks
238+ , concatChunksRev
249239
250240 -- *** Compact
251241 -- | Append the arrays in a stream to form a stream of larger arrays.
252242 , SpliceState (..)
253− , pCompactLE
254− , pPinnedCompactLE
255− , compactLeAs
256− , fCompactGE
257− , fPinnedCompactGE
258− , lCompactGE
259− , lPinnedCompactGE
260− , compactGE
261− , compactEQ
243+ , pCompactChunksLE
244+ , rCompactChunksLE
245+ , fCompactChunksGE
246+ , lCompactChunksGE
247+ , compactChunksGE
248+ , compactChunksEQ
262249
263250 -- ** Utilities
264251 , roundUpToPower2
@@ -267,7 +254,6 @@ module Streamly.Internal.Data.MutArray.Type
267254 , c_memchr
268255
269256 -- * Deprecated
270− , asPtrUnsafe
271257 , writeChunks
272258 , flattenArrays
273259 , flattenArraysRev
@@ -277,27 +263,6 @@ module Streamly.Internal.Data.MutArray.Type
277263 , cmp
278264 , getIndices
279265 , getIndicesWith
280− , resize
281− , 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
301266 )
302267where
303268
@@ -353,7 +318,7 @@ import qualified Streamly.Internal.Data.StreamK.Type as K
353318import qualified Prelude
354319
355320import Prelude hiding
356− (Foldable(..), concat, read, unlines, splitAt, reverse, truncate)
321+ (Foldable(..), read, unlines, splitAt, reverse, truncate)
357322
358323#include "DocTestDataMutArray.hs"
359324
@@ -479,8 +444,6 @@ isPinned MutArray{..} = Unboxed.isPinned arrContents
479444-- we need more than that. See stg_pinnedNewAlignedByteArrayzh and
480445-- allocatePinned in GHC source.
481446
482−-- XXX Rename to emptyAlignedWith, alignSize should be first arg.
483−
484447-- | @newArrayWith allocator alignment count@ allocates a new array of zero
485448-- length and with a capacity to hold @count@ elements, using @allocator
486449-- size alignment@ as the memory allocator function.
@@ -504,25 +467,12 @@ newArrayWith alloc alignSize count = do
504467 , arrBound = size
505468 }
506469
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." #-}
520470nil ::
521471#ifdef DEVBUILD
522472 Unbox a =>
523473#endif
524474 MutArray a
525−nil = empty
475+nil = MutArray Unboxed.nil 0 0 0
526476
527477{-# INLINE newBytesAs #-}
528478newBytesAs :: MonadIO m =>
@@ -539,13 +489,12 @@ newBytesAs ps bytes = do
539489 , arrBound = bytes
540490 }
541491
542−-- | Allocates a pinned empty array that with a reserved capacity of bytes.
543−-- The memory of the array is uninitialized and the allocation is aligned as
544−-- per the 'Unboxed' instance of the type.
492+-- | Allocates a pinned empty array that can hold 'count' items. The memory of
493+-- the array is uninitialized and the allocation is aligned as per the
494+-- 'Unboxed' instance of the type.
545495--
546496-- /Pre-release/
547497{-# INLINE pinnedNewBytes #-}
548−{-# DEPRECATED pinnedNewBytes "Please use pinnedEmptyOf with appropriate calculation" #-}
549498pinnedNewBytes :: MonadIO m =>
550499#ifdef DEVBUILD
551500 Unbox a =>
@@ -570,31 +519,21 @@ newAs ps =
570519 (error "new: alignment is not used in unpinned arrays.")
571520
572521-- 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." #-}
522+--
523+-- | Allocates an empty pinned array that can hold 'count' items. The memory of
524+-- the array is uninitialized and the allocation is aligned as per the 'Unboxed'
525+-- instance of the type.
526+--
582527{-# INLINE pinnedNew #-}
583528pinnedNew :: forall m a. (MonadIO m, Unbox a) => Int -> m (MutArray a)
584−pinnedNew = pinnedEmptyOf
529+pinnedNew = newAs Pinned
585530
586−-- | Allocates an unpinned array of zero length but growable to the specified
587−-- capacity without reallocation.
531+-- | Allocates an empty unpinned array that can hold 'count' items. The memory
532+-- of the array is uninitialized.
588533--
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." #-}
595534{-# INLINE new #-}
596535new :: (MonadIO m, Unbox a) => Int -> m (MutArray a)
597−new = emptyOf
536+new = newAs Unpinned
598537
599538-------------------------------------------------------------------------------
600539-- Random writes
@@ -642,7 +581,7 @@ putIndices arr = FL.foldlM' step (return ())
642581
643582 where
644583
645− step () (i, x) = putIndex i arr x
584+ step () (i, x) = liftIO (putIndex i arr x)
646585
647586-- | Modify a given index of an array using a modifier function.
648587--
@@ -838,19 +777,22 @@ arrayChunkBytes = 1024
838777roundDownTo :: Int -> Int -> Int
839778roundDownTo elemSize size = size - (size `mod` elemSize)
840779
780+-- XXX See if resizing can be implemented by reading the old array as a stream
781+-- and then using writeN to the new array.
782+--
841783-- NOTE: we are passing elemSize explicitly to avoid an Unboxed constraint.
842784-- Since this is not inlined Unboxed consrraint leads to dictionary passing
843785-- which complicates some inspection tests.
844786--
845−{-# NOINLINE reallocExplicitAs #-}
846−reallocExplicitAs :: PinnedState -> Int -> Int -> MutArray a -> IO (MutArray a)
847−reallocExplicitAs ps elemSize newCapacityInBytes MutArray{..} = do
787+{-# NOINLINE reallocExplicit #-}
788+reallocExplicit :: Int -> Int -> MutArray a -> IO (MutArray a)
789+reallocExplicit elemSize newCapacityInBytes MutArray{..} = do
848790 assertM(arrEnd <= arrBound)
849791
850792 -- Allocate new array
851793 let newCapMaxInBytes = roundUpLargeArray newCapacityInBytes
852794 contents <-
853− if ps == Pinned
795+ if Unboxed.isPinned arrContents
854796 then Unboxed.pinnedNew newCapMaxInBytes
855797 else Unboxed.new newCapMaxInBytes
856798 let !(MutByteArray mbarrFrom#) = arrContents
@@ -875,10 +817,6 @@ reallocExplicitAs ps elemSize newCapacityInBytes MutArray{..} = do
875817 , arrBound = newCapInBytes
876818 }
877819
878−-- XXX Should these be called reallocBytes etc?
879−-- XXX We may also need reallocAs to allocate as pinned/unpinned explicitly. In
880−-- fact clone/pinnedClone can be implemented using reallocAs.
881−
882820-- | @realloc newCapacity array@ reallocates the array to the specified
883821-- capacity in bytes.
884822--
@@ -890,12 +828,7 @@ reallocExplicitAs ps elemSize newCapacityInBytes MutArray{..} = do
890828-- If the original array is pinned, the newly allocated array is also pinned.
891829{-# INLINABLE realloc #-}
892830realloc :: 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
831+realloc bytes arr = liftIO $ reallocExplicit (SIZE_OF(a)) bytes arr
899832
900833-- | @reallocWith label capSizer minIncrBytes array@. The label is used
901834-- in error messages and the capSizer is used to determine the capacity of the
@@ -918,14 +851,14 @@ reallocWith label capSizer minIncrBytes arr = do
918851 where
919852
920853 badSize newSize =
921− Prelude.concat
854+ concat
922855 [ label
923856 , ": new array size (in bytes) is less than required size "
924857 , show newSize
925858 , ". Please check the sizing function passed."
926859 ]
927860
928−-- | @grow newCapacity array@ changes the total capacity of the array so that
861+-- | @resize newCapacity array@ changes the total capacity of the array so that
929862-- it is enough to hold the specified number of elements. Nothing is done if
930863-- the specified capacity is less than the length of the array.
931864--
@@ -933,46 +866,34 @@ reallocWith label capSizer minIncrBytes arr = do
933866-- the block size (4K).
934867--
935868-- /Pre-release/
936−{-# INLINE grow #-}
937−grow :: forall m a. (MonadIO m, Unbox a) =>
869+{-# INLINE resize #-}
870+resize :: forall m a. (MonadIO m, Unbox a) =>
938871 Int -> MutArray a -> m (MutArray a)
939−grow nElems arr@MutArray{..} = do
872+resize nElems arr@MutArray{..} = do
940873 let req = SIZE_OF(a) * nElems
941− cap = arrBound - arrStart
942− if req < cap
874+ len = arrEnd - arrStart
875+ if req < len
943876 then return arr
944877 else realloc req arr
945878
946−{-# DEPRECATED resize "Please use grow instead." #-}
947−{-# INLINE resize #-}
948−resize :: forall m a. (MonadIO m, Unbox a) =>
949− Int -> MutArray a -> m (MutArray a)
950−resize = grow
951−
952−-- | Like 'grow' but if the requested byte capacity is more than
953−-- 'largeObjectThreshold' then it is rounded up to the closest power of 2.
879+-- | Like 'resize' but if the byte capacity is more than 'largeObjectThreshold'
880+-- then it is rounded up to the closest power of 2.
954881--
955882-- /Pre-release/
956−{-# INLINE growExp #-}
957−growExp :: forall m a. (MonadIO m, Unbox a) =>
883+{-# INLINE resizeExp #-}
884+resizeExp :: forall m a. (MonadIO m, Unbox a) =>
958885 Int -> MutArray a -> m (MutArray a)
959−growExp nElems arr@MutArray{..} = do
886+resizeExp nElems arr@MutArray{..} = do
960887 let req = roundUpLargeArray (SIZE_OF(a) * nElems)
961888 req1 =
962889 if req > largeObjectThreshold
963890 then roundUpToPower2 req
964891 else req
965− cap = arrBound - arrStart
966− if req1 < cap
892+ len = arrEnd - arrStart
893+ if req1 < len
967894 then return arr
968895 else realloc req1 arr
969896
970−{-# DEPRECATED resizeExp "Please use growExp instead." #-}
971−{-# INLINE resizeExp #-}
972−resizeExp :: forall m a. (MonadIO m, Unbox a) =>
973− Int -> MutArray a -> m (MutArray a)
974−resizeExp = growExp
975−
976897-- | Resize the allocated memory to drop any reserved free space at the end of
977898-- the array and reallocate it to reduce wastage.
978899--
@@ -1037,7 +958,7 @@ snocUnsafe arr@MutArray{..} = snocNewEnd (INDEX_NEXT(arrEnd,a)) arr
1037958{-# INLINE snocMay #-}
1038959snocMay :: forall m a. (MonadIO m, Unbox a) =>
1039960 MutArray a -> a -> m (Maybe (MutArray a))
1040−snocMay arr@MutArray{..} x = do
961+snocMay arr@MutArray{..} x = liftIO $ do
1041962 let newEnd = INDEX_NEXT(arrEnd,a)
1042963 if newEnd <= arrBound
1043964 then Just <$> snocNewEnd newEnd arr x
@@ -1051,7 +972,7 @@ snocWithRealloc :: forall m a. (MonadIO m, Unbox a) =>
1051972 -> a
1052973 -> m (MutArray a)
1053974snocWithRealloc sizer arr x = do
1054− arr1 <- reallocWith "snocWith" sizer (SIZE_OF(a)) arr
975+ arr1 <- liftIO $ reallocWith "snocWith" sizer (SIZE_OF(a)) arr
1055976 snocUnsafe arr1 x
1056977
1057978-- | @snocWith sizer arr elem@ mutates @arr@ to append @elem@. The length of
@@ -1073,7 +994,7 @@ snocWith :: forall m a. (MonadIO m, Unbox a) =>
1073994 -> MutArray a
1074995 -> a
1075996 -> m (MutArray a)
1076−snocWith allocSize arr x = do
997+snocWith allocSize arr x = liftIO $ do
1077998 let newEnd = INDEX_NEXT(arrEnd arr,a)
1078999 if newEnd <= arrBound arr
10791000 then snocNewEnd newEnd arr x
@@ -1117,126 +1038,6 @@ snoc = snocWith f
11171038 then oldSize * 2
11181039 else roundUpToPower2 oldSize * 2
11191040
1120−-------------------------------------------------------------------------------
1121−-- Serialization/Deserialization
1122−-------------------------------------------------------------------------------
1123−
1124−{-# INLINE pokeNewEnd #-}
1125−pokeNewEnd :: (MonadIO m, Unbox a) =>
1126− Int -> MutArray Word8 -> a -> m (MutArray Word8)
1127−pokeNewEnd newEnd arr@MutArray{..} x = liftIO $ do
1128− assert (newEnd <= arrBound) (return ())
1129− liftIO $ pokeAt arrEnd arrContents x
1130− return $ arr {arrEnd = newEnd}
1131−
1132−-- | Really really unsafe, unboxes a Haskell type and appends the resulting
1133−-- bytes to the byte array, may cause silent data corruption or if you are
1134−-- lucky a segfault if the array does not have enough space to append the
1135−-- element.
1136−--
1137−-- /Internal/
1138−{-# INLINE pokeAppendUnsafe #-}
1139−pokeAppendUnsafe :: forall m a. (MonadIO m, Unbox a) =>
1140− MutArray Word8 -> a -> m (MutArray Word8)
1141−pokeAppendUnsafe arr@MutArray{..} = pokeNewEnd (arrEnd + SIZE_OF(a)) arr
1142−
1143−-- | Skip the specified number of bytes in the array. The data in the skipped
1144−-- region remains uninitialzed.
1145−{-# INLINE pokeSkipUnsafe #-}
1146−pokeSkipUnsafe :: Int -> MutArray Word8 -> MutArray Word8
1147−pokeSkipUnsafe n arr@MutArray{..} = do
1148− let newEnd = arrEnd + n
1149− in assert (newEnd <= arrBound) (arr {arrEnd = newEnd})
1150−
1151−-- | Like 'pokeAppend' but does not grow the array when pre-allocated array
1152−-- capacity becomes full.
1153−--
1154−-- /Internal/
1155−{-# INLINE pokeAppendMay #-}
1156−pokeAppendMay :: forall m a. (MonadIO m, Unbox a) =>
1157− MutArray Word8 -> a -> m (Maybe (MutArray Word8))
1158−pokeAppendMay arr@MutArray{..} x = liftIO $ do
1159− let newEnd = arrEnd + SIZE_OF(a)
1160− if newEnd <= arrBound
1161− then Just <$> pokeNewEnd newEnd arr x
1162− else return Nothing
1163−
1164−{-# NOINLINE pokeWithRealloc #-}
1165−pokeWithRealloc :: forall m a. (MonadIO m, Unbox a) =>
1166− (Int -> Int)
1167− -> MutArray Word8
1168− -> a
1169− -> m (MutArray Word8)
1170−pokeWithRealloc sizer arr x = do
1171− arr1 <- liftIO $ reallocWith "pokeWithRealloc" sizer (SIZE_OF(a)) arr
1172− pokeAppendUnsafe arr1 x
1173−
1174−{-# INLINE pokeAppendWith #-}
1175−pokeAppendWith :: forall m a. (MonadIO m, Unbox a) =>
1176− (Int -> Int)
1177− -> MutArray Word8
1178− -> a
1179− -> m (MutArray Word8)
1180−pokeAppendWith allocSize arr x = liftIO $ do
1181− let newEnd = arrEnd arr + SIZE_OF(a)
1182− if newEnd <= arrBound arr
1183− then pokeNewEnd newEnd arr x
1184− else pokeWithRealloc allocSize arr x
1185−
1186−-- | Unbox a Haskell type and append the resulting bytes to a mutable byte
1187−-- array. The array is grown exponentially when more space is needed.
1188−--
1189−-- Definition:
1190−--
1191−-- >>> pokeAppend arr x = MutArray.castUnsafe <$> MutArray.snoc (MutArray.castUnsafe arr) x
1192−--
1193−{-# INLINE pokeAppend #-}
1194−pokeAppend :: forall m a. (MonadIO m, Unbox a) =>
1195− MutArray Word8 -> a -> m (MutArray Word8)
1196−pokeAppend = pokeAppendWith f
1197−
1198− where
1199−
1200− f oldSize =
1201− if isPower2 oldSize
1202− then oldSize * 2
1203− else roundUpToPower2 oldSize * 2
1204−
1205−-- | Really really unsafe, create a Haskell value from an unboxed byte array,
1206−-- does not check if the array is big enough, may return garbage or if you are
1207−-- lucky may cause a segfault.
1208−--
1209−-- /Internal/
1210−{-# INLINE peekUnconsUnsafe #-}
1211−peekUnconsUnsafe :: forall m a. (MonadIO m, Unbox a) =>
1212− MutArray Word8 -> m (a, MutArray Word8)
1213−peekUnconsUnsafe MutArray{..} = do
1214− let start1 = arrStart + SIZE_OF(a)
1215− assert (start1 <= arrEnd) (return ())
1216− liftIO $ do
1217− r <- peekAt arrStart arrContents
1218− return (r, MutArray arrContents start1 arrEnd arrBound)
1219−
1220−-- | Discard the specified number of bytes in the array.
1221−{-# INLINE peekSkipUnsafe #-}
1222−peekSkipUnsafe :: Int -> MutArray Word8 -> MutArray Word8
1223−peekSkipUnsafe n MutArray{..} =
1224− let start1 = arrStart + n
1225− in assert (start1 <= arrEnd) (MutArray arrContents start1 arrEnd arrBound)
1226−
1227−-- | Create a Haskell value from its unboxed representation from the head of a
1228−-- byte array, return the value and the remaining array.
1229−{-# INLINE peekUncons #-}
1230−peekUncons :: forall m a. (MonadIO m, Unbox a) =>
1231− MutArray Word8 -> m (Maybe a, MutArray Word8)
1232−peekUncons arr@MutArray{..} = do
1233− let start1 = arrStart + SIZE_OF(a)
1234− if start1 > arrEnd
1235− then return (Nothing, arr)
1236− else liftIO $ do
1237− r <- peekAt arrStart arrContents
1238− return (Just r, MutArray arrContents start1 arrEnd arrBound)
1239−
12401041-------------------------------------------------------------------------------
12411042-- Random reads
12421043-------------------------------------------------------------------------------
@@ -1302,7 +1103,6 @@ indexReaderWith liftio (D.Stream stepi sti) = Unfold step inject
13021103 D.Stop -> return D.Stop
13031104
13041105{-# DEPRECATED getIndicesWith "Please use indexReaderWith instead." #-}
1305−{-# INLINE getIndicesWith #-}
13061106getIndicesWith :: (Monad m, Unbox a) =>
13071107 (forall b. IO b -> m b) -> D.Stream m Int -> Unfold m (MutArray a) a
13081108getIndicesWith = indexReaderWith
@@ -1314,11 +1114,10 @@ getIndicesWith = indexReaderWith
13141114-- /Pre-release/
13151115{-# INLINE indexReader #-}
13161116indexReader :: (MonadIO m, Unbox a) => Stream m Int -> Unfold m (MutArray a) a
1317−indexReader = indexReaderWith liftIO
1117+indexReader = getIndicesWith liftIO
13181118
13191119-- XXX DO NOT REMOVE, change the signature to use Stream instead of unfold
13201120{-# DEPRECATED getIndices "Please use indexReader instead." #-}
1321−{-# INLINE getIndices #-}
13221121getIndices :: (MonadIO m, Unbox a) => Stream m Int -> Unfold m (MutArray a) a
13231122getIndices = indexReader
13241123
@@ -1615,7 +1414,7 @@ chunksOfAs ps n (D.Stream step state) =
16151414--
16161415-- Same as the following but may be more efficient:
16171416--
1618−-- >>> chunksOf n = Stream.foldMany (MutArray.createOf n)
1417+-- >>> chunksOf n = Stream.foldMany (MutArray.writeN n)
16191418--
16201419-- /Pre-release/
16211420{-# INLINE_NORMAL chunksOf #-}
@@ -1624,14 +1423,14 @@ chunksOf :: forall m a. (MonadIO m, Unbox a)
16241423-- XXX the idiomatic implementation leads to large regression in the D.reverse'
16251424-- benchmark. It seems it has difficulty producing optimized code when
16261425-- converting to StreamK. Investigate GHC optimizations.
1627−-- chunksOf n = D.foldMany (createOf n)
1426+-- chunksOf n = D.foldMany (writeN n)
16281427chunksOf = chunksOfAs Unpinned
16291428
16301429-- | Like 'chunksOf' but creates pinned arrays.
16311430{-# INLINE_NORMAL pinnedChunksOf #-}
16321431pinnedChunksOf :: forall m a. (MonadIO m, Unbox a)
16331432 => Int -> D.Stream m a -> D.Stream m (MutArray a)
1634−-- pinnedChunksOf n = D.foldMany (pinnedCreateOf n)
1433+-- pinnedChunksOf n = D.foldMany (pinnedWriteN n)
16351434pinnedChunksOf = chunksOfAs Pinned
16361435
16371436-- | When we are buffering a stream of unknown size into an array we do not
@@ -1664,10 +1463,16 @@ data FlattenState s contents a =
16641463 OuterLoop s
16651464 | InnerLoop s contents !Int !Int
16661465
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)
1466+-- | Use the "reader" unfold instead.
1467+--
1468+-- @concatChunks = unfoldMany reader@
1469+--
1470+-- We can try this if there are any fusion issues in the unfold.
1471+--
1472+{-# INLINE_NORMAL concatChunks #-}
1473+concatChunks :: forall m a. (MonadIO m, Unbox a)
1474+ => D.Stream m (MutArray a) -> D.Stream m a
1475+concatChunks (D.Stream step state) = D.Stream step' (OuterLoop state)
16711476
16721477 where
16731478
@@ -1684,30 +1489,24 @@ concatWith liftio (D.Stream step state) = D.Stream step' (OuterLoop state)
16841489 return $ D.Skip $ OuterLoop st
16851490
16861491 step' _ (InnerLoop st contents p end) = do
1687− x <- liftio $ peekAt p contents
1492+ x <- liftIO $ peekAt p contents
16881493 return $ D.Yield x (InnerLoop st contents (INDEX_NEXT(p,a)) end)
16891494
1690−-- | Use the "reader" unfold instead.
1495+{-# DEPRECATED flattenArrays "Please use \"unfoldMany reader\" instead." #-}
1496+flattenArrays :: forall m a. (MonadIO m, Unbox a)
1497+ => D.Stream m (MutArray a) -> D.Stream m a
1498+flattenArrays = concatChunks
1499+
1500+-- | Use the "readerRev" unfold instead.
16911501--
1692−-- @concat = unfoldMany reader@
1502+-- @concatChunks = unfoldMany readerRev@
16931503--
16941504-- We can try this if there are any fusion issues in the unfold.
16951505--
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−
1701−{-# DEPRECATED flattenArrays "Please use \"unfoldMany reader\" instead." #-}
1702−{-# INLINE flattenArrays #-}
1703−flattenArrays :: forall m a. (MonadIO m, Unbox a)
1506+{-# INLINE_NORMAL concatChunksRev #-}
1507+concatChunksRev :: forall m a. (MonadIO m, Unbox a)
17041508 => D.Stream m (MutArray a) -> D.Stream m a
1705−flattenArrays = concat
1706−
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)
1509+concatChunksRev (D.Stream step state) = D.Stream step' (OuterLoop state)
17111510
17121511 where
17131512
@@ -1725,26 +1524,14 @@ concatRevWith liftio (D.Stream step state) = D.Stream step' (OuterLoop state)
17251524 return $ D.Skip $ OuterLoop st
17261525
17271526 step' _ (InnerLoop st contents p start) = do
1728− x <- liftio $ peekAt p contents
1527+ x <- liftIO $ peekAt p contents
17291528 let cur = INDEX_PREV(p,a)
17301529 return $ D.Yield x (InnerLoop st contents cur start)
17311530
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−
17431531{-# DEPRECATED flattenArraysRev "Please use \"unfoldMany readerRev\" instead." #-}
1744−{-# INLINE flattenArraysRev #-}
17451532flattenArraysRev :: forall m a. (MonadIO m, Unbox a)
17461533 => D.Stream m (MutArray a) -> D.Stream m a
1747−flattenArraysRev = concatRev
1534+flattenArraysRev = concatChunksRev
17481535
17491536-------------------------------------------------------------------------------
17501537-- Unfolds
@@ -1973,7 +1760,7 @@ foldr f z arr = D.foldr f z $ read arr
19731760
19741761-- XXX Keep the bound intact to not lose any free space? Perf impact?
19751762
1976−-- | @unsafeAppendN n arr@ appends up to @n@ input items to the supplied
1763+-- | @writeAppendNUnsafe n arr@ appends up to @n@ input items to the supplied
19771764-- array.
19781765--
19791766-- Unsafe: Do not drive the fold beyond @n@ elements, it will lead to memory
@@ -1982,12 +1769,13 @@ foldr f z arr = D.foldr f z $ read arr
19821769-- Any free space left in the array after appending @n@ elements is lost.
19831770--
19841771-- /Internal/
1985−{-# INLINE_NORMAL unsafeAppendN #-}
1986−unsafeAppendN :: forall m a. (MonadIO m, Unbox a) =>
1772+{-# INLINE_NORMAL writeAppendNUnsafe #-}
1773+writeAppendNUnsafe :: forall m a. (MonadIO m, Unbox a) =>
19871774 Int
19881775 -> m (MutArray a)
19891776 -> Fold m a (MutArray a)
1990−unsafeAppendN n action = fmap fromArrayUnsafe $ FL.foldlM' step initial
1777+writeAppendNUnsafe n action =
1778+ fmap fromArrayUnsafe $ FL.foldlM' step initial
19911779
19921780 where
19931781
@@ -2000,7 +1788,7 @@ unsafeAppendN n action = fmap fromArrayUnsafe $ FL.foldlM' step initial
20001788 -- otherwise we lose that space.
20011789 arr1 <-
20021790 if free < needed
2003− then noinline reallocWith "unsafeAppendN" (+ needed) needed arr
1791+ then noinline reallocWith "writeAppendNUnsafeWith" (+ needed) needed arr
20041792 else return arr
20051793 return $ toArrayUnsafe arr1
20061794
@@ -2008,51 +1796,30 @@ unsafeAppendN n action = fmap fromArrayUnsafe $ FL.foldlM' step initial
20081796 liftIO $ pokeAt end contents x
20091797 return $ ArrayUnsafe contents start (INDEX_NEXT(end,a))
20101798
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−
20191799-- | Append @n@ elements to an existing array. Any free space left in the array
20201800-- after appending @n@ elements is lost.
20211801--
2022−-- >>> appendN n initial = Fold.take n (MutArray.unsafeAppendN n initial)
1802+-- >>> writeAppendN n initial = Fold.take n (MutArray.writeAppendNUnsafe n initial)
20231803--
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 #-}
1804+{-# INLINE_NORMAL writeAppendN #-}
20321805writeAppendN :: forall m a. (MonadIO m, Unbox a) =>
20331806 Int -> m (MutArray a) -> Fold m a (MutArray a)
2034−writeAppendN = appendN
1807+writeAppendN n initial = FL.take n (writeAppendNUnsafe n initial)
20351808
2036−-- | @appendWith realloc action@ mutates the array generated by @action@ to
1809+-- | @writeAppendWith realloc action@ mutates the array generated by @action@ to
20371810-- append the input stream. If there is no reserved space available in the
20381811-- array it is reallocated to a size in bytes determined by @realloc oldSize@,
20391812-- where @oldSize@ is the current size of the array in bytes.
20401813--
20411814-- Note that the returned array may be a mutated version of original array.
20421815--
2043−-- >>> appendWith sizer = Fold.foldlM' (MutArray.snocWith sizer)
1816+-- >>> writeAppendWith sizer = Fold.foldlM' (MutArray.snocWith sizer)
20441817--
20451818-- /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." #-}
20521819{-# INLINE writeAppendWith #-}
20531820writeAppendWith :: forall m a. (MonadIO m, Unbox a) =>
20541821 (Int -> Int) -> m (MutArray a) -> Fold m a (MutArray a)
2055−writeAppendWith = appendWith
1822+writeAppendWith sizer = FL.foldlM' (snocWith sizer)
20561823
20571824-- | @append action@ mutates the array generated by @action@ to append the
20581825-- input stream. If there is no reserved space available in the array it is
@@ -2060,33 +1827,26 @@ writeAppendWith = appendWith
20601827--
20611828-- Note that the returned array may be a mutated version of original array.
20621829--
2063−-- >>> append = MutArray.appendWith (* 2)
1830+-- >>> writeAppend = MutArray.writeAppendWith (* 2)
20641831--
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." #-}
20721832{-# INLINE writeAppend #-}
20731833writeAppend :: forall m a. (MonadIO m, Unbox a) =>
20741834 m (MutArray a) -> Fold m a (MutArray a)
2075−writeAppend = append
1835+writeAppend = writeAppendWith (* 2)
20761836
20771837-- XXX We can carry bound as well in the state to make sure we do not lose the
20781838-- remaining capacity. Need to check perf impact.
20791839--
2080−-- | Like 'unsafeCreateOf' but takes a new array allocator @alloc size@
2081−-- function as argument.
1840+-- | Like 'writeNUnsafe' but takes a new array allocator @alloc size@ function
1841+-- as argument.
20821842--
2083−-- >>> unsafeCreateOfWith alloc n = MutArray.unsafeAppendN (alloc n) n
1843+-- >>> writeNWithUnsafe alloc n = MutArray.writeAppendNUnsafe (alloc n) n
20841844--
20851845-- /Pre-release/
2086−{-# INLINE_NORMAL unsafeCreateOfWith #-}
2087−unsafeCreateOfWith :: forall m a. (MonadIO m, Unbox a)
1846+{-# INLINE_NORMAL writeNWithUnsafe #-}
1847+writeNWithUnsafe :: forall m a. (MonadIO m, Unbox a)
20881848 => (Int -> m (MutArray a)) -> Int -> Fold m a (MutArray a)
2089−unsafeCreateOfWith alloc n = fromArrayUnsafe <$> FL.foldlM' step initial
1849+writeNWithUnsafe alloc n = fromArrayUnsafe <$> FL.foldlM' step initial
20901850
20911851 where
20921852
@@ -2097,64 +1857,40 @@ unsafeCreateOfWith alloc n = fromArrayUnsafe <$> FL.foldlM' step initial
20971857 return
20981858 $ ArrayUnsafe contents start (INDEX_NEXT(end,a))
20991859
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−
21061860{-# INLINE_NORMAL writeNUnsafeAs #-}
21071861writeNUnsafeAs :: forall m a. (MonadIO m, Unbox a)
21081862 => PinnedState -> Int -> Fold m a (MutArray a)
2109−writeNUnsafeAs ps = unsafeCreateOfWith (newAs ps)
1863+writeNUnsafeAs ps = writeNWithUnsafe (newAs ps)
21101864
2111−-- | Like 'createOf' but does not check the array bounds when writing. The fold
1865+-- | Like 'writeN' but does not check the array bounds when writing. The fold
21121866-- driver must not call the step function more than 'n' times otherwise it will
21131867-- corrupt the memory and crash. This function exists mainly because any
21141868-- conditional in the step function blocks fusion causing 10x performance
21151869-- slowdown.
21161870--
2117−-- >>> unsafeCreateOf = MutArray.unsafeCreateOfWith MutArray.emptyOf
1871+-- >>> writeNUnsafe = MutArray.writeNWithUnsafe MutArray.new
21181872--
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 #-}
1873+{-# INLINE_NORMAL writeNUnsafe #-}
21261874writeNUnsafe :: forall m a. (MonadIO m, Unbox a)
21271875 => 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
1876+writeNUnsafe = writeNUnsafeAs Unpinned
21351877
2136−{-# DEPRECATED pinnedWriteNUnsafe "Please use unsafePinnedCreateOf instead." #-}
2137−{-# INLINE pinnedWriteNUnsafe #-}
1878+-- | Like 'writeNUnsafe' but creates a pinned array.
1879+{-# INLINE_NORMAL pinnedWriteNUnsafe #-}
21381880pinnedWriteNUnsafe :: forall m a. (MonadIO m, Unbox a)
21391881 => Int -> Fold m a (MutArray a)
2140−pinnedWriteNUnsafe = unsafePinnedCreateOf
1882+pinnedWriteNUnsafe = writeNUnsafeAs Pinned
21411883
2142−-- | @createOfWith alloc n@ folds a maximum of @n@ elements into an array
1884+-- | @writeNWith alloc n@ folds a maximum of @n@ elements into an array
21431885-- allocated using the @alloc@ function.
21441886--
2145−-- >>> createOfWith alloc n = Fold.take n (MutArray.unsafeCreateOfWith alloc n)
2146−-- >>> createOfWith alloc n = MutArray.appendN (alloc n) n
1887+-- >>> writeNWith alloc n = Fold.take n (MutArray.writeNWithUnsafe alloc n)
1888+-- >>> writeNWith alloc n = MutArray.writeAppendN (alloc n) n
21471889--
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 #-}
1890+{-# INLINE_NORMAL writeNWith #-}
21551891writeNWith :: forall m a. (MonadIO m, Unbox a)
21561892 => (Int -> m (MutArray a)) -> Int -> Fold m a (MutArray a)
2157−writeNWith = createOfWith
1893+writeNWith alloc n = FL.take n (writeNWithUnsafe alloc n)
21581894
21591895{-# INLINE_NORMAL writeNAs #-}
21601896writeNAs ::
@@ -2162,42 +1898,28 @@ writeNAs ::
21621898 => PinnedState
21631899 -> Int
21641900 -> Fold m a (MutArray a)
2165−writeNAs ps = createOfWith (newAs ps)
1901+writeNAs ps = writeNWith (newAs ps)
21661902
2167−-- | @createOf n@ folds a maximum of @n@ elements from the input stream to an
1903+-- | @writeN n@ folds a maximum of @n@ elements from the input stream to an
21681904-- 'MutArray'.
21691905--
2170−-- >>> createOf = MutArray.createOfWith MutArray.new
2171−-- >>> createOf n = Fold.take n (MutArray.unsafeCreateOf n)
2172−-- >>> createOf n = MutArray.appendN n (MutArray.emptyOf n)
1906+-- >>> writeN = MutArray.writeNWith MutArray.new
1907+-- >>> writeN n = Fold.take n (MutArray.writeNUnsafe n)
1908+-- >>> writeN n = MutArray.writeAppendN n (MutArray.new n)
21731909--
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 #-}
1910+{-# INLINE_NORMAL writeN #-}
21811911writeN :: 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
1912+writeN = writeNAs Unpinned
21911913
2192−{-# DEPRECATED pinnedWriteN "Please use pinnedCreateOf instead." #-}
2193−{-# INLINE pinnedWriteN #-}
1914+-- | Like 'writeN' but creates a pinned array.
1915+{-# INLINE_NORMAL pinnedWriteN #-}
21941916pinnedWriteN ::
21951917 forall m a. (MonadIO m, Unbox a)
21961918 => Int
21971919 -> Fold m a (MutArray a)
2198−pinnedWriteN = pinnedCreateOf
1920+pinnedWriteN = writeNAs Pinned
21991921
2200−-- | Like unsafeCreateOfWith but writes the array in reverse order.
1922+-- | Like writeNWithUnsafe but writes the array in reverse order.
22011923--
22021924-- /Internal/
22031925{-# INLINE_NORMAL writeRevNWithUnsafe #-}
@@ -2218,7 +1940,7 @@ writeRevNWithUnsafe alloc n = fromArrayUnsafe <$> FL.foldlM' step initial
22181940 return
22191941 $ ArrayUnsafe contents ptr end
22201942
2221−-- | Like createOfWith but writes the array in reverse order.
1943+-- | Like writeNWith but writes the array in reverse order.
22221944--
22231945-- /Internal/
22241946{-# INLINE_NORMAL writeRevNWith #-}
@@ -2226,36 +1948,31 @@ writeRevNWith :: forall m a. (MonadIO m, Unbox a)
22261948 => (Int -> m (MutArray a)) -> Int -> Fold m a (MutArray a)
22271949writeRevNWith alloc n = FL.take n (writeRevNWithUnsafe alloc n)
22281950
2229−-- | Like 'createOf' but writes the array in reverse order.
1951+-- | Like writeN but writes the array in reverse order.
22301952--
22311953-- /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 #-}
1954+{-# INLINE_NORMAL writeRevN #-}
22381955writeRevN :: forall m a. (MonadIO m, Unbox a) => Int -> Fold m a (MutArray a)
2239−writeRevN = revCreateOf
1956+writeRevN = writeRevNWith new
22401957
22411958-- | @pinnedWriteNAligned align n@ folds a maximum of @n@ elements from the
22421959-- input stream to a 'MutArray' aligned to the given size.
22431960--
2244−-- >>> pinnedWriteNAligned align = MutArray.createOfWith (MutArray.pinnedNewAligned align)
2245−-- >>> pinnedWriteNAligned align n = MutArray.appendN n (MutArray.pinnedNewAligned align n)
1961+-- >>> pinnedWriteNAligned align = MutArray.writeNWith (MutArray.pinnedNewAligned align)
1962+-- >>> pinnedWriteNAligned align n = MutArray.writeAppendN n (MutArray.pinnedNewAligned align n)
22461963--
22471964-- /Pre-release/
22481965--
22491966{-# INLINE_NORMAL pinnedWriteNAligned #-}
22501967pinnedWriteNAligned :: forall m a. (MonadIO m, Unbox a)
22511968 => Int -> Int -> Fold m a (MutArray a)
2252−pinnedWriteNAligned align = createOfWith (pinnedNewAligned align)
1969+pinnedWriteNAligned align = writeNWith (pinnedNewAligned align)
22531970
22541971-- XXX Buffer to a list instead?
22551972
22561973-- | Buffer a stream into a stream of arrays.
22571974--
2258−-- >>> buildChunks n = Fold.many (MutArray.createOf n) Fold.toStreamK
1975+-- >>> buildChunks n = Fold.many (MutArray.writeN n) Fold.toStreamK
22591976--
22601977-- Breaking an array into an array stream can be useful to consume a large
22611978-- array sequentially such that memory of the array is released incrementatlly.
@@ -2267,10 +1984,9 @@ pinnedWriteNAligned align = createOfWith (pinnedNewAligned align)
22671984{-# INLINE_NORMAL buildChunks #-}
22681985buildChunks :: (MonadIO m, Unbox a) =>
22691986 Int -> Fold m a (StreamK n (MutArray a))
2270−buildChunks n = FL.many (createOf n) FL.toStreamK
1987+buildChunks n = FL.many (writeN n) FL.toStreamK
22711988
22721989{-# DEPRECATED writeChunks "Please use buildChunks instead." #-}
2273−{-# INLINE writeChunks #-}
22741990writeChunks :: (MonadIO m, Unbox a) =>
22751991 Int -> Fold m a (StreamK n (MutArray a))
22761992writeChunks = buildChunks
@@ -2278,29 +1994,29 @@ writeChunks = buildChunks
22781994{-# INLINE_NORMAL writeWithAs #-}
22791995writeWithAs :: forall m a. (MonadIO m, Unbox a)
22801996 => PinnedState -> Int -> Fold m a (MutArray a)
2281−-- writeWithAs ps n = FL.rmapM rightSize $ appendWith (* 2) (newAs ps n)
1997+-- writeWithAs ps n = FL.rmapM rightSize $ writeAppendWith (* 2) (newAs ps n)
22821998writeWithAs ps elemCount =
22831999 FL.rmapM extract $ FL.foldlM' step initial
22842000
22852001 where
22862002
22872003 initial = do
2288− when (elemCount < 0) $ error "createWith: elemCount is negative"
2289− newAs ps elemCount
2004+ when (elemCount < 0) $ error "writeWith: elemCount is negative"
2005+ liftIO $ newAs ps elemCount
22902006
22912007 step arr@(MutArray _ start end bound) x
22922008 | INDEX_NEXT(end,a) > bound = do
22932009 let oldSize = end - start
22942010 newSize = max (oldSize * 2) 1
2295− arr1 <- liftIO $ reallocExplicitAs ps (SIZE_OF(a)) newSize arr
2011+ arr1 <- liftIO $ reallocExplicit (SIZE_OF(a)) newSize arr
22962012 snocUnsafe arr1 x
22972013 step arr x = snocUnsafe arr x
22982014
22992015 extract = liftIO . rightSize
23002016
2301−-- XXX Compare createWith with fromStreamD which uses an array of streams
2017+-- XXX Compare writeWith with fromStreamD which uses an array of streams
23022018-- implementation. We can write this using buildChunks above if that is faster.
2303−-- If createWith is faster then we should use that to implement
2019+-- If writeWith is faster then we should use that to implement
23042020-- fromStreamD.
23052021--
23062022-- XXX The realloc based implementation needs to make one extra copy if we use
@@ -2315,56 +2031,39 @@ writeWithAs ps elemCount =
23152031-- XXX check if GHC's memory allocator is efficient enough. We can try the C
23162032-- malloc to compare against.
23172033
2318−-- | @createWith minCount@ folds the whole input to a single array. The array
2034+-- | @writeWith minCount@ folds the whole input to a single array. The array
23192035-- starts at a size big enough to hold minCount elements, the size is doubled
23202036-- every time the array needs to be grown.
23212037--
23222038-- /Caution! Do not use this on infinite streams./
23232039--
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)
2040+-- >>> f n = MutArray.writeAppendWith (* 2) (MutArray.new n)
2041+-- >>> writeWith n = Fold.rmapM MutArray.rightSize (f n)
2042+-- >>> writeWith n = Fold.rmapM MutArray.fromChunksK (MutArray.buildChunks n)
23272043--
23282044-- /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 #-}
2045+{-# INLINE_NORMAL writeWith #-}
23372046writeWith :: forall m a. (MonadIO m, Unbox a)
23382047 => Int -> Fold m a (MutArray a)
2339−writeWith = createWith
2048+-- writeWith n = FL.rmapM rightSize $ writeAppendWith (* 2) (new n)
2049+writeWith = writeWithAs Unpinned
23402050
23412051-- | Fold the whole input to a single array.
23422052--
2343−-- Same as 'createWith' using an initial array size of 'arrayChunkBytes' bytes
2053+-- Same as 'writeWith' using an initial array size of 'arrayChunkBytes' bytes
23442054-- rounded up to the element size.
23452055--
23462056-- /Caution! Do not use this on infinite streams./
23472057--
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." #-}
23542058{-# INLINE write #-}
23552059write :: forall m a. (MonadIO m, Unbox a) => Fold m a (MutArray a)
2356−write = create
2060+write = writeWith (allocBytesToElemCount (undefined :: a) arrayChunkBytes)
23572061
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." #-}
2062+-- | Like 'write' but creates a pinned array.
23652063{-# INLINE pinnedWrite #-}
23662064pinnedWrite :: forall m a. (MonadIO m, Unbox a) => Fold m a (MutArray a)
2367−pinnedWrite = pinnedCreate
2065+pinnedWrite =
2066+ writeWithAs Pinned (allocBytesToElemCount (undefined :: a) arrayChunkBytes)
23682067
23692068-------------------------------------------------------------------------------
23702069-- construct from streams, known size
@@ -2374,7 +2073,7 @@ pinnedWrite = pinnedCreate
23742073fromStreamDNAs :: forall m a. (MonadIO m, Unbox a)
23752074 => PinnedState -> Int -> D.Stream m a -> m (MutArray a)
23762075fromStreamDNAs ps limit str = do
2377− (arr :: MutArray a) <- newAs ps limit
2076+ (arr :: MutArray a) <- liftIO $ newAs ps limit
23782077 end <- D.foldlM' (fwrite (arrContents arr)) (return $ arrEnd arr) $ D.take limit str
23792078 return $ arr {arrEnd = end}
23802079
@@ -2384,18 +2083,17 @@ fromStreamDNAs ps limit str = do
23842083 liftIO $ pokeAt ptr arrContents x
23852084 return $ INDEX_NEXT(ptr,a)
23862085
2387−-- | Use the 'createOf' fold instead.
2086+-- | Use the 'writeN' fold instead.
23882087--
2389−-- >>> fromStreamN n = Stream.fold (MutArray.createOf n)
2088+-- >>> fromStreamN n = Stream.fold (MutArray.writeN n)
23902089--
23912090{-# INLINE_NORMAL fromStreamN #-}
23922091fromStreamN :: forall m a. (MonadIO m, Unbox a)
23932092 => Int -> D.Stream m a -> m (MutArray a)
2394−-- fromStreamDN n = D.fold (createOf n)
2093+-- fromStreamDN n = D.fold (writeN n)
23952094fromStreamN = fromStreamDNAs Unpinned
23962095
23972096{-# DEPRECATED fromStreamDN "Please use fromStreamN instead." #-}
2398−{-# INLINE fromStreamDN #-}
23992097fromStreamDN :: forall m a. (MonadIO m, Unbox a)
24002098 => Int -> D.Stream m a -> m (MutArray a)
24012099fromStreamDN = fromStreamN
@@ -2418,20 +2116,20 @@ pinnedFromListN n xs = fromStreamDNAs Pinned n $ D.fromList xs
24182116-- /Pre-release/
24192117{-# INLINE fromListRevN #-}
24202118fromListRevN :: (MonadIO m, Unbox a) => Int -> [a] -> m (MutArray a)
2421−fromListRevN n xs = D.fold (revCreateOf n) $ D.fromList xs
2119+fromListRevN n xs = D.fold (writeRevN n) $ D.fromList xs
24222120
24232121-- | Convert a pure stream in Identity monad to a mutable array.
24242122{-# INLINABLE fromPureStreamN #-}
24252123fromPureStreamN :: (MonadIO m, Unbox a) =>
24262124 Int -> Stream Identity a -> m (MutArray a)
24272125fromPureStreamN n xs =
2428− D.fold (createOf n) $ D.morphInner (return . runIdentity) xs
2126+ liftIO $ D.fold (writeN n) $ D.morphInner (return . runIdentity) xs
24292127
24302128-- | Convert a pure stream in Identity monad to a mutable array.
24312129{-# INLINABLE fromPureStream #-}
24322130fromPureStream :: (MonadIO m, Unbox a) => Stream Identity a -> m (MutArray a)
24332131fromPureStream xs =
2434− D.fold create $ D.morphInner (return . runIdentity) xs
2132+ liftIO $ D.fold write $ D.morphInner (return . runIdentity) xs
24352133
24362134{-# INLINABLE fromPtrN #-}
24372135fromPtrN :: MonadIO m => Int -> Ptr Word8 -> m (MutArray Word8)
@@ -2440,7 +2138,7 @@ fromPtrN len addr = do
24402138 -- XXX We can implement a stream copy in a similar way by streaming Word64
24412139 -- first and then remaining Word8.
24422140 arr <- new len
2443− _ <- unsafeAsPtr arr
2141+ _ <- asUnpinnedPtrUnsafe arr
24442142 (\ptr -> liftIO $ c_memcpy ptr addr (fromIntegral len))
24452143 return (arr {arrEnd = len})
24462144
@@ -2456,7 +2154,7 @@ fromByteStr# addr = do
24562154 len <- liftIO $ c_strlen (Ptr addr)
24572155 let lenInt = fromIntegral len
24582156 arr <- new lenInt
2459− _ <- unsafeAsPtr arr (\ptr -> liftIO $ c_memcpy ptr (Ptr addr) len)
2157+ _ <- asUnpinnedPtrUnsafe arr (\ptr -> liftIO $ c_memcpy ptr (Ptr addr) len)
24602158 return (arr {arrEnd = lenInt})
24612159
24622160-------------------------------------------------------------------------------
@@ -2477,7 +2175,7 @@ fromChunksRealloced s = do
24772175 arr <- D.foldlM' spliceExp (pure a) strm
24782176 -- Reallocation is exponential so there may be 50% empty space in
24792177 -- worst case. One more reallocation to reclaim the space.
2480− rightSize arr
2178+ liftIO $ rightSize arr
24812179 Nothing -> pure nil
24822180
24832181-------------------------------------------------------------------------------
@@ -2515,7 +2213,6 @@ fromChunksK :: (Unbox a, MonadIO m) =>
25152213fromChunksK = fromChunkskAs Unpinned
25162214
25172215{-# DEPRECATED fromArrayStreamK "Please use fromChunksK instead." #-}
2518−{-# INLINE fromArrayStreamK #-}
25192216fromArrayStreamK :: (Unbox a, MonadIO m) =>
25202217 StreamK m (MutArray a) -> m (MutArray a)
25212218fromArrayStreamK = fromChunksK
@@ -2527,7 +2224,7 @@ fromStreamDAs ps m =
25272224 arrayStreamKFromStreamDAs Unpinned m >>= fromChunkskAs ps
25282225
25292226-- | 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
2227+-- single array from a stream of unknown size. 'writeN' is at least twice
25312228-- as efficient when the size is already known.
25322229--
25332230-- Note that if the input stream is too large memory allocation for the array
@@ -2539,19 +2236,19 @@ fromStreamDAs ps m =
25392236fromStream :: (MonadIO m, Unbox a) => Stream m a -> m (MutArray a)
25402237fromStream = fromStreamDAs Unpinned
25412238
2542−-- fromStream (Stream m) = P.fold create m
2239+-- fromStream (Stream m) = P.fold write m
25432240-- CAUTION: a very large number (millions) of arrays can degrade performance
25442241-- due to GC overhead because we need to buffer the arrays before we flatten
25452242-- all the arrays.
25462243--
2547−-- XXX Compare if this is faster or "fold create".
2244+-- XXX Compare if this is faster or "fold write".
25482245--
25492246-- | We could take the approach of doubling the memory allocation on each
25502247-- overflow. This would result in more or less the same amount of copying as in
25512248-- the chunking approach. However, if we have to shrink in the end then it may
25522249-- result in an extra copy of the entire data.
25532250--
2554−-- >>> fromStreamD = StreamD.fold MutArray.create
2251+-- >>> fromStreamD = StreamD.fold MutArray.write
25552252--
25562253{-# INLINE fromStreamD #-}
25572254{-# DEPRECATED fromStreamD "Please use fromStream instead." #-}
@@ -2581,10 +2278,6 @@ fromListRev xs = fromListRevN (Prelude.length xs) xs
25812278-- Cloning
25822279-------------------------------------------------------------------------------
25832280
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−
25882281{-# INLINE cloneAs #-}
25892282cloneAs ::
25902283 ( MonadIO m
@@ -2594,7 +2287,7 @@ cloneAs ::
25942287 )
25952288 => PinnedState -> MutArray a -> m (MutArray a)
25962289cloneAs ps src =
2597− do
2290+ liftIO $ do
25982291 let startSrc = arrStart src
25992292 srcLen = arrEnd src - startSrc
26002293 newArrContents <-
@@ -2633,16 +2326,17 @@ spliceCopy :: forall m a. MonadIO m =>
26332326 Unbox a =>
26342327#endif
26352328 MutArray a -> MutArray a -> m (MutArray a)
2636−spliceCopy arr1 arr2 = do
2329+spliceCopy arr1 arr2 = liftIO $ do
26372330 let start1 = arrStart arr1
26382331 start2 = arrStart arr2
26392332 len1 = arrEnd arr1 - start1
26402333 len2 = arrEnd arr2 - start2
2641− let len = len1 + len2
2334+ let newLen = len1 + len2
26422335 newArrContents <-
26432336 if Unboxed.isPinned (arrContents arr1)
2644− then liftIO $ Unboxed.pinnedNew len
2645− else liftIO $ Unboxed.new len
2337+ then Unboxed.pinnedNew newLen
2338+ else Unboxed.new newLen
2339+ let len = len1 + len2
26462340 putSliceUnsafe (arrContents arr1) start1 newArrContents 0 len1
26472341 putSliceUnsafe (arrContents arr2) start2 newArrContents len1 len2
26482342 return $ MutArray newArrContents 0 len len
@@ -2654,7 +2348,7 @@ spliceCopy arr1 arr2 = do
26542348spliceUnsafe :: MonadIO m =>
26552349 MutArray a -> MutArray a -> m (MutArray a)
26562350spliceUnsafe dst src =
2657− do
2351+ liftIO $ do
26582352 let startSrc = arrStart src
26592353 srcLen = arrEnd src - startSrc
26602354 endDst = arrEnd dst
@@ -2676,7 +2370,7 @@ spliceWith :: forall m a. (MonadIO m, Unbox a) =>
26762370 (Int -> Int -> Int) -> MutArray a -> MutArray a -> m (MutArray a)
26772371spliceWith sizer dst@(MutArray _ start end bound) src = do
26782372{-
2679− let f = appendWith (`sizer` byteLength src) (return dst)
2373+ let f = writeAppendWith (`sizer` byteLength src) (return dst)
26802374 in D.fold f (toStreamD src)
26812375-}
26822376 assert (end <= bound) (return ())
@@ -2692,7 +2386,7 @@ spliceWith sizer dst@(MutArray _ start end bound) src = do
26922386 $ "splice: newSize is less than the total size "
26932387 ++ "of arrays being appended. Please check the "
26942388 ++ "sizer function passed."
2695− realloc newSizeInBytes dst
2389+ liftIO $ realloc newSizeInBytes dst
26962390 else return dst
26972391 spliceUnsafe dst1 src
26982392
@@ -2744,7 +2438,7 @@ splitOn predicate arr =
27442438{-# INLINE breakOn #-}
27452439breakOn :: MonadIO m
27462440 => Word8 -> MutArray Word8 -> m (MutArray Word8, Maybe (MutArray Word8))
2747−breakOn sep arr@MutArray{..} = unsafeAsPtr arr $ \p -> liftIO $ do
2441+breakOn sep arr@MutArray{..} = asUnpinnedPtrUnsafe arr $ \p -> liftIO $ do
27482442 -- XXX We do not need memchr here, we can use a Haskell equivalent.
27492443 -- Need efficient stream based primitives that work on Word64.
27502444 loc <- c_memchr p sep (fromIntegral $ byteLength arr)
@@ -2767,41 +2461,33 @@ breakOn sep arr@MutArray{..} = unsafeAsPtr arr $ \p -> liftIO $ do
27672461 }
27682462 )
27692463
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−
27922464-- | Create two slices of an array without copying the original array. The
27932465-- specified index @i@ is the first index of the second slice.
27942466--
27952467{-# INLINE splitAt #-}
27962468splitAt :: forall a. Unbox a => Int -> MutArray a -> (MutArray a, MutArray a)
2797−splitAt i arr =
2469+splitAt i arr@MutArray{..} =
27982470 let maxIndex = length arr - 1
27992471 in if i < 0
28002472 then error "sliceAt: negative array index"
28012473 else if i > maxIndex
28022474 then error $ "sliceAt: specified array index " ++ show i
28032475 ++ " is beyond the maximum index " ++ show maxIndex
2804− else unsafeSplitAt i arr
2476+ else let off = i * SIZE_OF(a)
2477+ p = arrStart + off
2478+ in ( MutArray
2479+ { arrContents = arrContents
2480+ , arrStart = arrStart
2481+ , arrEnd = p
2482+ , arrBound = p
2483+ }
2484+ , MutArray
2485+ { arrContents = arrContents
2486+ , arrStart = p
2487+ , arrEnd = arrEnd
2488+ , arrBound = arrBound
2489+ }
2490+ )
28052491
28062492-------------------------------------------------------------------------------
28072493-- Casting
@@ -2851,13 +2537,9 @@ cast arr =
28512537-- guarantees that unsafe calls will be performed in the calling thread. Making
28522538-- it safe to pass heap-allocated objects to unsafe functions.
28532539
2854−-- XXX Should we just name it asPtr, the unsafety is implicit for any pointer
2540+-- Should we just name it asPtr, the unsafety is implicit for any pointer
28552541-- operations. And we are safe from Haskell perspective because we will be
28562542-- 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.
28612543
28622544-- | Use a @MutArray a@ as @Ptr a@. This is useful when we want to pass an
28632545-- array as a pointer to some operating system call or to a "safe" FFI call.
@@ -2875,21 +2557,16 @@ cast arr =
28752557--
28762558-- /Pre-release/
28772559--
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." #-}
28852560{-# INLINE asPtrUnsafe #-}
28862561asPtrUnsafe :: MonadIO m => MutArray a -> (Ptr a -> m b) -> m b
2887−asPtrUnsafe = unsafePinnedAsPtr
2562+asPtrUnsafe arr f =
2563+ Unboxed.asPtrUnsafe
2564+ (arrContents arr) (\ptr -> f (ptr `plusPtr` arrStart arr))
28882565
2889−{-# INLINE unsafeAsPtr #-}
2890−unsafeAsPtr :: MonadIO m => MutArray a -> (Ptr a -> m b) -> m b
2891−unsafeAsPtr arr f =
2892− Unboxed.unsafeAsPtr
2566+{-# INLINE asUnpinnedPtrUnsafe #-}
2567+asUnpinnedPtrUnsafe :: MonadIO m => MutArray a -> (Ptr a -> m b) -> m b
2568+asUnpinnedPtrUnsafe arr f =
2569+ Unboxed.asUnpinnedPtrUnsafe
28932570 (arrContents arr) (\ptr -> f (ptr `plusPtr` arrStart arr))
28942571
28952572-------------------------------------------------------------------------------
@@ -2907,26 +2584,28 @@ unsafeAsPtr arr f =
29072584-- /Pre-release/
29082585{-# INLINE byteCmp #-}
29092586byteCmp :: MonadIO m => MutArray a -> MutArray a -> m Ordering
2910−byteCmp arr1 arr2 = do
2911− let marr1 = getMutableByteArray# (arrContents arr1)
2912− marr2 = getMutableByteArray# (arrContents arr2)
2913− !(I# st1#) = arrStart arr1
2914− !(I# st2#) = arrStart arr2
2915− !(I# len#) = byteLength arr1
2916− case compare (byteLength arr1) (byteLength arr2) of
2917− EQ -> do
2918− r <- liftIO $ IO $ \s# ->
2919− let res =
2920− I#
2921− (compareByteArrays#
2922− (unsafeCoerce# marr1)
2923− st1#
2924− (unsafeCoerce# marr2)
2925− st2#
2926− len#)
2927− in (# s#, res #)
2928− return $ compare r 0
2929− x -> return x
2587+byteCmp arr1 arr2 =
2588+ liftIO
2589+ $ do
2590+ let marr1 = getMutableByteArray# (arrContents arr1)
2591+ marr2 = getMutableByteArray# (arrContents arr2)
2592+ !(I# st1#) = arrStart arr1
2593+ !(I# st2#) = arrStart arr2
2594+ !(I# len#) = byteLength arr1
2595+ case compare (byteLength arr1) (byteLength arr2) of
2596+ EQ -> do
2597+ r <- liftIO $ IO $ \s# ->
2598+ let res =
2599+ I#
2600+ (compareByteArrays#
2601+ (unsafeCoerce# marr1)
2602+ st1#
2603+ (unsafeCoerce# marr2)
2604+ st2#
2605+ len#)
2606+ in (# s#, res #)
2607+ return $ compare r 0
2608+ x -> return x
29302609
29312610{-# INLINE cmp #-}
29322611{-# DEPRECATED cmp "Please use byteCmp instead." #-}
@@ -2946,48 +2625,46 @@ byteEq arr1 arr2 = fmap (EQ ==) $ byteCmp arr1 arr2
29462625-- Compact
29472626-------------------------------------------------------------------------------
29482627
2949−-- Note: LE versions avoid an extra copy compared to GE. LE parser trades
2950−-- backtracking one array in lieu of avoiding a copy. However, LE and GE both
2951−-- can leave some memory unused. They can split the last array to fit it
2952−-- exactly in the space.
2953−
2954−{-# INLINE_NORMAL pCompactLeAs #-}
2955−pCompactLeAs ::
2628+-- | Parser @pCompactChunksLE n@ coalesces adjacent arrays in the input stream
2629+-- only if the combined size would be less than or equal to n.
2630+--
2631+-- /Internal/
2632+{-# INLINE_NORMAL pCompactChunksLE #-}
2633+pCompactChunksLE ::
29562634 forall m a. (MonadIO m, Unbox a)
2957− => PinnedState -> Int -> Parser (MutArray a) m (MutArray a)
2958−pCompactLeAs ps maxElems = Parser step initial extract
2635+ => Int -> Parser (MutArray a) m (MutArray a)
2636+pCompactChunksLE n = Parser step initial extract
29592637
29602638 where
29612639
2962− maxBytes = maxElems * SIZE_OF(a)
2640+ nBytes = n * SIZE_OF(a)
29632641
2964− functionName = "Streamly.Internal.Data.MutArray.pCompactLE"
2642+ functionName = "Streamly.Internal.Data.MutArray.pCompactChunksLE"
29652643
29662644 initial =
29672645 return
2968− $ if maxElems <= 0
2646+ $ if n <= 0
29692647 then error
29702648 $ functionName
29712649 ++ ": the size of arrays ["
2972− ++ show maxElems ++ "] must be a natural number"
2650+ ++ show n ++ "] must be a natural number"
29732651 else Parser.IPartial Nothing
29742652
29752653 step Nothing arr =
29762654 return
29772655 $ let len = byteLength arr
2978− in if len >= maxBytes
2656+ in if len >= nBytes
29792657 then Parser.Done 0 arr
29802658 else Parser.Partial 0 (Just arr)
2981− -- XXX Split the last array to use the space more compactly.
29822659 step (Just buf) arr =
29832660 let len = byteLength buf + byteLength arr
2984− in if len > maxBytes
2661+ in if len > nBytes
29852662 then return $ Parser.Done 1 buf
29862663 else do
2664+ -- XXX check this in the Nothing case
29872665 buf1 <-
2988− if byteCapacity buf < maxBytes
2989− then liftIO $ reallocExplicitAs
2990− ps (SIZE_OF(a)) maxBytes buf
2666+ if byteCapacity buf < nBytes
2667+ then liftIO $ realloc nBytes buf
29912668 else return buf
29922669 buf2 <- spliceUnsafe buf1 arr
29932670 return $ Parser.Partial 0 (Just buf2)
@@ -2995,30 +2672,6 @@ pCompactLeAs ps maxElems = Parser step initial extract
29952672 extract Nothing = return $ Parser.Done 0 nil
29962673 extract (Just buf) = return $ Parser.Done 0 buf
29972674
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−
30222675data SpliceState s arr
30232676 = SpliceInitial s
30242677 | SpliceBuffering s arr
@@ -3030,47 +2683,48 @@ data SpliceState s arr
30302683-- immutable array never has additional space so a new array is allocated
30312684-- instead of mutating it.
30322685
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) =
2686+-- | Scan @rCompactChunksLE n@ coalesces adjacent arrays in the input stream
2687+-- only if the combined size would be less than or equal to n.
2688+--
2689+-- /Internal/
2690+{-# INLINE_NORMAL rCompactChunksLE #-}
2691+rCompactChunksLE :: (MonadIO m, Unbox a)
2692+ => Int -> D.Stream m (MutArray a) -> D.Stream m (MutArray a)
2693+rCompactChunksLE n (D.Stream step state) =
30372694 D.Stream step' (SpliceInitial state)
30382695
30392696 where
30402697
3041− maxBytes = maxElems * SIZE_OF(a)
3042−
3043− functionName = "Streamly.Internal.Data.MutArray.rCompactLE"
2698+ functionName = "Streamly.Internal.Data.MutArray.rCompactChunksLE"
30442699
30452700 {-# INLINE_LATE step' #-}
30462701 step' gst (SpliceInitial st) = do
3047− when (maxElems <= 0) $
2702+ when (n <= 0) $
30482703 -- XXX we can pass the module string from the higher level API
3049− error $ functionName ++ ": the size of arrays [" ++ show maxElems
2704+ error $ functionName ++ ": the size of arrays [" ++ show n
30502705 ++ "] must be a natural number"
30512706 r <- step gst st
30522707 case r of
30532708 D.Yield arr s -> return $
30542709 let len = byteLength arr
3055− in if len >= maxBytes
2710+ in if len >= n
30562711 then D.Skip (SpliceYielding arr (SpliceInitial s))
30572712 else D.Skip (SpliceBuffering s arr)
30582713 D.Skip s -> return $ D.Skip (SpliceInitial s)
30592714 D.Stop -> return D.Stop
30602715
3061− -- XXX Split the last array to use the space more compactly.
30622716 step' gst (SpliceBuffering st buf) = do
30632717 r <- step gst st
30642718 case r of
30652719 D.Yield arr s -> do
30662720 let len = byteLength buf + byteLength arr
3067− if len > maxBytes
2721+ if len > n
30682722 then return $
30692723 D.Skip (SpliceYielding buf (SpliceBuffering s arr))
30702724 else do
3071− buf1 <- if byteCapacity buf < maxBytes
3072− then liftIO $ reallocExplicitAs
3073− ps (SIZE_OF(a)) maxBytes buf
2725+ -- XXX check this in the SpliceInitial case
2726+ buf1 <- if byteCapacity buf < n
2727+ then liftIO $ realloc n buf
30742728 else return buf
30752729 buf2 <- spliceUnsafe buf1 arr
30762730 return $ D.Skip (SpliceBuffering s buf2)
@@ -3081,124 +2735,113 @@ compactLeAs ps maxElems (D.Stream step state) =
30812735
30822736 step' _ (SpliceYielding arr next) = return $ D.Yield arr next
30832737
3084−
3085−{-# INLINE_NORMAL fCompactGeAs #-}
3086−fCompactGeAs ::
2738+-- | Fold @fCompactChunksGE n@ coalesces adjacent arrays in the input stream
2739+-- until the size becomes greater than or equal to n.
2740+--
2741+{-# INLINE_NORMAL fCompactChunksGE #-}
2742+fCompactChunksGE ::
30872743 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
2744+ => Int -> FL.Fold m (MutArray a) (MutArray a)
2745+fCompactChunksGE n = Fold step initial extract extract
30902746
30912747 where
30922748
3093− minBytes = minElems * SIZE_OF(a)
2749+ nBytes = n * SIZE_OF(a)
30942750
3095− functionName = "Streamly.Internal.Data.MutArray.fCompactGE"
2751+ functionName = "Streamly.Internal.Data.MutArray.fCompactChunksGE"
30962752
30972753 initial =
30982754 return
3099− $ if minElems < 0
2755+ $ if n < 0
31002756 then error
31012757 $ functionName
31022758 ++ ": the size of arrays ["
3103− ++ show minElems ++ "] must be a natural number"
2759+ ++ show n ++ "] must be a natural number"
31042760 else FL.Partial Nothing
31052761
31062762 step Nothing arr =
31072763 return
31082764 $ let len = byteLength arr
3109− in if len >= minBytes
2765+ in if len >= nBytes
31102766 then FL.Done arr
31112767 else FL.Partial (Just arr)
3112− -- XXX Buffer arrays as a list to avoid copy and reallocations
31132768 step (Just buf) arr = do
31142769 let len = byteLength buf + byteLength arr
31152770 buf1 <-
31162771 if byteCapacity buf < len
3117− then liftIO $ reallocExplicitAs
3118− ps (SIZE_OF(a)) (max minBytes len) buf
2772+ then liftIO $ realloc (max len nBytes) buf
31192773 else return buf
31202774 buf2 <- spliceUnsafe buf1 arr
3121− if len >= minBytes
2775+ if len >= n
31222776 then return $ FL.Done buf2
31232777 else return $ FL.Partial (Just buf2)
31242778
31252779 extract Nothing = return nil
31262780 extract (Just buf) = return buf
31272781
3128−-- | Fold @fCompactGE minElems@ coalesces adjacent arrays in the input stream
3129−-- until the size becomes greater than or equal to @minElems@.
2782+-- | Like 'compactChunksGE' but for transforming folds instead of stream.
31302783--
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) =
2784+-- >>> lCompactChunksGE n = Fold.many (MutArray.fCompactChunksGE n)
2785+--
2786+{-# INLINE_NORMAL lCompactChunksGE #-}
2787+lCompactChunksGE :: (MonadIO m, Unbox a)
2788+ => Int -> Fold m (MutArray a) () -> Fold m (MutArray a) ()
2789+-- lCompactChunksGE n = Fold.many (fCompactChunksGE n)
2790+lCompactChunksGE n (Fold step1 initial1 _ final1) =
31522791 Fold step initial extract final
31532792
31542793 where
31552794
3156− minBytes = minElems * SIZE_OF(a)
3157−
3158− functionName = "Streamly.Internal.Data.MutArray.lCompactGE"
2795+ functionName = "Streamly.Internal.Data.MutArray.lCompactChunksGE"
31592796
31602797 initial = do
3161− when (minElems <= 0) $
2798+ when (n <= 0) $
31622799 -- XXX we can pass the module string from the higher level API
31632800 error $ functionName ++ ": the size of arrays ["
3164− ++ show minElems ++ "] must be a natural number"
2801+ ++ show n ++ "] must be a natural number"
31652802
31662803 r <- initial1
31672804 return $ first (Tuple' Nothing) r
31682805
3169− {-# INLINE runInner #-}
3170− runInner len acc buf =
3171− if len >= minBytes
2806+ step (Tuple' Nothing r1) arr =
2807+ let len = byteLength arr
2808+ in if len >= n
2809+ then do
2810+ r <- step1 r1 arr
2811+ case r of
2812+ FL.Done _ -> return $ FL.Done ()
2813+ FL.Partial s -> do
2814+ _ <- final1 s
2815+ res <- initial1
2816+ return $ first (Tuple' Nothing) res
2817+ else return $ FL.Partial $ Tuple' (Just arr) r1
2818+
2819+ step (Tuple' (Just buf) r1) arr = do
2820+ let len = byteLength buf + byteLength arr
2821+ buf1 <- if byteCapacity buf < len
2822+ then liftIO $ realloc (max n len) buf
2823+ else return buf
2824+ buf2 <- spliceUnsafe buf1 arr
2825+
2826+ -- XXX this is common in both the equations of step
2827+ if len >= n
31722828 then do
3173− r <- step1 acc buf
2829+ r <- step1 r1 buf2
31742830 case r of
31752831 FL.Done _ -> return $ FL.Done ()
31762832 FL.Partial s -> do
31772833 _ <- final1 s
31782834 res <- initial1
31792835 return $ first (Tuple' Nothing) res
3180− else return $ FL.Partial $ Tuple' (Just buf) acc
3181−
3182− step (Tuple' Nothing r1) arr =
3183− runInner (byteLength arr) r1 arr
3184−
3185− -- XXX Buffer arrays as a list to avoid copy and reallocations
3186− step (Tuple' (Just buf) r1) arr = do
3187− let len = byteLength buf + byteLength arr
3188− buf1 <- if byteCapacity buf < len
3189− then liftIO $ reallocExplicitAs
3190− ps (SIZE_OF(a)) (max minBytes len) buf
3191− else return buf
3192− buf2 <- spliceUnsafe buf1 arr
3193− runInner len r1 buf2
2836+ else return $ FL.Partial $ Tuple' (Just buf2) r1
31942837
31952838 -- XXX Several folds do extract >=> final, therefore, we need to make final
3196− -- return "m b" rather than using extract post it if we want extract to be
2839+ -- return "m b" rather than using extract post it if we want extract to be
31972840 -- partial.
31982841 --
31992842 -- extract forces the pending buffer to be sent to the fold which is not
32002843 -- what we want.
3201− extract _ = error "lCompactGE: not designed for scanning"
2844+ extract _ = error "lCompactChunksGE: not designed for scanning"
32022845
32032846 final (Tuple' Nothing r1) = final1 r1
32042847 final (Tuple' (Just buf) r1) = do
@@ -3207,42 +2850,25 @@ lCompactGeAs ps minElems (Fold step1 initial1 _ final1) =
32072850 FL.Partial rr -> final1 rr
32082851 FL.Done _ -> return ()
32092852
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−
3227−-- | @compactGE n stream@ coalesces adjacent arrays in the @stream@ until
2853+-- | @compactChunksGE n stream@ coalesces adjacent arrays in the @stream@ until
32282854-- the size becomes greater than or equal to @n@.
32292855--
3230−-- >>> compactGE n = Stream.foldMany (MutArray.fCompactGE n)
2856+-- >>> compactChunksGE n = Stream.foldMany (MutArray.fCompactChunksGE n)
32312857--
3232−{-# INLINE compactGE #-}
3233−compactGE ::
2858+{-# INLINE compactChunksGE #-}
2859+compactChunksGE ::
32342860 (MonadIO m, Unbox a)
32352861 => Int -> Stream m (MutArray a) -> Stream m (MutArray a)
3236−compactGE n = D.foldMany (fCompactGE n)
2862+compactChunksGE n = D.foldMany (fCompactChunksGE n)
32372863
3238−-- | 'compactEQ n' coalesces adajacent arrays in the input stream to
2864+-- | 'compactChunksEQ n' coalesces adajacent arrays in the input stream to
32392865-- arrays of exact size @n@.
32402866--
32412867-- /Unimplemented/
3242−{-# INLINE compactEQ #-}
3243−compactEQ :: -- (MonadIO m, Unbox a) =>
2868+{-# INLINE compactChunksEQ #-}
2869+compactChunksEQ :: -- (MonadIO m, Unbox a) =>
32442870 Int -> Stream m (MutArray a) -> Stream m (MutArray a)
3245−compactEQ _n = undefined -- D.parseManyD (pCompactEQ n)
2871+compactChunksEQ _n = undefined -- D.parseManyD (pCompactChunksEQ n)
32462872
32472873-------------------------------------------------------------------------------
32482874-- In-place mutation algorithms