on master help sign in
Non-Functional renaming changes in the array APIs
df5d151Adithya Kumar
core/src/Streamly/Internal/Data/Array/Mut/Type.hs+27−17
@@ -53,10 +53,9 @@ module Streamly.Internal.Data.Array.Mut.Type
53 , writeNWithUnsafe
53 , writeNWithUnsafe
54 , writeNWith
54 , writeNWith
55 , writeNUnsafe
55 , writeNUnsafe
56− , writeNUnsafeAs
56+ , pinnedWriteNUnsafe
57 , writeN
57 , writeN
58 , pinnedWriteN
58 , pinnedWriteN
59− , writeNAs
60 , pinnedWriteNAligned
59 , pinnedWriteNAligned
61
60
62 , writeWith
61 , writeWith
@@ -189,8 +188,7 @@ module Streamly.Internal.Data.Array.Mut.Type
189
188
190 -- ** Construct from streams
189 -- ** Construct from streams
191 , chunksOf
190 , chunksOf
192− , chunksOfAs
191+ , pinnedChunksOf
193− , arrayStreamKFromStreamD
194 , writeChunks
192 , writeChunks
195
193
196 -- ** Eliminate to streams
194 -- ** Eliminate to streams
@@ -429,7 +427,7 @@ newBytesAs :: MonadIO m =>
429#endif
427#endif
430 PinnedState -> Int -> m (MutArray a)
428 PinnedState -> Int -> m (MutArray a)
431newBytesAs ps bytes = do
429newBytesAs ps bytes = do
432− contents <- liftIO $ Unboxed.newBytes ps bytes
430+ contents <- liftIO $ Unboxed.newBytesAs ps bytes
433 return $ MutArray
431 return $ MutArray
434 { arrContents = contents
432 { arrContents = contents
435 , arrStart = 0
433 , arrStart = 0
@@ -463,7 +461,7 @@ pinnedNewAligned =
463newAs :: (MonadIO m, Unbox a) => PinnedState -> Int -> m (MutArray a)
461newAs :: (MonadIO m, Unbox a) => PinnedState -> Int -> m (MutArray a)
464newAs ps =
462newAs ps =
465 newArrayWith
463 newArrayWith
466− (\s _ -> liftIO $ Unboxed.newBytes ps s)
464+ (\s _ -> liftIO $ Unboxed.newBytesAs ps s)
467 (error "new: alignment is not used in unpinned arrays.")
465 (error "new: alignment is not used in unpinned arrays.")
468
466
469-- XXX can unaligned allocation be more efficient when alignment is not needed?
467-- XXX can unaligned allocation be more efficient when alignment is not needed?
@@ -742,7 +740,7 @@ reallocExplicit elemSize newCapacityInBytes MutArray{..} = do
742 contents <-
740 contents <-
743 if Unboxed.isPinned arrContents
741 if Unboxed.isPinned arrContents
744 then Unboxed.pinnedNewBytes newCapMaxInBytes
742 then Unboxed.pinnedNewBytes newCapMaxInBytes
745− else Unboxed.newUnpinnedBytes newCapMaxInBytes
743+ else Unboxed.newBytes newCapMaxInBytes
746 let !(MutableByteArray mbarrFrom#) = arrContents
744 let !(MutableByteArray mbarrFrom#) = arrContents
747 !(MutableByteArray mbarrTo#) = contents
745 !(MutableByteArray mbarrTo#) = contents
748
746
@@ -1362,6 +1360,16 @@ chunksOf :: forall m a. (MonadIO m, Unbox a)
1362-- chunksOf n = D.foldMany (writeN n)
1360-- chunksOf n = D.foldMany (writeN n)
1363chunksOf = chunksOfAs Unpinned
1361chunksOf = chunksOfAs Unpinned
1364
1362
1363+-- | Like 'chunksOf' but creates pinned arrays.
1364+{-# INLINE_NORMAL pinnedChunksOf #-}
1365+pinnedChunksOf :: forall m a. (MonadIO m, Unbox a)
1366+ => Int -> D.Stream m a -> D.Stream m (MutArray a)
1367+-- pinnedChunksOf n = D.foldMany (pinnedWriteN n)
1368+pinnedChunksOf = chunksOfAs Pinned
1369+
1370+-- XXX This should take a PinnedState
1371+-- XXX buffer to a list instead?
1372+-- | Buffer the stream into arrays in memory.
1365{-# INLINE arrayStreamKFromStreamDAs #-}
1373{-# INLINE arrayStreamKFromStreamDAs #-}
1366arrayStreamKFromStreamDAs :: forall m a. (MonadIO m, Unbox a) =>
1374arrayStreamKFromStreamDAs :: forall m a. (MonadIO m, Unbox a) =>
1367 PinnedState -> D.Stream m a -> m (StreamK m (MutArray a))
1375 PinnedState -> D.Stream m a -> m (StreamK m (MutArray a))
@@ -1369,14 +1377,6 @@ arrayStreamKFromStreamDAs ps =
1369 let n = allocBytesToElemCount (undefined :: a) defaultChunkSize
1377 let n = allocBytesToElemCount (undefined :: a) defaultChunkSize
1370 in D.foldr K.cons K.nil . chunksOfAs ps n
1378 in D.foldr K.cons K.nil . chunksOfAs ps n
1371
1379
1372−-- XXX This should take a PinnedState
1373−-- XXX buffer to a list instead?
1374−-- | Buffer the stream into arrays in memory.
1375−{-# INLINE arrayStreamKFromStreamD #-}
1376−arrayStreamKFromStreamD :: forall m a. (MonadIO m, Unbox a) =>
1377− D.Stream m a -> m (StreamK m (MutArray a))
1378−arrayStreamKFromStreamD = arrayStreamKFromStreamDAs Pinned
1379−
1380-------------------------------------------------------------------------------
1380-------------------------------------------------------------------------------
1381-- Streams of arrays - Flattening
1381-- Streams of arrays - Flattening
1382-------------------------------------------------------------------------------
1382-------------------------------------------------------------------------------
@@ -1787,6 +1787,12 @@ writeNUnsafe :: forall m a. (MonadIO m, Unbox a)
1787 => Int -> Fold m a (MutArray a)
1787 => Int -> Fold m a (MutArray a)
1788writeNUnsafe = writeNUnsafeAs Unpinned
1788writeNUnsafe = writeNUnsafeAs Unpinned
1789
1789
1790+-- | Like 'writeNUnsafe' but creates a pinned array.
1791+{-# INLINE_NORMAL pinnedWriteNUnsafe #-}
1792+pinnedWriteNUnsafe :: forall m a. (MonadIO m, Unbox a)
1793+ => Int -> Fold m a (MutArray a)
1794+pinnedWriteNUnsafe = writeNUnsafeAs Pinned
1795+
1790-- | @writeNWith alloc n@ folds a maximum of @n@ elements into an array
1796-- | @writeNWith alloc n@ folds a maximum of @n@ elements into an array
1791-- allocated using the @alloc@ function.
1797-- allocated using the @alloc@ function.
1792--
1798--
@@ -1817,6 +1823,7 @@ writeNAs ps = writeNWith (newAs ps)
1817writeN :: forall m a. (MonadIO m, Unbox a) => Int -> Fold m a (MutArray a)
1823writeN :: forall m a. (MonadIO m, Unbox a) => Int -> Fold m a (MutArray a)
1818writeN = writeNAs Unpinned
1824writeN = writeNAs Unpinned
1819
1825
1826+-- | Like 'writeN' but creates a pinned array.
1820{-# INLINE_NORMAL pinnedWriteN #-}
1827{-# INLINE_NORMAL pinnedWriteN #-}
1821pinnedWriteN ::
1828pinnedWriteN ::
1822 forall m a. (MonadIO m, Unbox a)
1829 forall m a. (MonadIO m, Unbox a)
@@ -1959,6 +1966,7 @@ writeWith = writeWithAs Unpinned
1959write :: forall m a. (MonadIO m, Unbox a) => Fold m a (MutArray a)
1966write :: forall m a. (MonadIO m, Unbox a) => Fold m a (MutArray a)
1960write = writeWith (allocBytesToElemCount (undefined :: a) arrayChunkBytes)
1967write = writeWith (allocBytesToElemCount (undefined :: a) arrayChunkBytes)
1961
1968
1969+-- | Like 'write' but creates a pinned array.
1962{-# INLINE pinnedWrite #-}
1970{-# INLINE pinnedWrite #-}
1963pinnedWrite :: forall m a. (MonadIO m, Unbox a) => Fold m a (MutArray a)
1971pinnedWrite :: forall m a. (MonadIO m, Unbox a) => Fold m a (MutArray a)
1964pinnedWrite =
1972pinnedWrite =
@@ -2000,6 +2008,7 @@ fromStreamDN = fromStreamDNAs Unpinned
2000fromListN :: (MonadIO m, Unbox a) => Int -> [a] -> m (MutArray a)
2008fromListN :: (MonadIO m, Unbox a) => Int -> [a] -> m (MutArray a)
2001fromListN n xs = fromStreamDN n $ D.fromList xs
2009fromListN n xs = fromStreamDN n $ D.fromList xs
2002
2010
2011+-- | Like 'fromListN' but creates a pinned array.
2003{-# INLINABLE pinnedFromListN #-}
2012{-# INLINABLE pinnedFromListN #-}
2004pinnedFromListN :: (MonadIO m, Unbox a) => Int -> [a] -> m (MutArray a)
2013pinnedFromListN :: (MonadIO m, Unbox a) => Int -> [a] -> m (MutArray a)
2005pinnedFromListN n xs = fromStreamDNAs Pinned n $ D.fromList xs
2014pinnedFromListN n xs = fromStreamDNAs Pinned n $ D.fromList xs
@@ -2063,6 +2072,7 @@ fromStreamD = fromStreamDAs Unpinned
2063fromList :: (MonadIO m, Unbox a) => [a] -> m (MutArray a)
2072fromList :: (MonadIO m, Unbox a) => [a] -> m (MutArray a)
2064fromList xs = fromStreamD $ D.fromList xs
2073fromList xs = fromStreamD $ D.fromList xs
2065
2074
2075+-- | Like 'fromList' but creates a pinned array.
2066{-# INLINE pinnedFromList #-}
2076{-# INLINE pinnedFromList #-}
2067pinnedFromList :: (MonadIO m, Unbox a) => [a] -> m (MutArray a)
2077pinnedFromList :: (MonadIO m, Unbox a) => [a] -> m (MutArray a)
2068pinnedFromList xs = fromStreamDAs Pinned $ D.fromList xs
2078pinnedFromList xs = fromStreamDAs Pinned $ D.fromList xs
@@ -2111,8 +2121,8 @@ spliceCopy arr1 arr2 = liftIO $ do
2111 let newLen = len1 + len2
2121 let newLen = len1 + len2
2112 newArrContents <-
2122 newArrContents <-
2113 if Unboxed.isPinned (arrContents arr1)
2123 if Unboxed.isPinned (arrContents arr1)
2114− then Unboxed.newPinnedBytes newLen
2124+ then Unboxed.pinnedNewBytes newLen
2115− else Unboxed.newUnpinnedBytes newLen
2125+ else Unboxed.newBytes newLen
2116 let len = len1 + len2
2126 let len = len1 + len2
2117 newArr = MutArray newArrContents 0 len len
2127 newArr = MutArray newArrContents 0 len len
2118 putSliceUnsafe arr1 start1 newArr 0 len1
2128 putSliceUnsafe arr1 start1 newArr 0 len1
core/src/Streamly/Internal/Data/Array/Type.hs+19−24
@@ -68,9 +68,8 @@ module Streamly.Internal.Data.Array.Type
68 , writeWith
68 , writeWith
69 , writeN
69 , writeN
70 , pinnedWriteN
70 , pinnedWriteN
71− , writeNAs
72 , writeNUnsafe
71 , writeNUnsafe
73− , writeNUnsafeAs
72+ , pinnedWriteNUnsafe
74 , MA.ArrayUnsafe (..)
73 , MA.ArrayUnsafe (..)
75 , pinnedWriteNAligned
74 , pinnedWriteNAligned
76 , write
75 , write
@@ -79,7 +78,7 @@ module Streamly.Internal.Data.Array.Type
79
78
80 -- * Streams of arrays
79 -- * Streams of arrays
81 , chunksOf
80 , chunksOf
82− , chunksOfAs
81+ , pinnedChunksOf
83 , bufferChunks
82 , bufferChunks
84 , flattenArrays
83 , flattenArrays
85 , flattenArraysRev
84 , flattenArraysRev
@@ -103,7 +102,7 @@ import GHC.Ptr (Ptr(..))
103import Streamly.Internal.Data.Array.Mut.Type (MutArray(..), MutableByteArray)
102import Streamly.Internal.Data.Array.Mut.Type (MutArray(..), MutableByteArray)
104import Streamly.Internal.Data.Fold.Type (Fold(..))
103import Streamly.Internal.Data.Fold.Type (Fold(..))
105import Streamly.Internal.Data.Stream.StreamD.Type (Stream)
104import Streamly.Internal.Data.Stream.StreamD.Type (Stream)
106−import Streamly.Internal.Data.Unbox (PinnedState, Unbox(..))
105+import Streamly.Internal.Data.Unbox (Unbox(..))
107import Streamly.Internal.Data.Unfold.Type (Unfold(..))
106import Streamly.Internal.Data.Unfold.Type (Unfold(..))
108import Text.Read (readPrec)
107import Text.Read (readPrec)
109
108
@@ -256,7 +255,7 @@ splice arr1 arr2 =
256fromListN :: Unbox a => Int -> [a] -> Array a
255fromListN :: Unbox a => Int -> [a] -> Array a
257fromListN n xs = unsafePerformIO $ unsafeFreeze <$> MA.fromListN n xs
256fromListN n xs = unsafePerformIO $ unsafeFreeze <$> MA.fromListN n xs
258
257
259−-- | Like 'fromListN' but creates a pinned 'Array'
258+-- | Like 'fromListN' but creates a pinned array.
260{-# INLINABLE pinnedFromListN #-}
259{-# INLINABLE pinnedFromListN #-}
261pinnedFromListN :: Unbox a => Int -> [a] -> Array a
260pinnedFromListN :: Unbox a => Int -> [a] -> Array a
262pinnedFromListN n xs =
261pinnedFromListN n xs =
@@ -277,6 +276,7 @@ fromListRevN n xs = unsafePerformIO $ unsafeFreeze <$> MA.fromListRevN n xs
277fromList :: Unbox a => [a] -> Array a
276fromList :: Unbox a => [a] -> Array a
278fromList xs = unsafePerformIO $ unsafeFreeze <$> MA.fromList xs
277fromList xs = unsafePerformIO $ unsafeFreeze <$> MA.fromList xs
279
278
279+-- | Like 'fromList' but creates a pinned array.
280{-# INLINE pinnedFromList #-}
280{-# INLINE pinnedFromList #-}
281pinnedFromList :: Unbox a => [a] -> Array a
281pinnedFromList :: Unbox a => [a] -> Array a
282pinnedFromList xs = unsafePerformIO $ unsafeFreeze <$> MA.pinnedFromList xs
282pinnedFromList xs = unsafePerformIO $ unsafeFreeze <$> MA.pinnedFromList xs
@@ -308,11 +308,6 @@ bufferChunks :: (MonadIO m, Unbox a) =>
308 D.Stream m a -> m (K.StreamK m (Array a))
308 D.Stream m a -> m (K.StreamK m (Array a))
309bufferChunks m = D.foldr K.cons K.nil $ chunksOf defaultChunkSize m
309bufferChunks m = D.foldr K.cons K.nil $ chunksOf defaultChunkSize m
310
310
311−{-# INLINE_NORMAL chunksOfAs #-}
312−chunksOfAs :: forall m a. (MonadIO m, Unbox a)
313− => PinnedState -> Int -> D.Stream m a -> D.Stream m (Array a)
314−chunksOfAs ps n str = D.map unsafeFreeze $ MA.chunksOfAs ps n str
315−
316-- | @chunksOf n stream@ groups the elements in the input stream into arrays of
311-- | @chunksOf n stream@ groups the elements in the input stream into arrays of
317-- @n@ elements each.
312-- @n@ elements each.
318--
313--
@@ -326,6 +321,12 @@ chunksOf :: forall m a. (MonadIO m, Unbox a)
326 => Int -> D.Stream m a -> D.Stream m (Array a)
321 => Int -> D.Stream m a -> D.Stream m (Array a)
327chunksOf n str = D.map unsafeFreeze $ MA.chunksOf n str
322chunksOf n str = D.map unsafeFreeze $ MA.chunksOf n str
328
323
324+-- | Like 'chunksOf' but creates pinned arrays.
325+{-# INLINE_NORMAL pinnedChunksOf #-}
326+pinnedChunksOf :: forall m a. (MonadIO m, Unbox a)
327+ => Int -> D.Stream m a -> D.Stream m (Array a)
328+pinnedChunksOf n str = D.map unsafeFreeze $ MA.pinnedChunksOf n str
329+
329-- | Use the "read" unfold instead.
330-- | Use the "read" unfold instead.
330--
331--
331-- @flattenArrays = unfoldMany read@
332-- @flattenArrays = unfoldMany read@
@@ -488,14 +489,6 @@ toList s = build (\c n -> toListFB c n s)
488-- Folds
489-- Folds
489-------------------------------------------------------------------------------
490-------------------------------------------------------------------------------
490
491
491−{-# INLINE_NORMAL writeNAs #-}
492−writeNAs ::
493− forall m a. (MonadIO m, Unbox a)
494− => PinnedState
495− -> Int
496− -> Fold m a (Array a)
497−writeNAs ps = fmap unsafeFreeze . MA.writeNAs ps
498−
499-- | @writeN n@ folds a maximum of @n@ elements from the input stream to an
492-- | @writeN n@ folds a maximum of @n@ elements from the input stream to an
500-- 'Array'.
493-- 'Array'.
501--
494--
@@ -503,6 +496,7 @@ writeNAs ps = fmap unsafeFreeze . MA.writeNAs ps
503writeN :: forall m a. (MonadIO m, Unbox a) => Int -> Fold m a (Array a)
496writeN :: forall m a. (MonadIO m, Unbox a) => Int -> Fold m a (Array a)
504writeN = fmap unsafeFreeze . MA.writeN
497writeN = fmap unsafeFreeze . MA.writeN
505
498
499+-- | Like 'fromListN' but creates a pinned array.
506{-# INLINE_NORMAL pinnedWriteN #-}
500{-# INLINE_NORMAL pinnedWriteN #-}
507pinnedWriteN :: forall m a. (MonadIO m, Unbox a) => Int -> Fold m a (Array a)
501pinnedWriteN :: forall m a. (MonadIO m, Unbox a) => Int -> Fold m a (Array a)
508pinnedWriteN = fmap unsafeFreeze . MA.pinnedWriteN
502pinnedWriteN = fmap unsafeFreeze . MA.pinnedWriteN
@@ -517,11 +511,6 @@ pinnedWriteNAligned :: forall m a. (MonadIO m, Unbox a)
517 => Int -> Int -> Fold m a (Array a)
511 => Int -> Int -> Fold m a (Array a)
518pinnedWriteNAligned alignSize = fmap unsafeFreeze . MA.pinnedWriteNAligned alignSize
512pinnedWriteNAligned alignSize = fmap unsafeFreeze . MA.pinnedWriteNAligned alignSize
519
513
520−{-# INLINE_NORMAL writeNUnsafeAs #-}
521−writeNUnsafeAs :: forall m a. (MonadIO m, Unbox a)
522− => PinnedState -> Int -> Fold m a (Array a)
523−writeNUnsafeAs ps n = unsafeFreeze <$> MA.writeNUnsafeAs ps n
524−
525-- | Like 'writeN' but does not check the array bounds when writing. The fold
514-- | Like 'writeN' but does not check the array bounds when writing. The fold
526-- driver must not call the step function more than 'n' times otherwise it will
515-- driver must not call the step function more than 'n' times otherwise it will
527-- corrupt the memory and crash. This function exists mainly because any
516-- corrupt the memory and crash. This function exists mainly because any
@@ -533,6 +522,11 @@ writeNUnsafe :: forall m a. (MonadIO m, Unbox a)
533 => Int -> Fold m a (Array a)
522 => Int -> Fold m a (Array a)
534writeNUnsafe n = unsafeFreeze <$> MA.writeNUnsafe n
523writeNUnsafe n = unsafeFreeze <$> MA.writeNUnsafe n
535
524
525+{-# INLINE_NORMAL pinnedWriteNUnsafe #-}
526+pinnedWriteNUnsafe :: forall m a. (MonadIO m, Unbox a)
527+ => Int -> Fold m a (Array a)
528+pinnedWriteNUnsafe n = unsafeFreeze <$> MA.pinnedWriteNUnsafe n
529+
536{-# INLINE_NORMAL writeWith #-}
530{-# INLINE_NORMAL writeWith #-}
537writeWith :: forall m a. (MonadIO m, Unbox a)
531writeWith :: forall m a. (MonadIO m, Unbox a)
538 => Int -> Fold m a (Array a)
532 => Int -> Fold m a (Array a)
@@ -547,9 +541,10 @@ writeWith elemCount = unsafeFreeze <$> MA.writeWith elemCount
547write :: forall m a. (MonadIO m, Unbox a) => Fold m a (Array a)
541write :: forall m a. (MonadIO m, Unbox a) => Fold m a (Array a)
548write = fmap unsafeFreeze MA.write
542write = fmap unsafeFreeze MA.write
549
543
544+-- | Like 'write' but creates a pinned array.
550{-# INLINE pinnedWrite #-}
545{-# INLINE pinnedWrite #-}
551pinnedWrite :: forall m a. (MonadIO m, Unbox a) => Fold m a (Array a)
546pinnedWrite :: forall m a. (MonadIO m, Unbox a) => Fold m a (Array a)
552−pinnedWrite = fmap unsafeFreeze MA.write
547+pinnedWrite = fmap unsafeFreeze MA.pinnedWrite
553
548
554-- | Fold "step" has a dependency on "initial", and each step is dependent on
549-- | Fold "step" has a dependency on "initial", and each step is dependent on
555-- the previous invocation of step due to state passing, finally extract
550-- the previous invocation of step due to state passing, finally extract
core/src/Streamly/Internal/Data/IORef/Unboxed.hs+2−2
@@ -43,7 +43,7 @@ import Control.Monad.IO.Class (MonadIO(..))
43import Streamly.Internal.Data.Unbox
43import Streamly.Internal.Data.Unbox
44 ( MutableByteArray(..)
44 ( MutableByteArray(..)
45 , Unbox(..)
45 , Unbox(..)
46− , newUnpinnedBytes
46+ , newBytes
47 , sizeOf
47 , sizeOf
48 )
48 )
49
49
@@ -58,7 +58,7 @@ newtype IORef a = IORef MutableByteArray
58{-# INLINE newIORef #-}
58{-# INLINE newIORef #-}
59newIORef :: forall a. Unbox a => a -> IO (IORef a)
59newIORef :: forall a. Unbox a => a -> IO (IORef a)
60newIORef x = do
60newIORef x = do
61− var <- newUnpinnedBytes (sizeOf (Proxy :: Proxy a))
61+ var <- newBytes (sizeOf (Proxy :: Proxy a))
62 pokeByteIndex 0 var x
62 pokeByteIndex 0 var x
63 return $ IORef var
63 return $ IORef var
64
64
core/src/Streamly/Internal/Data/Unbox.hs+13−13
@@ -14,8 +14,8 @@ module Streamly.Internal.Data.Unbox
14 , isPinned
14 , isPinned
15 , pin
15 , pin
16 , unpin
16 , unpin
17+ , newBytesAs
17 , newBytes
18 , newBytes
18− , newUnpinnedBytes
19 , pinnedNewBytes
19 , pinnedNewBytes
20 , pinnedNewAlignedBytes
20 , pinnedNewAlignedBytes
21 , nil
21 , nil
@@ -108,13 +108,13 @@ sizeOfMutableByteArray (MutableByteArray arr) =
108
108
109{-# NOINLINE nil #-}
109{-# NOINLINE nil #-}
110nil :: MutableByteArray
110nil :: MutableByteArray
111−nil = unsafePerformIO $ newUnpinnedBytes 0
111+nil = unsafePerformIO $ newBytes 0
112
112
113−{-# INLINE newUnpinnedBytes #-}
113+{-# INLINE newBytes #-}
114−newUnpinnedBytes :: Int -> IO MutableByteArray
114+newBytes :: Int -> IO MutableByteArray
115−newUnpinnedBytes nbytes | nbytes < 0 =
115+newBytes nbytes | nbytes < 0 =
116− errorWithoutStackTrace "newUnpinnedBytes: size must be >= 0"
116+ errorWithoutStackTrace "newBytes: size must be >= 0"
117−newUnpinnedBytes (I# nbytes) = IO $ \s ->
117+newBytes (I# nbytes) = IO $ \s ->
118 case newByteArray# nbytes s of
118 case newByteArray# nbytes s of
119 (# s', mbarr# #) ->
119 (# s', mbarr# #) ->
120 let c = MutableByteArray mbarr#
120 let c = MutableByteArray mbarr#
@@ -140,10 +140,10 @@ pinnedNewAlignedBytes (I# nbytes) (I# align) = IO $ \s ->
140 let c = MutableByteArray mbarr#
140 let c = MutableByteArray mbarr#
141 in (# s', c #)
141 in (# s', c #)
142
142
143−{-# INLINE newBytes #-}
143+{-# INLINE newBytesAs #-}
144−newBytes :: PinnedState -> Int -> IO MutableByteArray
144+newBytesAs :: PinnedState -> Int -> IO MutableByteArray
145−newBytes Unpinned = newUnpinnedBytes
145+newBytesAs Unpinned = newBytes
146−newBytes Pinned = pinnedNewBytes
146+newBytesAs Pinned = pinnedNewBytes
147
147
148-------------------------------------------------------------------------------
148-------------------------------------------------------------------------------
149-- Pinning & Unpinning
149-- Pinning & Unpinning
@@ -178,7 +178,7 @@ pin arr@(MutableByteArray marr#) =
178 then return arr
178 then return arr
179 else
179 else
180#ifdef DEBUG
180#ifdef DEBUG
181− do
181+ do
182 -- XXX dump stack trace
182 -- XXX dump stack trace
183 trace ("pin: Copying array") (return ())
183 trace ("pin: Copying array") (return ())
184#endif
184#endif
@@ -194,7 +194,7 @@ unpin arr@(MutableByteArray marr#) =
194 then return arr
194 then return arr
195 else
195 else
196#ifdef DEBUG
196#ifdef DEBUG
197− do
197+ do
198 -- XXX dump stack trace
198 -- XXX dump stack trace
199 trace ("unpin: Copying array") (return ())
199 trace ("unpin: Copying array") (return ())
200#endif
200#endif
core/src/Streamly/Internal/FileSystem/Dir.hs+2−2
@@ -444,7 +444,7 @@ fromChunksWithBufferOf n h xs = fromChunks h $ AS.compact n xs
444-- @since 0.7.0
444-- @since 0.7.0
445{-# INLINE fromStreamWithBufferOf #-}
445{-# INLINE fromStreamWithBufferOf #-}
446fromStreamWithBufferOf :: MonadIO m => Int -> Handle -> Stream m Word8 -> m ()
446fromStreamWithBufferOf :: MonadIO m => Int -> Handle -> Stream m Word8 -> m ()
447−fromStreamWithBufferOf n h m = fromChunks h $ S.chunksOfWith Pinned n m
447+fromStreamWithBufferOf n h m = fromChunks h $ S.pinnedChunksOf n m
448-- fromStreamWithBufferOf n h m = fromChunks h $ AS.chunksOf n m
448-- fromStreamWithBufferOf n h m = fromChunks h $ AS.chunksOf n m
449
449
450-- > write = 'writeWithBufferOf' A.defaultChunkSize
450-- > write = 'writeWithBufferOf' A.defaultChunkSize
@@ -495,7 +495,7 @@ writeChunksWithBufferOf n h = lpackArraysChunksOf n (writeChunks h)
495-- @since 0.7.0
495-- @since 0.7.0
496{-# INLINE writeWithBufferOf #-}
496{-# INLINE writeWithBufferOf #-}
497writeWithBufferOf :: MonadIO m => Int -> Handle -> Fold m Word8 ()
497writeWithBufferOf :: MonadIO m => Int -> Handle -> Fold m Word8 ()
498−writeWithBufferOf n h = FL.groupsOf n (writeNUnsafeUsing Pinned n) (writeChunks h)
498+writeWithBufferOf n h = FL.groupsOf n (pinnedWriteNUnsafe n) (writeChunks h)
499
499
500-- > write = 'writeWithBufferOf' A.defaultChunkSize
500-- > write = 'writeWithBufferOf' A.defaultChunkSize
501--
501--
core/src/Streamly/Internal/FileSystem/File.hs+6−6
@@ -103,10 +103,10 @@ import qualified Control.Monad.Catch as MC
103import qualified System.IO as SIO
103import qualified System.IO as SIO
104
104
105import Streamly.Data.Fold (groupsOf, drain)
105import Streamly.Data.Fold (groupsOf, drain)
106−import Streamly.Internal.Data.Array.Type (Array(..), writeNUnsafeAs)
106+import Streamly.Internal.Data.Array.Type (Array(..), pinnedWriteNUnsafe)
107import Streamly.Internal.Data.Fold.Type (Fold(..))
107import Streamly.Internal.Data.Fold.Type (Fold(..))
108import Streamly.Data.Stream (Stream)
108import Streamly.Data.Stream (Stream)
109−import Streamly.Internal.Data.Unbox (PinnedState(..), Unbox)
109+import Streamly.Internal.Data.Unbox (Unbox)
110import Streamly.Internal.Data.Unfold.Type (Unfold(..))
110import Streamly.Internal.Data.Unfold.Type (Unfold(..))
111-- import Streamly.String (encodeUtf8, decodeUtf8, foldLines)
111-- import Streamly.String (encodeUtf8, decodeUtf8, foldLines)
112import Streamly.Internal.System.IO (defaultChunkSize)
112import Streamly.Internal.System.IO (defaultChunkSize)
@@ -114,7 +114,7 @@ import Streamly.Internal.System.IO (defaultChunkSize)
114import qualified Streamly.Data.Array as A
114import qualified Streamly.Data.Array as A
115import qualified Streamly.Data.Stream as S
115import qualified Streamly.Data.Stream as S
116import qualified Streamly.Data.Unfold as UF
116import qualified Streamly.Data.Unfold as UF
117−import qualified Streamly.Internal.Data.Array.Type as IA (chunksOfAs)
117+import qualified Streamly.Internal.Data.Array.Type as IA (pinnedChunksOf)
118import qualified Streamly.Internal.Data.Unfold as UF (bracketIO)
118import qualified Streamly.Internal.Data.Unfold as UF (bracketIO)
119import qualified Streamly.Internal.Data.Fold.Type as FL
119import qualified Streamly.Internal.Data.Fold.Type as FL
120 (Step(..), snoc, reduce)
120 (Step(..), snoc, reduce)
@@ -405,7 +405,7 @@ fromChunks = fromChunksMode WriteMode
405{-# INLINE fromBytesWith #-}
405{-# INLINE fromBytesWith #-}
406fromBytesWith :: (MonadIO m, MonadCatch m)
406fromBytesWith :: (MonadIO m, MonadCatch m)
407 => Int -> FilePath -> Stream m Word8 -> m ()
407 => Int -> FilePath -> Stream m Word8 -> m ()
408−fromBytesWith n file xs = fromChunks file $ IA.chunksOfAs Pinned n xs
408+fromBytesWith n file xs = fromChunks file $ IA.pinnedChunksOf n xs
409
409
410{-# DEPRECATED fromBytesWithBufferOf "Please use 'fromBytesWith' instead" #-}
410{-# DEPRECATED fromBytesWithBufferOf "Please use 'fromBytesWith' instead" #-}
411{-# INLINE fromBytesWithBufferOf #-}
411{-# INLINE fromBytesWithBufferOf #-}
@@ -464,7 +464,7 @@ writeChunks path = Fold step initial extract
464writeWith :: (MonadIO m, MonadCatch m)
464writeWith :: (MonadIO m, MonadCatch m)
465 => Int -> FilePath -> Fold m Word8 ()
465 => Int -> FilePath -> Fold m Word8 ()
466writeWith n path =
466writeWith n path =
467− groupsOf n (writeNUnsafeAs Pinned n) (writeChunks path)
467+ groupsOf n (pinnedWriteNUnsafe n) (writeChunks path)
468
468
469{-# DEPRECATED writeWithBufferOf "Please use 'writeWith' instead" #-}
469{-# DEPRECATED writeWithBufferOf "Please use 'writeWith' instead" #-}
470{-# INLINE writeWithBufferOf #-}
470{-# INLINE writeWithBufferOf #-}
@@ -503,7 +503,7 @@ writeAppendChunks = fromChunksMode AppendMode
503writeAppendWith :: (MonadIO m, MonadCatch m)
503writeAppendWith :: (MonadIO m, MonadCatch m)
504 => Int -> FilePath -> Stream m Word8 -> m ()
504 => Int -> FilePath -> Stream m Word8 -> m ()
505writeAppendWith n file xs =
505writeAppendWith n file xs =
506− writeAppendChunks file $ IA.chunksOfAs Pinned n xs
506+ writeAppendChunks file $ IA.pinnedChunksOf n xs
507
507
508-- | Append a byte stream to a file. Combines the bytes in chunks of size up to
508-- | Append a byte stream to a file. Combines the bytes in chunks of size up to
509-- 'A.defaultChunkSize' before writing. If the file exists then the new data
509-- 'A.defaultChunkSize' before writing. If the file exists then the new data
core/src/Streamly/Internal/FileSystem/Handle.hs+7−7
@@ -115,7 +115,7 @@ import Control.Monad.IO.Class (MonadIO(..))
115import Data.Function ((&))
115import Data.Function ((&))
116import Data.Maybe (isNothing, fromJust)
116import Data.Maybe (isNothing, fromJust)
117import Data.Word (Word8)
117import Data.Word (Word8)
118−import Streamly.Internal.Data.Unbox (PinnedState(..), Unbox)
118+import Streamly.Internal.Data.Unbox (Unbox)
119import System.IO (Handle, SeekMode(..), hGetBufSome, hPutBuf, hSeek)
119import System.IO (Handle, SeekMode(..), hGetBufSome, hPutBuf, hSeek)
120import Prelude hiding (read)
120import Prelude hiding (read)
121
121
@@ -123,7 +123,7 @@ import Streamly.Internal.Data.Fold (Fold)
123import Streamly.Internal.Data.Refold.Type (Refold(..))
123import Streamly.Internal.Data.Refold.Type (Refold(..))
124import Streamly.Internal.Data.Unfold.Type (Unfold(..))
124import Streamly.Internal.Data.Unfold.Type (Unfold(..))
125import Streamly.Internal.Data.Array.Type
125import Streamly.Internal.Data.Array.Type
126− (Array(..), writeNUnsafeAs, unsafeFreezeWithShrink, byteLength)
126+ (Array(..), pinnedWriteNUnsafe, unsafeFreezeWithShrink, byteLength)
127import Streamly.Internal.Data.Stream.StreamD.Type (Stream)
127import Streamly.Internal.Data.Stream.StreamD.Type (Stream)
128import Streamly.Internal.Data.Stream.Chunked (lpackArraysChunksOf)
128import Streamly.Internal.Data.Stream.Chunked (lpackArraysChunksOf)
129-- import Streamly.String (encodeUtf8, decodeUtf8, foldLines)
129-- import Streamly.String (encodeUtf8, decodeUtf8, foldLines)
@@ -425,7 +425,7 @@ putChunksWith :: (MonadIO m, Unbox a)
425 => Int -> Handle -> Stream m (Array a) -> m ()
425 => Int -> Handle -> Stream m (Array a) -> m ()
426putChunksWith n h xs = putChunks h $ AS.compact n xs
426putChunksWith n h xs = putChunks h $ AS.compact n xs
427
427
428−-- > putBytesWith n h m = Handle.putChunks h $ A.chunksOfWith Pinned n m
428+-- > putBytesWith n h m = Handle.putChunks h $ A.pinnedChunksOf n m
429
429
430-- | @putBytesWith bufsize handle stream@ writes @stream@ to @handle@
430-- | @putBytesWith bufsize handle stream@ writes @stream@ to @handle@
431-- in chunks of @bufsize@. A write is performed to the IO device as soon as we
431-- in chunks of @bufsize@. A write is performed to the IO device as soon as we
@@ -433,7 +433,7 @@ putChunksWith n h xs = putChunks h $ AS.compact n xs
433--
433--
434{-# INLINE putBytesWith #-}
434{-# INLINE putBytesWith #-}
435putBytesWith :: MonadIO m => Int -> Handle -> Stream m Word8 -> m ()
435putBytesWith :: MonadIO m => Int -> Handle -> Stream m Word8 -> m ()
436−putBytesWith n h m = putChunks h $ A.chunksOfAs Pinned n m
436+putBytesWith n h m = putChunks h $ A.pinnedChunksOf n m
437
437
438-- | Write a byte stream to a file handle. Accumulates the input in chunks of
438-- | Write a byte stream to a file handle. Accumulates the input in chunks of
439-- up to 'Streamly.Internal.Data.Array.Type.defaultChunkSize' before writing.
439-- up to 'Streamly.Internal.Data.Array.Type.defaultChunkSize' before writing.
@@ -502,7 +502,7 @@ writeChunksWithBufferOf = writeChunksWith
502--
502--
503{-# INLINE writeWith #-}
503{-# INLINE writeWith #-}
504writeWith :: MonadIO m => Int -> Handle -> Fold m Word8 ()
504writeWith :: MonadIO m => Int -> Handle -> Fold m Word8 ()
505−writeWith n h = FL.groupsOf n (writeNUnsafeAs Pinned n) (writeChunks h)
505+writeWith n h = FL.groupsOf n (pinnedWriteNUnsafe n) (writeChunks h)
506
506
507-- | Same as 'writeWith'
507-- | Same as 'writeWith'
508--
508--
@@ -520,7 +520,7 @@ writeWithBufferOf = writeWith
520writeMaybesWith :: (MonadIO m )
520writeMaybesWith :: (MonadIO m )
521 => Int -> Handle -> Fold m (Maybe Word8) ()
521 => Int -> Handle -> Fold m (Maybe Word8) ()
522writeMaybesWith n h =
522writeMaybesWith n h =
523− let writeNJusts = FL.lmap fromJust $ A.writeNAs Pinned n
523+ let writeNJusts = FL.lmap fromJust $ A.pinnedWriteN n
524 writeOnNothing = FL.takeEndBy_ isNothing writeNJusts
524 writeOnNothing = FL.takeEndBy_ isNothing writeNJusts
525 in FL.many writeOnNothing (writeChunks h)
525 in FL.many writeOnNothing (writeChunks h)
526
526
@@ -530,7 +530,7 @@ writeMaybesWith n h =
530{-# INLINE writerWith #-}
530{-# INLINE writerWith #-}
531writerWith :: MonadIO m => Int -> Refold m Handle Word8 ()
531writerWith :: MonadIO m => Int -> Refold m Handle Word8 ()
532writerWith n =
532writerWith n =
533− FL.refoldMany (FL.take n $ writeNUnsafeAs Pinned n) chunkWriter
533+ FL.refoldMany (FL.take n $ pinnedWriteNUnsafe n) chunkWriter
534
534
535-- | Write a byte stream to a file handle. Accumulates the input in chunks of
535-- | Write a byte stream to a file handle. Accumulates the input in chunks of
536-- up to 'Streamly.Internal.Data.Array.Type.defaultChunkSize' before writing
536-- up to 'Streamly.Internal.Data.Array.Type.defaultChunkSize' before writing
src/Streamly/Internal/Network/Inet/TCP.hs+4−4
@@ -106,11 +106,11 @@ import Prelude hiding (read)
106
106
107import Streamly.Internal.Control.Concurrent (MonadAsync)
107import Streamly.Internal.Control.Concurrent (MonadAsync)
108import Streamly.Internal.Control.ForkLifted (fork)
108import Streamly.Internal.Control.ForkLifted (fork)
109−import Streamly.Internal.Data.Array.Type (Array(..), writeNUnsafeAs)
109+import Streamly.Internal.Data.Array.Type (Array(..), pinnedWriteNUnsafe)
110import Streamly.Internal.Data.Fold.Type (Fold(..))
110import Streamly.Internal.Data.Fold.Type (Fold(..))
111import Streamly.Data.Stream (Stream)
111import Streamly.Data.Stream (Stream)
112import Streamly.Internal.Data.Tuple.Strict (Tuple'(..))
112import Streamly.Internal.Data.Tuple.Strict (Tuple'(..))
113−import Streamly.Internal.Data.Unbox (PinnedState(..), Unbox)
113+import Streamly.Internal.Data.Unbox (Unbox)
114import Streamly.Internal.Data.Unfold.Type (Unfold(..))
114import Streamly.Internal.Data.Unfold.Type (Unfold(..))
115import Streamly.Internal.Network.Socket (SockSpec(..), accept, acceptor)
115import Streamly.Internal.Network.Socket (SockSpec(..), accept, acceptor)
116import Streamly.Internal.System.IO (defaultChunkSize)
116import Streamly.Internal.System.IO (defaultChunkSize)
@@ -386,7 +386,7 @@ putBytesWithBufferOf
386 -> Stream m Word8
386 -> Stream m Word8
387 -> m ()
387 -> m ()
388putBytesWithBufferOf n addr port m =
388putBytesWithBufferOf n addr port m =
389− putChunks addr port $ A.chunksOfAs Pinned n m
389+ putChunks addr port $ A.pinnedChunksOf n m
390
390
391-- | Like 'write' but provides control over the write buffer. Output will
391-- | Like 'write' but provides control over the write buffer. Output will
392-- be written to the IO device as soon as we collect the specified number of
392-- be written to the IO device as soon as we collect the specified number of
@@ -400,7 +400,7 @@ writeWithBufferOf
400 -> PortNumber
400 -> PortNumber
401 -> Fold m Word8 ()
401 -> Fold m Word8 ()
402writeWithBufferOf n addr port =
402writeWithBufferOf n addr port =
403− FL.groupsOf n (writeNUnsafeAs Pinned n) (writeChunks addr port)
403+ FL.groupsOf n (pinnedWriteNUnsafe n) (writeChunks addr port)
404
404
405-- | Write a stream to the supplied IPv4 host address and port number.
405-- | Write a stream to the supplied IPv4 host address and port number.
406--
406--
src/Streamly/Internal/Network/Socket.hs+6−6
@@ -72,7 +72,7 @@ import Control.Monad.IO.Class (MonadIO(..))
72import Data.Maybe (isNothing, fromJust)
72import Data.Maybe (isNothing, fromJust)
73import Data.Word (Word8)
73import Data.Word (Word8)
74import Foreign.Ptr (plusPtr, Ptr, castPtr)
74import Foreign.Ptr (plusPtr, Ptr, castPtr)
75−import Streamly.Internal.Data.Unbox (PinnedState(..), Unbox)
75+import Streamly.Internal.Data.Unbox (Unbox)
76import Network.Socket
76import Network.Socket
77 (Socket, SocketOption(..), Family(..), SockAddr(..),
77 (Socket, SocketOption(..), Family(..), SockAddr(..),
78 ProtocolNumber, withSocketsDo, SocketType(..), socket, bind,
78 ProtocolNumber, withSocketsDo, SocketType(..), socket, bind,
@@ -97,8 +97,8 @@ import Streamly.Internal.System.IO (defaultChunkSize)
97import qualified Streamly.Data.Array as A (reader, length)
97import qualified Streamly.Data.Array as A (reader, length)
98import qualified Streamly.Data.Fold as FL
98import qualified Streamly.Data.Fold as FL
99import qualified Streamly.Internal.Data.Array.Type as A
99import qualified Streamly.Internal.Data.Array.Type as A
100− ( unsafeFreeze, asPtrUnsafe, byteLength, writeNUnsafeAs, chunksOfAs
100+ ( unsafeFreeze, asPtrUnsafe, byteLength, pinnedWriteNUnsafe, pinnedChunksOf
101− , writeNAs
101+ , pinnedWriteN
102 )
102 )
103import qualified Streamly.Internal.Data.Array.Mut as MArray
103import qualified Streamly.Internal.Data.Array.Mut as MArray
104 (MutArray(..), pinnedNewBytes, asPtrUnsafe)
104 (MutArray(..), pinnedNewBytes, asPtrUnsafe)
@@ -497,14 +497,14 @@ writeChunksWithBufferOf = writeChunksWith
497--
497--
498{-# INLINE putBytesWith #-}
498{-# INLINE putBytesWith #-}
499putBytesWith :: MonadIO m => Int -> Socket -> Stream m Word8 -> m ()
499putBytesWith :: MonadIO m => Int -> Socket -> Stream m Word8 -> m ()
500−putBytesWith n h m = putChunks h $ A.chunksOfAs Pinned n m
500+putBytesWith n h m = putChunks h $ A.pinnedChunksOf n m
501
501
502-- | Write a byte stream to a socket. Accumulates the input in chunks of
502-- | Write a byte stream to a socket. Accumulates the input in chunks of
503-- specified number of bytes before writing.
503-- specified number of bytes before writing.
504--
504--
505{-# INLINE writeWith #-}
505{-# INLINE writeWith #-}
506writeWith :: MonadIO m => Int -> Socket -> Fold m Word8 ()
506writeWith :: MonadIO m => Int -> Socket -> Fold m Word8 ()
507−writeWith n h = FL.groupsOf n (A.writeNUnsafeAs Pinned n) (writeChunks h)
507+writeWith n h = FL.groupsOf n (A.pinnedWriteNUnsafe n) (writeChunks h)
508
508
509-- | Same as 'writeWith'
509-- | Same as 'writeWith'
510--
510--
@@ -522,7 +522,7 @@ writeWithBufferOf = writeWith
522writeMaybesWith :: (MonadIO m )
522writeMaybesWith :: (MonadIO m )
523 => Int -> Socket -> Fold m (Maybe Word8) ()
523 => Int -> Socket -> Fold m (Maybe Word8) ()
524writeMaybesWith n h =
524writeMaybesWith n h =
525− let writeNJusts = FL.lmap fromJust $ A.writeNAs Pinned n
525+ let writeNJusts = FL.lmap fromJust $ A.pinnedWriteN n
526 writeOnNothing = FL.takeEndBy_ isNothing writeNJusts
526 writeOnNothing = FL.takeEndBy_ isNothing writeNJusts
527 in FL.many writeOnNothing (writeChunks h)
527 in FL.many writeOnNothing (writeChunks h)
528
528
test/Streamly/Test/Data/Unbox.hs+4−4
@@ -34,7 +34,7 @@ import Streamly.Internal.Data.Unbox
34 , genericPeekByteIndex
34 , genericPeekByteIndex
35 , genericPokeByteIndex
35 , genericPokeByteIndex
36 , genericSizeOf
36 , genericSizeOf
37− , newUnpinnedBytes
37+ , newBytes
38 , pokeByteIndex
38 , pokeByteIndex
39 )
39 )
40
40
@@ -114,7 +114,7 @@ testSerialization ::
114 => a
114 => a
115 -> IO ()
115 -> IO ()
116testSerialization val = do
116testSerialization val = do
117− arr <- newUnpinnedBytes (sizeOf (Proxy :: Proxy a))
117+ arr <- newBytes (sizeOf (Proxy :: Proxy a))
118 pokeByteIndex 0 arr val
118 pokeByteIndex 0 arr val
119 peekByteIndex 0 arr `shouldReturn` val
119 peekByteIndex 0 arr `shouldReturn` val
120
120
@@ -136,7 +136,7 @@ testGenericConsistency val = do
136 sizeOf (Proxy :: Proxy a) `shouldBe` genericSizeOf (Proxy :: Proxy a)
136 sizeOf (Proxy :: Proxy a) `shouldBe` genericSizeOf (Proxy :: Proxy a)
137
137
138 -- Test the serialization and deserialization
138 -- Test the serialization and deserialization
139− arr <- newUnpinnedBytes (sizeOf (Proxy :: Proxy a))
139+ arr <- newBytes (sizeOf (Proxy :: Proxy a))
140
140
141 pokeByteIndex 0 arr val
141 pokeByteIndex 0 arr val
142 genericPeekByteIndex arr 0 `shouldReturn` val
142 genericPeekByteIndex arr 0 `shouldReturn` val
@@ -162,7 +162,7 @@ testSerializeList sizeOfA val = do
162
162
163 sz `shouldBe` sizeOfA
163 sz `shouldBe` sizeOfA
164
164
165− arr <- newUnpinnedBytes sz
165+ arr <- newBytes sz
166
166
167 off1 <- Serialize.serialize 0 arr val
167 off1 <- Serialize.serialize 0 arr val
168 (off2, val2) <- Serialize.deserialize 0 arr
168 (off2, val2) <- Serialize.deserialize 0 arr