on master help sign in
Rename some array APIs in Array/MutArray/MutByteArray
9654ce9Harendra Kumar
core/src/Streamly/Data/MutArray.hs+12−6
@@ -32,14 +32,14 @@ module Streamly.Data.MutArray
32 -- * Construction
32 -- * Construction
33
33
34 -- Uninitialized Arrays
34 -- Uninitialized Arrays
35− , new
35+ , emptyOf
36− , pinnedNew
36+ , pinnedEmptyOf
37
37
38 -- From containers
38 -- From containers
39 , fromListN
39 , fromListN
40 , fromList
40 , fromList
41− , writeN -- drop new
41+ , createOf
42− , write -- full buffer
42+ , create
43 -- writeLastN
43 -- writeLastN
44
44
45 -- * Pinning & Unpinning
45 -- * Pinning & Unpinning
@@ -51,8 +51,8 @@ module Streamly.Data.MutArray
51 , snoc
51 , snoc
52
52
53 -- * Appending streams
53 -- * Appending streams
54− , writeAppendN -- XXX Rename to writeSnocN?
54+ , appendN
55− , writeAppend -- XXX Rename to writeSnoc?
55+ , append
56
56
57 -- * Inplace mutation
57 -- * Inplace mutation
58 , putIndex
58 , putIndex
@@ -88,6 +88,12 @@ module Streamly.Data.MutArray
88
88
89 -- * Deprecated
89 -- * Deprecated
90 , newPinned
90 , newPinned
91+ , new
92+ , pinnedNew
93+ , writeN
94+ , write
95+ , writeAppendN
96+ , writeAppend
91 )
97 )
92where
98where
93
99
core/src/Streamly/Internal/Data/Array/Type.hs+21−7
@@ -31,7 +31,7 @@ module Streamly.Internal.Data.Array.Type
31 , asPtrUnsafe
31 , asPtrUnsafe
32
32
33 -- ** Construction
33 -- ** Construction
34− , nil
34+ , empty
35
35
36 -- *** Cloning
36 -- *** Cloning
37 -- XXX Why would we clone an immutable array?
37 -- XXX Why would we clone an immutable array?
@@ -140,6 +140,7 @@ module Streamly.Internal.Data.Array.Type
140 , toStreamDRev
140 , toStreamDRev
141 , toStream
141 , toStream
142 , toStreamRev
142 , toStreamRev
143+ , nil
143 )
144 )
144where
145where
145
146
@@ -736,7 +737,7 @@ writeN = fmap unsafeFreeze . MA.writeN
736-- | Like 'fromListN' but creates a pinned array.
737-- | Like 'fromListN' but creates a pinned array.
737{-# INLINE_NORMAL pinnedWriteN #-}
738{-# INLINE_NORMAL pinnedWriteN #-}
738pinnedWriteN :: forall m a. (MonadIO m, Unbox a) => Int -> Fold m a (Array a)
739pinnedWriteN :: forall m a. (MonadIO m, Unbox a) => Int -> Fold m a (Array a)
739−pinnedWriteN = fmap unsafeFreeze . MA.pinnedWriteN
740+pinnedWriteN = fmap unsafeFreeze . MA.pinnedCreateOf
740
741
741-- | @pinnedWriteNAligned alignment n@ folds a maximum of @n@ elements from the input
742-- | @pinnedWriteNAligned alignment n@ folds a maximum of @n@ elements from the input
742-- stream to an 'Array' aligned to the given size.
743-- stream to an 'Array' aligned to the given size.
@@ -757,18 +758,18 @@ pinnedWriteNAligned alignSize = fmap unsafeFreeze . MA.pinnedWriteNAligned align
757{-# INLINE_NORMAL writeNUnsafe #-}
758{-# INLINE_NORMAL writeNUnsafe #-}
758writeNUnsafe :: forall m a. (MonadIO m, Unbox a)
759writeNUnsafe :: forall m a. (MonadIO m, Unbox a)
759 => Int -> Fold m a (Array a)
760 => Int -> Fold m a (Array a)
760−writeNUnsafe n = unsafeFreeze <$> MA.writeNUnsafe n
761+writeNUnsafe n = unsafeFreeze <$> MA.unsafeCreateOf n
761
762
762{-# INLINE_NORMAL pinnedWriteNUnsafe #-}
763{-# INLINE_NORMAL pinnedWriteNUnsafe #-}
763pinnedWriteNUnsafe :: forall m a. (MonadIO m, Unbox a)
764pinnedWriteNUnsafe :: forall m a. (MonadIO m, Unbox a)
764 => Int -> Fold m a (Array a)
765 => Int -> Fold m a (Array a)
765−pinnedWriteNUnsafe n = unsafeFreeze <$> MA.pinnedWriteNUnsafe n
766+pinnedWriteNUnsafe n = unsafeFreeze <$> MA.unsafePinnedCreateOf n
766
767
767{-# INLINE_NORMAL writeWith #-}
768{-# INLINE_NORMAL writeWith #-}
768writeWith :: forall m a. (MonadIO m, Unbox a)
769writeWith :: forall m a. (MonadIO m, Unbox a)
769 => Int -> Fold m a (Array a)
770 => Int -> Fold m a (Array a)
770-- writeWith n = FL.rmapM spliceArrays $ toArraysOf n
771-- writeWith n = FL.rmapM spliceArrays $ toArraysOf n
771−writeWith elemCount = unsafeFreeze <$> MA.writeWith elemCount
772+writeWith elemCount = unsafeFreeze <$> MA.createWith elemCount
772
773
773-- | Fold the whole input to a single array.
774-- | Fold the whole input to a single array.
774--
775--
@@ -781,7 +782,7 @@ write = fmap unsafeFreeze MA.write
781-- | Like 'write' but creates a pinned array.
782-- | Like 'write' but creates a pinned array.
782{-# INLINE pinnedWrite #-}
783{-# INLINE pinnedWrite #-}
783pinnedWrite :: forall m a. (MonadIO m, Unbox a) => Fold m a (Array a)
784pinnedWrite :: forall m a. (MonadIO m, Unbox a) => Fold m a (Array a)
784−pinnedWrite = fmap unsafeFreeze MA.pinnedWrite
785+pinnedWrite = fmap unsafeFreeze MA.pinnedCreate
785
786
786-- | Fold "step" has a dependency on "initial", and each step is dependent on
787-- | Fold "step" has a dependency on "initial", and each step is dependent on
787-- the previous invocation of step due to state passing, finally extract
788-- the previous invocation of step due to state passing, finally extract
@@ -957,11 +958,16 @@ MK_EQ_INSTANCE(Char)
957MK_EQ_INSTANCE(Word8)
958MK_EQ_INSTANCE(Word8)
958MK_EQ_INSTANCE(Word16)
959MK_EQ_INSTANCE(Word16)
959MK_EQ_INSTANCE(Word32)
960MK_EQ_INSTANCE(Word32)
961+
962+-- XXX The Word64 default instance should be as fast because we are comparing
963+-- 64-bit at a time.
960MK_EQ_INSTANCE(Word64)
964MK_EQ_INSTANCE(Word64)
961MK_EQ_INSTANCE(Int)
965MK_EQ_INSTANCE(Int)
962MK_EQ_INSTANCE(Int8)
966MK_EQ_INSTANCE(Int8)
963MK_EQ_INSTANCE(Int16)
967MK_EQ_INSTANCE(Int16)
964MK_EQ_INSTANCE(Int32)
968MK_EQ_INSTANCE(Int32)
969+
970+-- XXX The Int64 default instance should be as fast.
965MK_EQ_INSTANCE(Int64)
971MK_EQ_INSTANCE(Int64)
966
972
967-- | If the type allows a byte-by-byte comparison this instance can be
973-- | If the type allows a byte-by-byte comparison this instance can be
@@ -1051,12 +1057,20 @@ instance Foldable Array where
1051instance Unbox a => Semigroup (Array a) where
1057instance Unbox a => Semigroup (Array a) where
1052 arr1 <> arr2 = unsafePerformIO $ splice arr1 arr2
1058 arr1 <> arr2 = unsafePerformIO $ splice arr1 arr2
1053
1059
1060+empty ::
1061+#ifdef DEVBUILD
1062+ Unbox a =>
1063+#endif
1064+ Array a
1065+empty = Array Unboxed.empty 0 0
1066+
1067+{-# DEPRECATED nil "Please use empty instead." #-}
1054nil ::
1068nil ::
1055#ifdef DEVBUILD
1069#ifdef DEVBUILD
1056 Unbox a =>
1070 Unbox a =>
1057#endif
1071#endif
1058 Array a
1072 Array a
1059−nil = Array Unboxed.nil 0 0
1073+nil = empty
1060
1074
1061instance Unbox a => Monoid (Array a) where
1075instance Unbox a => Monoid (Array a) where
1062 mempty = nil
1076 mempty = nil
core/src/Streamly/Internal/Data/MutArray/Type.hs+275−128
@@ -40,22 +40,22 @@ module Streamly.Internal.Data.MutArray.Type
40
40
41 -- ** Casting
41 -- ** Casting
42 , cast
42 , cast
43− , castUnsafe
43+ , castUnsafe -- XXX unsafeCast
44 , asBytes
44 , asBytes
45− , asPtrUnsafe
45+ , asPtrUnsafe -- XXX rename to unsafePinnedAsPtr?
46− , asUnpinnedPtrUnsafe
46+ , asUnpinnedPtrUnsafe -- XXX rename to unsafeAsPtr
47
47
48 -- ** Construction
48 -- ** Construction
49− , nil
49+ , empty
50
50
51 -- *** New
51 -- *** New
52 -- | New arrays are always empty arrays with some reserve capacity to
52 -- | New arrays are always empty arrays with some reserve capacity to
53 -- extend the length without reallocating.
53 -- extend the length without reallocating.
54− , new
54+ , emptyOf
55− , newArrayWith
55+ , newArrayWith -- emptyAlignedWith
56− , pinnedNew
56+ , pinnedEmptyOf
57− , pinnedNewBytes
58− , pinnedNewAligned
57+ , pinnedNewAligned -- XXX not required
58+ -- , new -- uninitialized array of specified length
59
59
60 -- *** Cloning
60 -- *** Cloning
61 , clone
61 , clone
@@ -63,27 +63,25 @@ module Streamly.Internal.Data.MutArray.Type
63
63
64 -- *** Slicing
64 -- *** Slicing
65 -- | Get a subarray without copying
65 -- | Get a subarray without copying
66− , getSliceUnsafe
66+ , getSliceUnsafe -- XXX unsafeGetSlice
67 , getSlice
67 , getSlice
68 , splitAt -- XXX should be able to express using getSlice
68 , splitAt -- XXX should be able to express using getSlice
69 , breakOn
69 , breakOn
70
70
71 -- *** Stream Folds
71 -- *** Stream Folds
72 , ArrayUnsafe (..)
72 , ArrayUnsafe (..)
73− , writeNWithUnsafe
73+ , unsafeCreateOfWith
74− , writeNWith
74+ , unsafeCreateOf
75− , writeNUnsafe
75+ , unsafePinnedCreateOf
76− , pinnedWriteNUnsafe
76+ , pinnedCreateOf
77− , writeN
77+ , createOfWith
78− , pinnedWriteN
78+ , createOf
79− , pinnedWriteNAligned
79+ , revCreateOf
80−
80+
81− , writeWith
81+ , pinnedCreate
82− , write
82+ , createWith
83− , pinnedWrite
83+ , create
84−
84+ -- , revCreate
85− , writeRevN
86− -- , writeRev
87
85
88 -- *** From containers
86 -- *** From containers
89 , fromListN
87 , fromListN
@@ -103,7 +101,7 @@ module Streamly.Internal.Data.MutArray.Type
103
101
104 -- ** Random writes
102 -- ** Random writes
105 , putIndex
103 , putIndex
106− , putIndexUnsafe
104+ , putIndexUnsafe -- XXX unsafePutIndex
107 , putIndices
105 , putIndices
108 -- , putFromThenTo
106 -- , putFromThenTo
109 -- , putFrom -- start writing at the given position
107 -- , putFrom -- start writing at the given position
@@ -111,7 +109,7 @@ module Streamly.Internal.Data.MutArray.Type
111 -- , putFromTo
109 -- , putFromTo
112 -- , putFromRev
110 -- , putFromRev
113 -- , putUptoRev
111 -- , putUptoRev
114− , modifyIndexUnsafe
112+ , modifyIndexUnsafe -- XXX unsafeModifyIndex
115 , modifyIndex
113 , modifyIndex
116 , modifyIndices
114 , modifyIndices
117 , modify
115 , modify
@@ -122,9 +120,9 @@ module Streamly.Internal.Data.MutArray.Type
122
120
123 -- *** Indexing
121 -- *** Indexing
124 , getIndex
122 , getIndex
125− , getIndexUnsafe
123+ , getIndexUnsafe -- XXX unsafeGetIndex
126 -- , getFromThenTo
124 -- , getFromThenTo
127− , getIndexRev
125+ , getIndexRev -- getRevIndex?
128 , indexReader
126 , indexReader
129 , indexReaderWith
127 , indexReaderWith
130
128
@@ -195,20 +193,20 @@ module Streamly.Internal.Data.MutArray.Type
195 , snoc
193 , snoc
196 , snocLinear
194 , snocLinear
197 , snocMay
195 , snocMay
198− , snocUnsafe
196+ , snocUnsafe -- XXX unsafeSnoc
199
197
200 -- *** Appending streams
198 -- *** Appending streams
201− , writeAppendNUnsafe
199+ , unsafeAppendN
202− , writeAppendN
200+ , appendN
203− , writeAppendWith
201+ , appendWith
204− , writeAppend
202+ , append
205
203
206 -- *** Appending arrays
204 -- *** Appending arrays
207 , spliceCopy
205 , spliceCopy
208 , spliceWith
206 , spliceWith
209 , splice
207 , splice
210 , spliceExp
208 , spliceExp
211− , spliceUnsafe
209+ , spliceUnsafe -- XXX unsafeSplice
212 -- , putSlice
210 -- , putSlice
213 -- , appendSlice
211 -- , appendSlice
214 -- , appendSliceFrom
212 -- , appendSliceFrom
@@ -275,6 +273,25 @@ module Streamly.Internal.Data.MutArray.Type
275 , getIndicesWith
273 , getIndicesWith
276 , resize
274 , resize
277 , resizeExp
275 , resizeExp
276+ , nil
277+ , new
278+ , pinnedNew
279+ , pinnedNewBytes
280+ , writeAppendNUnsafe
281+ , writeAppendN
282+ , writeAppendWith
283+ , writeAppend
284+ , writeNWithUnsafe
285+ , writeNWith
286+ , writeNUnsafe
287+ , pinnedWriteNUnsafe
288+ , writeN
289+ , pinnedWriteN
290+ , pinnedWriteNAligned -- XXX not required
291+ , writeWith
292+ , write
293+ , pinnedWrite
294+ , writeRevN
278 )
295 )
279where
296where
280
297
@@ -456,6 +473,8 @@ isPinned MutArray{..} = Unboxed.isPinned arrContents
456-- we need more than that. See stg_pinnedNewAlignedByteArrayzh and
473-- we need more than that. See stg_pinnedNewAlignedByteArrayzh and
457-- allocatePinned in GHC source.
474-- allocatePinned in GHC source.
458
475
476+-- XXX Rename to emptyAlignedWith, alignSize should be first arg.
477+
459-- | @newArrayWith allocator alignment count@ allocates a new array of zero
478-- | @newArrayWith allocator alignment count@ allocates a new array of zero
460-- length and with a capacity to hold @count@ elements, using @allocator
479-- length and with a capacity to hold @count@ elements, using @allocator
461-- size alignment@ as the memory allocator function.
480-- size alignment@ as the memory allocator function.
@@ -479,12 +498,25 @@ newArrayWith alloc alignSize count = do
479 , arrBound = size
498 , arrBound = size
480 }
499 }
481
500
501+-- For arrays "nil" sounds a bit odd. empty is better. The only problem with
502+-- empty is that it is also used by the Alternative type class. But assuming we
503+-- will mostly import the Array module qualified this should be fine.
504+
505+-- | Create an empty array.
506+empty ::
507+#ifdef DEVBUILD
508+ Unbox a =>
509+#endif
510+ MutArray a
511+empty = MutArray Unboxed.empty 0 0 0
512+
513+{-# DEPRECATED nil "Please use empty instead." #-}
482nil ::
514nil ::
483#ifdef DEVBUILD
515#ifdef DEVBUILD
484 Unbox a =>
516 Unbox a =>
485#endif
517#endif
486 MutArray a
518 MutArray a
487−nil = MutArray Unboxed.nil 0 0 0
519+nil = empty
488
520
489{-# INLINE newBytesAs #-}
521{-# INLINE newBytesAs #-}
490newBytesAs :: MonadIO m =>
522newBytesAs :: MonadIO m =>
@@ -507,6 +539,7 @@ newBytesAs ps bytes = do
507--
539--
508-- /Pre-release/
540-- /Pre-release/
509{-# INLINE pinnedNewBytes #-}
541{-# INLINE pinnedNewBytes #-}
542+{-# DEPRECATED pinnedNewBytes "Please use pinnedEmptyOf with appropriate calculation" #-}
510pinnedNewBytes :: MonadIO m =>
543pinnedNewBytes :: MonadIO m =>
511#ifdef DEVBUILD
544#ifdef DEVBUILD
512 Unbox a =>
545 Unbox a =>
@@ -531,21 +564,31 @@ newAs ps =
531 (error "new: alignment is not used in unpinned arrays.")
564 (error "new: alignment is not used in unpinned arrays.")
532
565
533-- XXX can unaligned allocation be more efficient when alignment is not needed?
566-- XXX can unaligned allocation be more efficient when alignment is not needed?
534−--
567+
535−-- | Allocates an empty pinned array that can hold 'count' items. The memory of
568+-- | Allocates a pinned array of zero length but growable to the specified
536−-- the array is uninitialized and the allocation is aligned as per the 'Unboxed'
569+-- capacity without reallocation.
537−-- instance of the type.
570+{-# INLINE pinnedEmptyOf #-}
538−--
571+pinnedEmptyOf :: forall m a. (MonadIO m, Unbox a) => Int -> m (MutArray a)
572+pinnedEmptyOf = newAs Pinned
573+
574+-- XXX Deprecate in major
575+-- {-# DEPRECATED pinnedNew "Please use pinnedEmptyOf instead." #-}
539{-# INLINE pinnedNew #-}
576{-# INLINE pinnedNew #-}
540pinnedNew :: forall m a. (MonadIO m, Unbox a) => Int -> m (MutArray a)
577pinnedNew :: forall m a. (MonadIO m, Unbox a) => Int -> m (MutArray a)
541−pinnedNew = newAs Pinned
578+pinnedNew = pinnedEmptyOf
542
579
543−-- | Allocates an empty unpinned array that can hold 'count' items. The memory
580+-- | Allocates an unpinned array of zero length but growable to the specified
544−-- of the array is uninitialized.
581+-- capacity without reallocation.
545--
582--
583+{-# INLINE emptyOf #-}
584+emptyOf :: (MonadIO m, Unbox a) => Int -> m (MutArray a)
585+emptyOf = newAs Unpinned
586+
587+-- XXX Deprecate in major
588+-- {-# DEPRECATED new "Please use emptyOf instead." #-}
546{-# INLINE new #-}
589{-# INLINE new #-}
547new :: (MonadIO m, Unbox a) => Int -> m (MutArray a)
590new :: (MonadIO m, Unbox a) => Int -> m (MutArray a)
548−new = newAs Unpinned
591+new = emptyOf
549
592
550-------------------------------------------------------------------------------
593-------------------------------------------------------------------------------
551-- Random writes
594-- Random writes
@@ -890,6 +933,7 @@ grow nElems arr@MutArray{..} = do
890 else realloc req arr
933 else realloc req arr
891
934
892{-# DEPRECATED resize "Please use grow instead." #-}
935{-# DEPRECATED resize "Please use grow instead." #-}
936+{-# INLINE resize #-}
893resize :: forall m a. (MonadIO m, Unbox a) =>
937resize :: forall m a. (MonadIO m, Unbox a) =>
894 Int -> MutArray a -> m (MutArray a)
938 Int -> MutArray a -> m (MutArray a)
895resize = grow
939resize = grow
@@ -913,6 +957,7 @@ growExp nElems arr@MutArray{..} = do
913 else realloc req1 arr
957 else realloc req1 arr
914
958
915{-# DEPRECATED resizeExp "Please use growExp instead." #-}
959{-# DEPRECATED resizeExp "Please use growExp instead." #-}
960+{-# INLINE resizeExp #-}
916resizeExp :: forall m a. (MonadIO m, Unbox a) =>
961resizeExp :: forall m a. (MonadIO m, Unbox a) =>
917 Int -> MutArray a -> m (MutArray a)
962 Int -> MutArray a -> m (MutArray a)
918resizeExp = growExp
963resizeExp = growExp
@@ -1246,6 +1291,7 @@ indexReaderWith liftio (D.Stream stepi sti) = Unfold step inject
1246 D.Stop -> return D.Stop
1291 D.Stop -> return D.Stop
1247
1292
1248{-# DEPRECATED getIndicesWith "Please use indexReaderWith instead." #-}
1293{-# DEPRECATED getIndicesWith "Please use indexReaderWith instead." #-}
1294+{-# INLINE getIndicesWith #-}
1249getIndicesWith :: (Monad m, Unbox a) =>
1295getIndicesWith :: (Monad m, Unbox a) =>
1250 (forall b. IO b -> m b) -> D.Stream m Int -> Unfold m (MutArray a) a
1296 (forall b. IO b -> m b) -> D.Stream m Int -> Unfold m (MutArray a) a
1251getIndicesWith = indexReaderWith
1297getIndicesWith = indexReaderWith
@@ -1261,6 +1307,7 @@ indexReader = indexReaderWith liftIO
1261
1307
1262-- XXX DO NOT REMOVE, change the signature to use Stream instead of unfold
1308-- XXX DO NOT REMOVE, change the signature to use Stream instead of unfold
1263{-# DEPRECATED getIndices "Please use indexReader instead." #-}
1309{-# DEPRECATED getIndices "Please use indexReader instead." #-}
1310+{-# INLINE getIndices #-}
1264getIndices :: (MonadIO m, Unbox a) => Stream m Int -> Unfold m (MutArray a) a
1311getIndices :: (MonadIO m, Unbox a) => Stream m Int -> Unfold m (MutArray a) a
1265getIndices = indexReader
1312getIndices = indexReader
1266
1313
@@ -1557,7 +1604,7 @@ chunksOfAs ps n (D.Stream step state) =
1557--
1604--
1558-- Same as the following but may be more efficient:
1605-- Same as the following but may be more efficient:
1559--
1606--
1560−-- >>> chunksOf n = Stream.foldMany (MutArray.writeN n)
1607+-- >>> chunksOf n = Stream.foldMany (MutArray.createOf n)
1561--
1608--
1562-- /Pre-release/
1609-- /Pre-release/
1563{-# INLINE_NORMAL chunksOf #-}
1610{-# INLINE_NORMAL chunksOf #-}
@@ -1566,14 +1613,14 @@ chunksOf :: forall m a. (MonadIO m, Unbox a)
1566-- XXX the idiomatic implementation leads to large regression in the D.reverse'
1613-- XXX the idiomatic implementation leads to large regression in the D.reverse'
1567-- benchmark. It seems it has difficulty producing optimized code when
1614-- benchmark. It seems it has difficulty producing optimized code when
1568-- converting to StreamK. Investigate GHC optimizations.
1615-- converting to StreamK. Investigate GHC optimizations.
1569−-- chunksOf n = D.foldMany (writeN n)
1616+-- chunksOf n = D.foldMany (createOf n)
1570chunksOf = chunksOfAs Unpinned
1617chunksOf = chunksOfAs Unpinned
1571
1618
1572-- | Like 'chunksOf' but creates pinned arrays.
1619-- | Like 'chunksOf' but creates pinned arrays.
1573{-# INLINE_NORMAL pinnedChunksOf #-}
1620{-# INLINE_NORMAL pinnedChunksOf #-}
1574pinnedChunksOf :: forall m a. (MonadIO m, Unbox a)
1621pinnedChunksOf :: forall m a. (MonadIO m, Unbox a)
1575 => Int -> D.Stream m a -> D.Stream m (MutArray a)
1622 => Int -> D.Stream m a -> D.Stream m (MutArray a)
1576−-- pinnedChunksOf n = D.foldMany (pinnedWriteN n)
1623+-- pinnedChunksOf n = D.foldMany (pinnedCreateOf n)
1577pinnedChunksOf = chunksOfAs Pinned
1624pinnedChunksOf = chunksOfAs Pinned
1578
1625
1579-- | When we are buffering a stream of unknown size into an array we do not
1626-- | When we are buffering a stream of unknown size into an array we do not
@@ -1636,6 +1683,7 @@ concat (D.Stream step state) = D.Stream step' (OuterLoop state)
1636 return $ D.Yield x (InnerLoop st contents (INDEX_NEXT(p,a)) end)
1683 return $ D.Yield x (InnerLoop st contents (INDEX_NEXT(p,a)) end)
1637
1684
1638{-# DEPRECATED flattenArrays "Please use \"unfoldMany reader\" instead." #-}
1685{-# DEPRECATED flattenArrays "Please use \"unfoldMany reader\" instead." #-}
1686+{-# INLINE flattenArrays #-}
1639flattenArrays :: forall m a. (MonadIO m, Unbox a)
1687flattenArrays :: forall m a. (MonadIO m, Unbox a)
1640 => D.Stream m (MutArray a) -> D.Stream m a
1688 => D.Stream m (MutArray a) -> D.Stream m a
1641flattenArrays = concat
1689flattenArrays = concat
@@ -1672,6 +1720,7 @@ concatRev (D.Stream step state) = D.Stream step' (OuterLoop state)
1672 return $ D.Yield x (InnerLoop st contents cur start)
1720 return $ D.Yield x (InnerLoop st contents cur start)
1673
1721
1674{-# DEPRECATED flattenArraysRev "Please use \"unfoldMany readerRev\" instead." #-}
1722{-# DEPRECATED flattenArraysRev "Please use \"unfoldMany readerRev\" instead." #-}
1723+{-# INLINE flattenArraysRev #-}
1675flattenArraysRev :: forall m a. (MonadIO m, Unbox a)
1724flattenArraysRev :: forall m a. (MonadIO m, Unbox a)
1676 => D.Stream m (MutArray a) -> D.Stream m a
1725 => D.Stream m (MutArray a) -> D.Stream m a
1677flattenArraysRev = concatRev
1726flattenArraysRev = concatRev
@@ -1903,7 +1952,7 @@ foldr f z arr = D.foldr f z $ read arr
1903
1952
1904-- XXX Keep the bound intact to not lose any free space? Perf impact?
1953-- XXX Keep the bound intact to not lose any free space? Perf impact?
1905
1954
1906−-- | @writeAppendNUnsafe n arr@ appends up to @n@ input items to the supplied
1955+-- | @unsafeAppendN n arr@ appends up to @n@ input items to the supplied
1907-- array.
1956-- array.
1908--
1957--
1909-- Unsafe: Do not drive the fold beyond @n@ elements, it will lead to memory
1958-- Unsafe: Do not drive the fold beyond @n@ elements, it will lead to memory
@@ -1912,13 +1961,12 @@ foldr f z arr = D.foldr f z $ read arr
1912-- Any free space left in the array after appending @n@ elements is lost.
1961-- Any free space left in the array after appending @n@ elements is lost.
1913--
1962--
1914-- /Internal/
1963-- /Internal/
1915−{-# INLINE_NORMAL writeAppendNUnsafe #-}
1964+{-# INLINE_NORMAL unsafeAppendN #-}
1916−writeAppendNUnsafe :: forall m a. (MonadIO m, Unbox a) =>
1965+unsafeAppendN :: forall m a. (MonadIO m, Unbox a) =>
1917 Int
1966 Int
1918 -> m (MutArray a)
1967 -> m (MutArray a)
1919 -> Fold m a (MutArray a)
1968 -> Fold m a (MutArray a)
1920−writeAppendNUnsafe n action =
1921− fmap fromArrayUnsafe $ FL.foldlM' step initial
1969+unsafeAppendN n action = fmap fromArrayUnsafe $ FL.foldlM' step initial
1922
1970
1923 where
1971 where
1924
1972
@@ -1931,7 +1979,7 @@ writeAppendNUnsafe n action =
1931 -- otherwise we lose that space.
1979 -- otherwise we lose that space.
1932 arr1 <-
1980 arr1 <-
1933 if free < needed
1981 if free < needed
1934− then noinline reallocWith "writeAppendNUnsafeWith" (+ needed) needed arr
1982+ then noinline reallocWith "unsafeAppendN" (+ needed) needed arr
1935 else return arr
1983 else return arr
1936 return $ toArrayUnsafe arr1
1984 return $ toArrayUnsafe arr1
1937
1985
@@ -1939,30 +1987,51 @@ writeAppendNUnsafe n action =
1939 liftIO $ pokeAt end contents x
1987 liftIO $ pokeAt end contents x
1940 return $ ArrayUnsafe contents start (INDEX_NEXT(end,a))
1988 return $ ArrayUnsafe contents start (INDEX_NEXT(end,a))
1941
1989
1990+{-# DEPRECATED writeAppendNUnsafe "Please use unsafeAppendN instead." #-}
1991+{-# INLINE writeAppendNUnsafe #-}
1992+writeAppendNUnsafe :: forall m a. (MonadIO m, Unbox a) =>
1993+ Int
1994+ -> m (MutArray a)
1995+ -> Fold m a (MutArray a)
1996+writeAppendNUnsafe = unsafeAppendN
1997+
1942-- | Append @n@ elements to an existing array. Any free space left in the array
1998-- | Append @n@ elements to an existing array. Any free space left in the array
1943-- after appending @n@ elements is lost.
1999-- after appending @n@ elements is lost.
1944--
2000--
1945−-- >>> writeAppendN n initial = Fold.take n (MutArray.writeAppendNUnsafe n initial)
2001+-- >>> appendN n initial = Fold.take n (MutArray.unsafeAppendN n initial)
1946--
2002--
1947−{-# INLINE_NORMAL writeAppendN #-}
2003+{-# INLINE_NORMAL appendN #-}
2004+appendN :: forall m a. (MonadIO m, Unbox a) =>
2005+ Int -> m (MutArray a) -> Fold m a (MutArray a)
2006+appendN n initial = FL.take n (unsafeAppendN n initial)
2007+
2008+-- XXX Deprecate in major
2009+-- {-# DEPRECATED writeAppendN "Please use appendN instead." #-}
2010+{-# INLINE writeAppendN #-}
1948writeAppendN :: forall m a. (MonadIO m, Unbox a) =>
2011writeAppendN :: forall m a. (MonadIO m, Unbox a) =>
1949 Int -> m (MutArray a) -> Fold m a (MutArray a)
2012 Int -> m (MutArray a) -> Fold m a (MutArray a)
1950−writeAppendN n initial = FL.take n (writeAppendNUnsafe n initial)
2013+writeAppendN = appendN
1951
2014
1952−-- | @writeAppendWith realloc action@ mutates the array generated by @action@ to
2015+-- | @appendWith realloc action@ mutates the array generated by @action@ to
1953-- append the input stream. If there is no reserved space available in the
2016-- append the input stream. If there is no reserved space available in the
1954-- array it is reallocated to a size in bytes determined by @realloc oldSize@,
2017-- array it is reallocated to a size in bytes determined by @realloc oldSize@,
1955-- where @oldSize@ is the current size of the array in bytes.
2018-- where @oldSize@ is the current size of the array in bytes.
1956--
2019--
1957-- Note that the returned array may be a mutated version of original array.
2020-- Note that the returned array may be a mutated version of original array.
1958--
2021--
1959−-- >>> writeAppendWith sizer = Fold.foldlM' (MutArray.snocWith sizer)
2022+-- >>> appendWith sizer = Fold.foldlM' (MutArray.snocWith sizer)
1960--
2023--
1961-- /Pre-release/
2024-- /Pre-release/
2025+{-# INLINE appendWith #-}
2026+appendWith :: forall m a. (MonadIO m, Unbox a) =>
2027+ (Int -> Int) -> m (MutArray a) -> Fold m a (MutArray a)
2028+appendWith sizer = FL.foldlM' (snocWith sizer)
2029+
2030+{-# DEPRECATED writeAppendWith "Please use appendWith instead." #-}
1962{-# INLINE writeAppendWith #-}
2031{-# INLINE writeAppendWith #-}
1963writeAppendWith :: forall m a. (MonadIO m, Unbox a) =>
2032writeAppendWith :: forall m a. (MonadIO m, Unbox a) =>
1964 (Int -> Int) -> m (MutArray a) -> Fold m a (MutArray a)
2033 (Int -> Int) -> m (MutArray a) -> Fold m a (MutArray a)
1965−writeAppendWith sizer = FL.foldlM' (snocWith sizer)
2034+writeAppendWith = appendWith
1966
2035
1967-- | @append action@ mutates the array generated by @action@ to append the
2036-- | @append action@ mutates the array generated by @action@ to append the
1968-- input stream. If there is no reserved space available in the array it is
2037-- input stream. If there is no reserved space available in the array it is
@@ -1970,26 +2039,33 @@ writeAppendWith sizer = FL.foldlM' (snocWith sizer)
1970--
2039--
1971-- Note that the returned array may be a mutated version of original array.
2040-- Note that the returned array may be a mutated version of original array.
1972--
2041--
1973−-- >>> writeAppend = MutArray.writeAppendWith (* 2)
2042+-- >>> append = MutArray.appendWith (* 2)
1974--
2043--
2044+{-# INLINE append #-}
2045+append :: forall m a. (MonadIO m, Unbox a) =>
2046+ m (MutArray a) -> Fold m a (MutArray a)
2047+append = appendWith (* 2)
2048+
2049+-- XXX Deprecate in major
2050+-- {-# DEPRECATED writeAppend "Please use append instead." #-}
1975{-# INLINE writeAppend #-}
2051{-# INLINE writeAppend #-}
1976writeAppend :: forall m a. (MonadIO m, Unbox a) =>
2052writeAppend :: forall m a. (MonadIO m, Unbox a) =>
1977 m (MutArray a) -> Fold m a (MutArray a)
2053 m (MutArray a) -> Fold m a (MutArray a)
1978−writeAppend = writeAppendWith (* 2)
2054+writeAppend = append
1979
2055
1980-- XXX We can carry bound as well in the state to make sure we do not lose the
2056-- XXX We can carry bound as well in the state to make sure we do not lose the
1981-- remaining capacity. Need to check perf impact.
2057-- remaining capacity. Need to check perf impact.
1982--
2058--
1983−-- | Like 'writeNUnsafe' but takes a new array allocator @alloc size@ function
2059+-- | Like 'unsafeCreateOf' but takes a new array allocator @alloc size@
1984−-- as argument.
2060+-- function as argument.
1985--
2061--
1986−-- >>> writeNWithUnsafe alloc n = MutArray.writeAppendNUnsafe (alloc n) n
2062+-- >>> unsafeCreateOfWith alloc n = MutArray.unsafeAppendN (alloc n) n
1987--
2063--
1988-- /Pre-release/
2064-- /Pre-release/
1989−{-# INLINE_NORMAL writeNWithUnsafe #-}
2065+{-# INLINE_NORMAL unsafeCreateOfWith #-}
1990−writeNWithUnsafe :: forall m a. (MonadIO m, Unbox a)
2066+unsafeCreateOfWith :: forall m a. (MonadIO m, Unbox a)
1991 => (Int -> m (MutArray a)) -> Int -> Fold m a (MutArray a)
2067 => (Int -> m (MutArray a)) -> Int -> Fold m a (MutArray a)
1992−writeNWithUnsafe alloc n = fromArrayUnsafe <$> FL.foldlM' step initial
2068+unsafeCreateOfWith alloc n = fromArrayUnsafe <$> FL.foldlM' step initial
1993
2069
1994 where
2070 where
1995
2071
@@ -2000,40 +2076,64 @@ writeNWithUnsafe alloc n = fromArrayUnsafe <$> FL.foldlM' step initial
2000 return
2076 return
2001 $ ArrayUnsafe contents start (INDEX_NEXT(end,a))
2077 $ ArrayUnsafe contents start (INDEX_NEXT(end,a))
2002
2078
2079+{-# DEPRECATED writeNWithUnsafe "Please use unsafeCreateOfWith instead." #-}
2080+{-# INLINE writeNWithUnsafe #-}
2081+writeNWithUnsafe :: forall m a. (MonadIO m, Unbox a)
2082+ => (Int -> m (MutArray a)) -> Int -> Fold m a (MutArray a)
2083+writeNWithUnsafe = unsafeCreateOfWith
2084+
2003{-# INLINE_NORMAL writeNUnsafeAs #-}
2085{-# INLINE_NORMAL writeNUnsafeAs #-}
2004writeNUnsafeAs :: forall m a. (MonadIO m, Unbox a)
2086writeNUnsafeAs :: forall m a. (MonadIO m, Unbox a)
2005 => PinnedState -> Int -> Fold m a (MutArray a)
2087 => PinnedState -> Int -> Fold m a (MutArray a)
2006−writeNUnsafeAs ps = writeNWithUnsafe (newAs ps)
2088+writeNUnsafeAs ps = unsafeCreateOfWith (newAs ps)
2007
2089
2008−-- | Like 'writeN' but does not check the array bounds when writing. The fold
2090+-- | Like 'createOf' but does not check the array bounds when writing. The fold
2009-- driver must not call the step function more than 'n' times otherwise it will
2091-- driver must not call the step function more than 'n' times otherwise it will
2010-- corrupt the memory and crash. This function exists mainly because any
2092-- corrupt the memory and crash. This function exists mainly because any
2011-- conditional in the step function blocks fusion causing 10x performance
2093-- conditional in the step function blocks fusion causing 10x performance
2012-- slowdown.
2094-- slowdown.
2013--
2095--
2014−-- >>> writeNUnsafe = MutArray.writeNWithUnsafe MutArray.new
2096+-- >>> unsafeCreateOf = MutArray.unsafeCreateOfWith MutArray.emptyOf
2015--
2097--
2098+{-# INLINE_NORMAL unsafeCreateOf #-}
2099+unsafeCreateOf :: forall m a. (MonadIO m, Unbox a)
2100+ => Int -> Fold m a (MutArray a)
2101+unsafeCreateOf = writeNUnsafeAs Unpinned
2102+
2103+{-# DEPRECATED writeNUnsafe "Please use unsafeCreateOf instead." #-}
2016−{-# INLINE_NORMAL writeNUnsafe #-}
2104+{-# INLINE writeNUnsafe #-}
2017writeNUnsafe :: forall m a. (MonadIO m, Unbox a)
2105writeNUnsafe :: forall m a. (MonadIO m, Unbox a)
2018 => Int -> Fold m a (MutArray a)
2106 => Int -> Fold m a (MutArray a)
2019−writeNUnsafe = writeNUnsafeAs Unpinned
2107+writeNUnsafe = unsafeCreateOf
2020
2108
2021−-- | Like 'writeNUnsafe' but creates a pinned array.
2109+-- | Like 'unsafeCreateOf' but creates a pinned array.
2110+{-# INLINE_NORMAL unsafePinnedCreateOf #-}
2111+unsafePinnedCreateOf :: forall m a. (MonadIO m, Unbox a)
2112+ => Int -> Fold m a (MutArray a)
2113+unsafePinnedCreateOf = writeNUnsafeAs Pinned
2114+
2115+{-# DEPRECATED pinnedWriteNUnsafe "Please use unsafePinnedCreateOf instead." #-}
2022−{-# INLINE_NORMAL pinnedWriteNUnsafe #-}
2116+{-# INLINE pinnedWriteNUnsafe #-}
2023pinnedWriteNUnsafe :: forall m a. (MonadIO m, Unbox a)
2117pinnedWriteNUnsafe :: forall m a. (MonadIO m, Unbox a)
2024 => Int -> Fold m a (MutArray a)
2118 => Int -> Fold m a (MutArray a)
2025−pinnedWriteNUnsafe = writeNUnsafeAs Pinned
2119+pinnedWriteNUnsafe = unsafePinnedCreateOf
2026
2120
2027−-- | @writeNWith alloc n@ folds a maximum of @n@ elements into an array
2121+-- | @createOfWith alloc n@ folds a maximum of @n@ elements into an array
2028-- allocated using the @alloc@ function.
2122-- allocated using the @alloc@ function.
2029--
2123--
2030−-- >>> writeNWith alloc n = Fold.take n (MutArray.writeNWithUnsafe alloc n)
2124+-- >>> createOfWith alloc n = Fold.take n (MutArray.unsafeCreateOfWith alloc n)
2031−-- >>> writeNWith alloc n = MutArray.writeAppendN (alloc n) n
2125+-- >>> createOfWith alloc n = MutArray.appendN (alloc n) n
2032--
2126--
2033−{-# INLINE_NORMAL writeNWith #-}
2127+{-# INLINE_NORMAL createOfWith #-}
2128+createOfWith :: forall m a. (MonadIO m, Unbox a)
2129+ => (Int -> m (MutArray a)) -> Int -> Fold m a (MutArray a)
2130+createOfWith alloc n = FL.take n (unsafeCreateOfWith alloc n)
2131+
2132+-- {-# DEPRECATED writeNWith "Please use createOfWith instead." #-}
2133+{-# INLINE writeNWith #-}
2034writeNWith :: forall m a. (MonadIO m, Unbox a)
2134writeNWith :: forall m a. (MonadIO m, Unbox a)
2035 => (Int -> m (MutArray a)) -> Int -> Fold m a (MutArray a)
2135 => (Int -> m (MutArray a)) -> Int -> Fold m a (MutArray a)
2036−writeNWith alloc n = FL.take n (writeNWithUnsafe alloc n)
2136+writeNWith = createOfWith
2037
2137
2038{-# INLINE_NORMAL writeNAs #-}
2138{-# INLINE_NORMAL writeNAs #-}
2039writeNAs ::
2139writeNAs ::
@@ -2041,28 +2141,42 @@ writeNAs ::
2041 => PinnedState
2141 => PinnedState
2042 -> Int
2142 -> Int
2043 -> Fold m a (MutArray a)
2143 -> Fold m a (MutArray a)
2044−writeNAs ps = writeNWith (newAs ps)
2144+writeNAs ps = createOfWith (newAs ps)
2045
2145
2046−-- | @writeN n@ folds a maximum of @n@ elements from the input stream to an
2146+-- | @createOf n@ folds a maximum of @n@ elements from the input stream to an
2047-- 'MutArray'.
2147-- 'MutArray'.
2048--
2148--
2049−-- >>> writeN = MutArray.writeNWith MutArray.new
2149+-- >>> createOf = MutArray.createOfWith MutArray.new
2050−-- >>> writeN n = Fold.take n (MutArray.writeNUnsafe n)
2150+-- >>> createOf n = Fold.take n (MutArray.unsafeCreateOf n)
2051−-- >>> writeN n = MutArray.writeAppendN n (MutArray.new n)
2151+-- >>> createOf n = MutArray.appendN n (MutArray.emptyOf n)
2052--
2152--
2053−{-# INLINE_NORMAL writeN #-}
2153+{-# INLINE_NORMAL createOf #-}
2154+createOf :: forall m a. (MonadIO m, Unbox a) => Int -> Fold m a (MutArray a)
2155+createOf = writeNAs Unpinned
2156+
2157+-- XXX Deprecate in major
2158+-- {-# DEPRECATED writeN "Please use createOf instead." #-}
2159+{-# INLINE writeN #-}
2054writeN :: forall m a. (MonadIO m, Unbox a) => Int -> Fold m a (MutArray a)
2160writeN :: forall m a. (MonadIO m, Unbox a) => Int -> Fold m a (MutArray a)
2055−writeN = writeNAs Unpinned
2161+writeN = createOf
2056
2162
2057−-- | Like 'writeN' but creates a pinned array.
2163+-- | Like 'createOf' but creates a pinned array.
2164+{-# INLINE_NORMAL pinnedCreateOf #-}
2165+pinnedCreateOf ::
2166+ forall m a. (MonadIO m, Unbox a)
2167+ => Int
2168+ -> Fold m a (MutArray a)
2169+pinnedCreateOf = writeNAs Pinned
2170+
2171+{-# DEPRECATED pinnedWriteN "Please use pinnedCreateOf instead." #-}
2058−{-# INLINE_NORMAL pinnedWriteN #-}
2172+{-# INLINE pinnedWriteN #-}
2059pinnedWriteN ::
2173pinnedWriteN ::
2060 forall m a. (MonadIO m, Unbox a)
2174 forall m a. (MonadIO m, Unbox a)
2061 => Int
2175 => Int
2062 -> Fold m a (MutArray a)
2176 -> Fold m a (MutArray a)
2063−pinnedWriteN = writeNAs Pinned
2177+pinnedWriteN = pinnedCreateOf
2064
2178
2065−-- | Like writeNWithUnsafe but writes the array in reverse order.
2179+-- | Like unsafeCreateOfWith but writes the array in reverse order.
2066--
2180--
2067-- /Internal/
2181-- /Internal/
2068{-# INLINE_NORMAL writeRevNWithUnsafe #-}
2182{-# INLINE_NORMAL writeRevNWithUnsafe #-}
@@ -2083,7 +2197,7 @@ writeRevNWithUnsafe alloc n = fromArrayUnsafe <$> FL.foldlM' step initial
2083 return
2197 return
2084 $ ArrayUnsafe contents ptr end
2198 $ ArrayUnsafe contents ptr end
2085
2199
2086−-- | Like writeNWith but writes the array in reverse order.
2200+-- | Like createOfWith but writes the array in reverse order.
2087--
2201--
2088-- /Internal/
2202-- /Internal/
2089{-# INLINE_NORMAL writeRevNWith #-}
2203{-# INLINE_NORMAL writeRevNWith #-}
@@ -2091,31 +2205,36 @@ writeRevNWith :: forall m a. (MonadIO m, Unbox a)
2091 => (Int -> m (MutArray a)) -> Int -> Fold m a (MutArray a)
2205 => (Int -> m (MutArray a)) -> Int -> Fold m a (MutArray a)
2092writeRevNWith alloc n = FL.take n (writeRevNWithUnsafe alloc n)
2206writeRevNWith alloc n = FL.take n (writeRevNWithUnsafe alloc n)
2093
2207
2094−-- | Like writeN but writes the array in reverse order.
2208+-- | Like 'createOf' but writes the array in reverse order.
2095--
2209--
2096-- /Pre-release/
2210-- /Pre-release/
2097−{-# INLINE_NORMAL writeRevN #-}
2211+{-# INLINE_NORMAL revCreateOf #-}
2212+revCreateOf :: forall m a. (MonadIO m, Unbox a) => Int -> Fold m a (MutArray a)
2213+revCreateOf = writeRevNWith new
2214+
2215+{-# DEPRECATED writeRevN "Please use revCreateOf instead." #-}
2216+{-# INLINE writeRevN #-}
2098writeRevN :: forall m a. (MonadIO m, Unbox a) => Int -> Fold m a (MutArray a)
2217writeRevN :: forall m a. (MonadIO m, Unbox a) => Int -> Fold m a (MutArray a)
2099−writeRevN = writeRevNWith new
2218+writeRevN = revCreateOf
2100
2219
2101-- | @pinnedWriteNAligned align n@ folds a maximum of @n@ elements from the
2220-- | @pinnedWriteNAligned align n@ folds a maximum of @n@ elements from the
2102-- input stream to a 'MutArray' aligned to the given size.
2221-- input stream to a 'MutArray' aligned to the given size.
2103--
2222--
2104−-- >>> pinnedWriteNAligned align = MutArray.writeNWith (MutArray.pinnedNewAligned align)
2223+-- >>> pinnedWriteNAligned align = MutArray.createOfWith (MutArray.pinnedNewAligned align)
2105−-- >>> pinnedWriteNAligned align n = MutArray.writeAppendN n (MutArray.pinnedNewAligned align n)
2224+-- >>> pinnedWriteNAligned align n = MutArray.appendN n (MutArray.pinnedNewAligned align n)
2106--
2225--
2107-- /Pre-release/
2226-- /Pre-release/
2108--
2227--
2109{-# INLINE_NORMAL pinnedWriteNAligned #-}
2228{-# INLINE_NORMAL pinnedWriteNAligned #-}
2110pinnedWriteNAligned :: forall m a. (MonadIO m, Unbox a)
2229pinnedWriteNAligned :: forall m a. (MonadIO m, Unbox a)
2111 => Int -> Int -> Fold m a (MutArray a)
2230 => Int -> Int -> Fold m a (MutArray a)
2112−pinnedWriteNAligned align = writeNWith (pinnedNewAligned align)
2231+pinnedWriteNAligned align = createOfWith (pinnedNewAligned align)
2113
2232
2114-- XXX Buffer to a list instead?
2233-- XXX Buffer to a list instead?
2115
2234
2116-- | Buffer a stream into a stream of arrays.
2235-- | Buffer a stream into a stream of arrays.
2117--
2236--
2118−-- >>> buildChunks n = Fold.many (MutArray.writeN n) Fold.toStreamK
2237+-- >>> buildChunks n = Fold.many (MutArray.createOf n) Fold.toStreamK
2119--
2238--
2120-- Breaking an array into an array stream can be useful to consume a large
2239-- Breaking an array into an array stream can be useful to consume a large
2121-- array sequentially such that memory of the array is released incrementatlly.
2240-- array sequentially such that memory of the array is released incrementatlly.
@@ -2127,9 +2246,10 @@ pinnedWriteNAligned align = writeNWith (pinnedNewAligned align)
2127{-# INLINE_NORMAL buildChunks #-}
2246{-# INLINE_NORMAL buildChunks #-}
2128buildChunks :: (MonadIO m, Unbox a) =>
2247buildChunks :: (MonadIO m, Unbox a) =>
2129 Int -> Fold m a (StreamK n (MutArray a))
2248 Int -> Fold m a (StreamK n (MutArray a))
2130−buildChunks n = FL.many (writeN n) FL.toStreamK
2249+buildChunks n = FL.many (createOf n) FL.toStreamK
2131
2250
2132{-# DEPRECATED writeChunks "Please use buildChunks instead." #-}
2251{-# DEPRECATED writeChunks "Please use buildChunks instead." #-}
2252+{-# INLINE writeChunks #-}
2133writeChunks :: (MonadIO m, Unbox a) =>
2253writeChunks :: (MonadIO m, Unbox a) =>
2134 Int -> Fold m a (StreamK n (MutArray a))
2254 Int -> Fold m a (StreamK n (MutArray a))
2135writeChunks = buildChunks
2255writeChunks = buildChunks
@@ -2137,14 +2257,14 @@ writeChunks = buildChunks
2137{-# INLINE_NORMAL writeWithAs #-}
2257{-# INLINE_NORMAL writeWithAs #-}
2138writeWithAs :: forall m a. (MonadIO m, Unbox a)
2258writeWithAs :: forall m a. (MonadIO m, Unbox a)
2139 => PinnedState -> Int -> Fold m a (MutArray a)
2259 => PinnedState -> Int -> Fold m a (MutArray a)
2140−-- writeWithAs ps n = FL.rmapM rightSize $ writeAppendWith (* 2) (newAs ps n)
2260+-- writeWithAs ps n = FL.rmapM rightSize $ appendWith (* 2) (newAs ps n)
2141writeWithAs ps elemCount =
2261writeWithAs ps elemCount =
2142 FL.rmapM extract $ FL.foldlM' step initial
2262 FL.rmapM extract $ FL.foldlM' step initial
2143
2263
2144 where
2264 where
2145
2265
2146 initial = do
2266 initial = do
2147− when (elemCount < 0) $ error "writeWith: elemCount is negative"
2267+ when (elemCount < 0) $ error "createWith: elemCount is negative"
2148 newAs ps elemCount
2268 newAs ps elemCount
2149
2269
2150 step arr@(MutArray _ start end bound) x
2270 step arr@(MutArray _ start end bound) x
@@ -2157,9 +2277,9 @@ writeWithAs ps elemCount =
2157
2277
2158 extract = liftIO . rightSize
2278 extract = liftIO . rightSize
2159
2279
2160−-- XXX Compare writeWith with fromStreamD which uses an array of streams
2280+-- XXX Compare createWith with fromStreamD which uses an array of streams
2161-- implementation. We can write this using buildChunks above if that is faster.
2281-- implementation. We can write this using buildChunks above if that is faster.
2162−-- If writeWith is faster then we should use that to implement
2282+-- If createWith is faster then we should use that to implement
2163-- fromStreamD.
2283-- fromStreamD.
2164--
2284--
2165-- XXX The realloc based implementation needs to make one extra copy if we use
2285-- XXX The realloc based implementation needs to make one extra copy if we use
@@ -2174,39 +2294,56 @@ writeWithAs ps elemCount =
2174-- XXX check if GHC's memory allocator is efficient enough. We can try the C
2294-- XXX check if GHC's memory allocator is efficient enough. We can try the C
2175-- malloc to compare against.
2295-- malloc to compare against.
2176
2296
2177−-- | @writeWith minCount@ folds the whole input to a single array. The array
2297+-- | @createWith minCount@ folds the whole input to a single array. The array
2178-- starts at a size big enough to hold minCount elements, the size is doubled
2298-- starts at a size big enough to hold minCount elements, the size is doubled
2179-- every time the array needs to be grown.
2299-- every time the array needs to be grown.
2180--
2300--
2181-- /Caution! Do not use this on infinite streams./
2301-- /Caution! Do not use this on infinite streams./
2182--
2302--
2183−-- >>> f n = MutArray.writeAppendWith (* 2) (MutArray.new n)
2303+-- >>> f n = MutArray.appendWith (* 2) (MutArray.emptyOf n)
2184−-- >>> writeWith n = Fold.rmapM MutArray.rightSize (f n)
2304+-- >>> createWith n = Fold.rmapM MutArray.rightSize (f n)
2185−-- >>> writeWith n = Fold.rmapM MutArray.fromChunksK (MutArray.buildChunks n)
2305+-- >>> createWith n = Fold.rmapM MutArray.fromChunksK (MutArray.buildChunks n)
2186--
2306--
2187-- /Pre-release/
2307-- /Pre-release/
2188−{-# INLINE_NORMAL writeWith #-}
2308+{-# INLINE_NORMAL createWith #-}
2309+createWith :: forall m a. (MonadIO m, Unbox a)
2310+ => Int -> Fold m a (MutArray a)
2311+-- createWith n = FL.rmapM rightSize $ appendWith (* 2) (emptyOf n)
2312+createWith = writeWithAs Unpinned
2313+
2314+{-# DEPRECATED writeWith "Please use createWith instead." #-}
2315+{-# INLINE writeWith #-}
2189writeWith :: forall m a. (MonadIO m, Unbox a)
2316writeWith :: forall m a. (MonadIO m, Unbox a)
2190 => Int -> Fold m a (MutArray a)
2317 => Int -> Fold m a (MutArray a)
2191−-- writeWith n = FL.rmapM rightSize $ writeAppendWith (* 2) (new n)
2318+writeWith = createWith
2192−writeWith = writeWithAs Unpinned
2193
2319
2194-- | Fold the whole input to a single array.
2320-- | Fold the whole input to a single array.
2195--
2321--
2196−-- Same as 'writeWith' using an initial array size of 'arrayChunkBytes' bytes
2322+-- Same as 'createWith' using an initial array size of 'arrayChunkBytes' bytes
2197-- rounded up to the element size.
2323-- rounded up to the element size.
2198--
2324--
2199-- /Caution! Do not use this on infinite streams./
2325-- /Caution! Do not use this on infinite streams./
2200--
2326--
2327+{-# INLINE create #-}
2328+create :: forall m a. (MonadIO m, Unbox a) => Fold m a (MutArray a)
2329+create = createWith (allocBytesToElemCount (undefined :: a) arrayChunkBytes)
2330+
2331+-- XXX Deprecate in major
2332+-- {-# DEPRECATED write "Please use create instead." #-}
2201{-# INLINE write #-}
2333{-# INLINE write #-}
2202write :: forall m a. (MonadIO m, Unbox a) => Fold m a (MutArray a)
2334write :: forall m a. (MonadIO m, Unbox a) => Fold m a (MutArray a)
2203−write = writeWith (allocBytesToElemCount (undefined :: a) arrayChunkBytes)
2335+write = create
2204
2336
2205−-- | Like 'write' but creates a pinned array.
2337+-- | Like 'create' but creates a pinned array.
2338+{-# INLINE pinnedCreate #-}
2339+pinnedCreate :: forall m a. (MonadIO m, Unbox a) => Fold m a (MutArray a)
2340+pinnedCreate =
2341+ writeWithAs Pinned (allocBytesToElemCount (undefined :: a) arrayChunkBytes)
2342+
2343+{-# DEPRECATED pinnedWrite "Please use pinnedCreate instead." #-}
2206{-# INLINE pinnedWrite #-}
2344{-# INLINE pinnedWrite #-}
2207pinnedWrite :: forall m a. (MonadIO m, Unbox a) => Fold m a (MutArray a)
2345pinnedWrite :: forall m a. (MonadIO m, Unbox a) => Fold m a (MutArray a)
2208−pinnedWrite =
2346+pinnedWrite = pinnedCreate
2209− writeWithAs Pinned (allocBytesToElemCount (undefined :: a) arrayChunkBytes)
2210
2347
2211-------------------------------------------------------------------------------
2348-------------------------------------------------------------------------------
2212-- construct from streams, known size
2349-- construct from streams, known size
@@ -2226,17 +2363,18 @@ fromStreamDNAs ps limit str = do
2226 liftIO $ pokeAt ptr arrContents x
2363 liftIO $ pokeAt ptr arrContents x
2227 return $ INDEX_NEXT(ptr,a)
2364 return $ INDEX_NEXT(ptr,a)
2228
2365
2229−-- | Use the 'writeN' fold instead.
2366+-- | Use the 'createOf' fold instead.
2230--
2367--
2231−-- >>> fromStreamN n = Stream.fold (MutArray.writeN n)
2368+-- >>> fromStreamN n = Stream.fold (MutArray.createOf n)
2232--
2369--
2233{-# INLINE_NORMAL fromStreamN #-}
2370{-# INLINE_NORMAL fromStreamN #-}
2234fromStreamN :: forall m a. (MonadIO m, Unbox a)
2371fromStreamN :: forall m a. (MonadIO m, Unbox a)
2235 => Int -> D.Stream m a -> m (MutArray a)
2372 => Int -> D.Stream m a -> m (MutArray a)
2236−-- fromStreamDN n = D.fold (writeN n)
2373+-- fromStreamDN n = D.fold (createOf n)
2237fromStreamN = fromStreamDNAs Unpinned
2374fromStreamN = fromStreamDNAs Unpinned
2238
2375
2239{-# DEPRECATED fromStreamDN "Please use fromStreamN instead." #-}
2376{-# DEPRECATED fromStreamDN "Please use fromStreamN instead." #-}
2377+{-# INLINE fromStreamDN #-}
2240fromStreamDN :: forall m a. (MonadIO m, Unbox a)
2378fromStreamDN :: forall m a. (MonadIO m, Unbox a)
2241 => Int -> D.Stream m a -> m (MutArray a)
2379 => Int -> D.Stream m a -> m (MutArray a)
2242fromStreamDN = fromStreamN
2380fromStreamDN = fromStreamN
@@ -2259,20 +2397,20 @@ pinnedFromListN n xs = fromStreamDNAs Pinned n $ D.fromList xs
2259-- /Pre-release/
2397-- /Pre-release/
2260{-# INLINE fromListRevN #-}
2398{-# INLINE fromListRevN #-}
2261fromListRevN :: (MonadIO m, Unbox a) => Int -> [a] -> m (MutArray a)
2399fromListRevN :: (MonadIO m, Unbox a) => Int -> [a] -> m (MutArray a)
2262−fromListRevN n xs = D.fold (writeRevN n) $ D.fromList xs
2400+fromListRevN n xs = D.fold (revCreateOf n) $ D.fromList xs
2263
2401
2264-- | Convert a pure stream in Identity monad to a mutable array.
2402-- | Convert a pure stream in Identity monad to a mutable array.
2265{-# INLINABLE fromPureStreamN #-}
2403{-# INLINABLE fromPureStreamN #-}
2266fromPureStreamN :: (MonadIO m, Unbox a) =>
2404fromPureStreamN :: (MonadIO m, Unbox a) =>
2267 Int -> Stream Identity a -> m (MutArray a)
2405 Int -> Stream Identity a -> m (MutArray a)
2268fromPureStreamN n xs =
2406fromPureStreamN n xs =
2269− D.fold (writeN n) $ D.morphInner (return . runIdentity) xs
2407+ D.fold (createOf n) $ D.morphInner (return . runIdentity) xs
2270
2408
2271-- | Convert a pure stream in Identity monad to a mutable array.
2409-- | Convert a pure stream in Identity monad to a mutable array.
2272{-# INLINABLE fromPureStream #-}
2410{-# INLINABLE fromPureStream #-}
2273fromPureStream :: (MonadIO m, Unbox a) => Stream Identity a -> m (MutArray a)
2411fromPureStream :: (MonadIO m, Unbox a) => Stream Identity a -> m (MutArray a)
2274fromPureStream xs =
2412fromPureStream xs =
2275− D.fold write $ D.morphInner (return . runIdentity) xs
2413+ D.fold create $ D.morphInner (return . runIdentity) xs
2276
2414
2277{-# INLINABLE fromPtrN #-}
2415{-# INLINABLE fromPtrN #-}
2278fromPtrN :: MonadIO m => Int -> Ptr Word8 -> m (MutArray Word8)
2416fromPtrN :: MonadIO m => Int -> Ptr Word8 -> m (MutArray Word8)
@@ -2356,6 +2494,7 @@ fromChunksK :: (Unbox a, MonadIO m) =>
2356fromChunksK = fromChunkskAs Unpinned
2494fromChunksK = fromChunkskAs Unpinned
2357
2495
2358{-# DEPRECATED fromArrayStreamK "Please use fromChunksK instead." #-}
2496{-# DEPRECATED fromArrayStreamK "Please use fromChunksK instead." #-}
2497+{-# INLINE fromArrayStreamK #-}
2359fromArrayStreamK :: (Unbox a, MonadIO m) =>
2498fromArrayStreamK :: (Unbox a, MonadIO m) =>
2360 StreamK m (MutArray a) -> m (MutArray a)
2499 StreamK m (MutArray a) -> m (MutArray a)
2361fromArrayStreamK = fromChunksK
2500fromArrayStreamK = fromChunksK
@@ -2367,7 +2506,7 @@ fromStreamDAs ps m =
2367 arrayStreamKFromStreamDAs Unpinned m >>= fromChunkskAs ps
2506 arrayStreamKFromStreamDAs Unpinned m >>= fromChunkskAs ps
2368
2507
2369-- | Create an 'Array' from a stream. This is useful when we want to create a
2508-- | Create an 'Array' from a stream. This is useful when we want to create a
2370−-- single array from a stream of unknown size. 'writeN' is at least twice
2509+-- single array from a stream of unknown size. 'createOf' is at least twice
2371-- as efficient when the size is already known.
2510-- as efficient when the size is already known.
2372--
2511--
2373-- Note that if the input stream is too large memory allocation for the array
2512-- Note that if the input stream is too large memory allocation for the array
@@ -2379,19 +2518,19 @@ fromStreamDAs ps m =
2379fromStream :: (MonadIO m, Unbox a) => Stream m a -> m (MutArray a)
2518fromStream :: (MonadIO m, Unbox a) => Stream m a -> m (MutArray a)
2380fromStream = fromStreamDAs Unpinned
2519fromStream = fromStreamDAs Unpinned
2381
2520
2382−-- fromStream (Stream m) = P.fold write m
2521+-- fromStream (Stream m) = P.fold create m
2383-- CAUTION: a very large number (millions) of arrays can degrade performance
2522-- CAUTION: a very large number (millions) of arrays can degrade performance
2384-- due to GC overhead because we need to buffer the arrays before we flatten
2523-- due to GC overhead because we need to buffer the arrays before we flatten
2385-- all the arrays.
2524-- all the arrays.
2386--
2525--
2387−-- XXX Compare if this is faster or "fold write".
2526+-- XXX Compare if this is faster or "fold create".
2388--
2527--
2389-- | We could take the approach of doubling the memory allocation on each
2528-- | We could take the approach of doubling the memory allocation on each
2390-- overflow. This would result in more or less the same amount of copying as in
2529-- overflow. This would result in more or less the same amount of copying as in
2391-- the chunking approach. However, if we have to shrink in the end then it may
2530-- the chunking approach. However, if we have to shrink in the end then it may
2392-- result in an extra copy of the entire data.
2531-- result in an extra copy of the entire data.
2393--
2532--
2394−-- >>> fromStreamD = StreamD.fold MutArray.write
2533+-- >>> fromStreamD = StreamD.fold MutArray.create
2395--
2534--
2396{-# INLINE fromStreamD #-}
2535{-# INLINE fromStreamD #-}
2397{-# DEPRECATED fromStreamD "Please use fromStream instead." #-}
2536{-# DEPRECATED fromStreamD "Please use fromStream instead." #-}
@@ -2421,6 +2560,10 @@ fromListRev xs = fromListRevN (Prelude.length xs) xs
2421-- Cloning
2560-- Cloning
2422-------------------------------------------------------------------------------
2561-------------------------------------------------------------------------------
2423
2562
2563+-- Arrays are aligned on 64-bit boundaries. The fastest way to copy an array is
2564+-- to unsafeCast it to Word64, read it, write it to Word64 array and unsafeCast
2565+-- it again. We can use SIMD read/write as well.
2566+
2424{-# INLINE cloneAs #-}
2567{-# INLINE cloneAs #-}
2425cloneAs ::
2568cloneAs ::
2426 ( MonadIO m
2569 ( MonadIO m
@@ -2512,7 +2655,7 @@ spliceWith :: forall m a. (MonadIO m, Unbox a) =>
2512 (Int -> Int -> Int) -> MutArray a -> MutArray a -> m (MutArray a)
2655 (Int -> Int -> Int) -> MutArray a -> MutArray a -> m (MutArray a)
2513spliceWith sizer dst@(MutArray _ start end bound) src = do
2656spliceWith sizer dst@(MutArray _ start end bound) src = do
2514{-
2657{-
2515− let f = writeAppendWith (`sizer` byteLength src) (return dst)
2658+ let f = appendWith (`sizer` byteLength src) (return dst)
2516 in D.fold f (toStreamD src)
2659 in D.fold f (toStreamD src)
2517-}
2660-}
2518 assert (end <= bound) (return ())
2661 assert (end <= bound) (return ())
@@ -2687,9 +2830,13 @@ cast arr =
2687-- guarantees that unsafe calls will be performed in the calling thread. Making
2830-- guarantees that unsafe calls will be performed in the calling thread. Making
2688-- it safe to pass heap-allocated objects to unsafe functions.
2831-- it safe to pass heap-allocated objects to unsafe functions.
2689
2832
2690−-- Should we just name it asPtr, the unsafety is implicit for any pointer
2833+-- XXX Should we just name it asPtr, the unsafety is implicit for any pointer
2691-- operations. And we are safe from Haskell perspective because we will be
2834-- operations. And we are safe from Haskell perspective because we will be
2692-- pinning the memory.
2835-- pinning the memory.
2836+--
2837+-- XXX we cannot pass the length of the ptr here as in some cases it may not be
2838+-- available e.g. a null terminated C string. However, we can create another
2839+-- flavor of the API e.g. asPtrN.
2693
2840
2694-- | Use a @MutArray a@ as @Ptr a@. This is useful when we want to pass an
2841-- | Use a @MutArray a@ as @Ptr a@. This is useful when we want to pass an
2695-- array as a pointer to some operating system call or to a "safe" FFI call.
2842-- array as a pointer to some operating system call or to a "safe" FFI call.
core/src/Streamly/Internal/Data/MutByteArray/Type.hs+17−10
@@ -13,7 +13,7 @@ module Streamly.Internal.Data.MutByteArray.Type
13 -- ** MutByteArray
13 -- ** MutByteArray
14 MutByteArray(..)
14 MutByteArray(..)
15 , MutableByteArray
15 , MutableByteArray
16− , getMutableByteArray#
16+ , getMutableByteArray# -- XXX getMutByteArray#
17
17
18 -- ** Pinning
18 -- ** Pinning
19 , PinnedState(..)
19 , PinnedState(..)
@@ -22,20 +22,23 @@ module Streamly.Internal.Data.MutByteArray.Type
22 , unpin
22 , unpin
23
23
24 -- ** Allocation
24 -- ** Allocation
25− , nil
25+ , empty
26− , newBytesAs
26+ , newBytesAs -- XXX should be removed
27 , new
27 , new
28 , pinnedNew
28 , pinnedNew
29− , pinnedNewAlignedBytes
29+ , pinnedNewAlignedBytes -- XXX should be removed
30
30
31 -- ** Access
31 -- ** Access
32− , sizeOfMutableByteArray
32+ , sizeOfMutableByteArray -- XXX length
33 , putSliceUnsafe
33 , putSliceUnsafe
34 , cloneSliceUnsafeAs
34 , cloneSliceUnsafeAs
35 , cloneSliceUnsafe
35 , cloneSliceUnsafe
36 , pinnedCloneSliceUnsafe
36 , pinnedCloneSliceUnsafe
37− , asPtrUnsafe
37+ , asPtrUnsafe -- XXX unsafePinnedAsPtr
38− , asUnpinnedPtrUnsafe
38+ , asUnpinnedPtrUnsafe -- XXX unsafeAsPtr
39+
40+ -- ** Deprecated
41+ , nil
39 ) where
42 ) where
40
43
41import Control.Monad.IO.Class (MonadIO(..))
44import Control.Monad.IO.Class (MonadIO(..))
@@ -138,9 +141,13 @@ asUnpinnedPtrUnsafe arr f = do
138-- Creation
141-- Creation
139--------------------------------------------------------------------------------
142--------------------------------------------------------------------------------
140
143
141−{-# NOINLINE nil #-}
144+{-# NOINLINE empty #-}
145+empty :: MutByteArray
146+empty = unsafePerformIO $ new 0
147+
148+{-# DEPRECATED nil "Please use empty instead" #-}
142nil :: MutByteArray
149nil :: MutByteArray
143−nil = unsafePerformIO $ new 0
150+nil = empty
144
151
145-- XXX add "newRounded" to round up the large size to the next page boundary
152-- XXX add "newRounded" to round up the large size to the next page boundary
146-- and return the allocated size.
153-- and return the allocated size.
@@ -157,7 +164,7 @@ new (I# nbytes) = IO $ \s ->
157{-# INLINE pinnedNew #-}
164{-# INLINE pinnedNew #-}
158pinnedNew :: Int -> IO MutByteArray
165pinnedNew :: Int -> IO MutByteArray
159pinnedNew nbytes | nbytes < 0 =
166pinnedNew nbytes | nbytes < 0 =
160− errorWithoutStackTrace "pinnedNewByteArray: size must be >= 0"
167+ errorWithoutStackTrace "pinnedNew: size must be >= 0"
161pinnedNew (I# nbytes) = IO $ \s ->
168pinnedNew (I# nbytes) = IO $ \s ->
162 case newPinnedByteArray# nbytes s of
169 case newPinnedByteArray# nbytes s of
163 (# s', mbarr# #) ->
170 (# s', mbarr# #) ->
core/src/Streamly/Internal/FileSystem/Handle.hs+3−1
@@ -167,6 +167,8 @@ import qualified Streamly.Internal.Data.StreamK.Type as K (mkStream)
167-- Array IO (Input)
167-- Array IO (Input)
168-------------------------------------------------------------------------------
168-------------------------------------------------------------------------------
169
169
170+-- XXX add an API that compacts the arrays to an exact size.
171+
170-- | Read a 'ByteArray' consisting of one or more bytes from a file handle. If
172-- | Read a 'ByteArray' consisting of one or more bytes from a file handle. If
171-- no data is available on the handle it blocks until at least one byte becomes
173-- no data is available on the handle it blocks until at least one byte becomes
172-- available. If any data is available then it immediately returns that data
174-- available. If any data is available then it immediately returns that data
@@ -176,7 +178,7 @@ import qualified Streamly.Internal.Data.StreamK.Type as K (mkStream)
176{-# INLINABLE getChunk #-}
178{-# INLINABLE getChunk #-}
177getChunk :: MonadIO m => Int -> Handle -> m (Array Word8)
179getChunk :: MonadIO m => Int -> Handle -> m (Array Word8)
178getChunk size h = liftIO $ do
180getChunk size h = liftIO $ do
179− arr <- MArray.pinnedNewBytes size
181+ arr :: MArray.MutArray Word8 <- MArray.pinnedEmptyOf size
180 -- ptr <- mallocPlainForeignPtrAlignedBytes size (alignment (undefined :: Word8))
182 -- ptr <- mallocPlainForeignPtrAlignedBytes size (alignment (undefined :: Word8))
181 MArray.asPtrUnsafe arr $ \p -> do
183 MArray.asPtrUnsafe arr $ \p -> do
182 n <- hGetBufSome h p size
184 n <- hGetBufSome h p size
src/Streamly/Internal/Network/Socket.hs+4−2
@@ -101,7 +101,7 @@ import qualified Streamly.Internal.Data.Array as A
101 ( unsafeFreeze, asPtrUnsafe, byteLength, pinnedChunksOf,
101 ( unsafeFreeze, asPtrUnsafe, byteLength, pinnedChunksOf,
102 pinnedWriteN, pinnedWriteNUnsafe, lCompactGE )
102 pinnedWriteN, pinnedWriteNUnsafe, lCompactGE )
103import qualified Streamly.Internal.Data.MutArray as MArray
103import qualified Streamly.Internal.Data.MutArray as MArray
104− (MutArray(..), asPtrUnsafe, pinnedNewBytes)
104+ (MutArray(..), asPtrUnsafe, pinnedEmptyOf)
105import qualified Streamly.Internal.Data.Stream as S (fromStreamK, Stream(..), Step(..))
105import qualified Streamly.Internal.Data.Stream as S (fromStreamK, Stream(..), Step(..))
106import qualified Streamly.Internal.Data.StreamK as K (mkStream)
106import qualified Streamly.Internal.Data.StreamK as K (mkStream)
107
107
@@ -252,6 +252,8 @@ accept tcpListenQ spec addr =
252-- Array IO (Input)
252-- Array IO (Input)
253-------------------------------------------------------------------------------
253-------------------------------------------------------------------------------
254
254
255+-- XXX add an API that compacts the arrays to an exact size.
256+
255{-# INLINABLE readArrayUptoWith #-}
257{-# INLINABLE readArrayUptoWith #-}
256readArrayUptoWith
258readArrayUptoWith
257 :: (h -> Ptr Word8 -> Int -> IO Int)
259 :: (h -> Ptr Word8 -> Int -> IO Int)
@@ -259,7 +261,7 @@ readArrayUptoWith
259 -> h
261 -> h
260 -> IO (Array Word8)
262 -> IO (Array Word8)
261readArrayUptoWith f size h = do
263readArrayUptoWith f size h = do
262− arr <- MArray.pinnedNewBytes size
264+ arr <- MArray.pinnedEmptyOf size
263 -- ptr <- mallocPlainForeignPtrAlignedBytes size (alignment (undefined :: Word8))
265 -- ptr <- mallocPlainForeignPtrAlignedBytes size (alignment (undefined :: Word8))
264 MArray.asPtrUnsafe arr $ \p -> do
266 MArray.asPtrUnsafe arr $ \p -> do
265 n <- f h p size
267 n <- f h p size