on master help sign in
Use the pinned prefix over pinned suffix in array combinators
3a3436cAdithya Kumar
core/src/Streamly/Data/MutArray.hs+10−1
@@ -33,7 +33,7 @@ module Streamly.Data.MutArray
33
33
34 -- Uninitialized Arrays
34 -- Uninitialized Arrays
35 , new
35 , new
36− , newPinned
36+ , pinnedNew
37
37
38 -- From containers
38 -- From containers
39 , fromListN
39 , fromListN
@@ -84,11 +84,20 @@ module Streamly.Data.MutArray
84 -- * Pinning & Unpinning
84 -- * Pinning & Unpinning
85 , pin
85 , pin
86 , unpin
86 , unpin
87+
88+ -- * Deprecated
89+ , newPinned
87 )
90 )
88where
91where
89
92
90import Prelude hiding (length, read)
93import Prelude hiding (length, read)
91import Streamly.Internal.Data.Array.Mut
94import Streamly.Internal.Data.Array.Mut
92import Streamly.Internal.Data.Unbox (Unbox (..))
95import Streamly.Internal.Data.Unbox (Unbox (..))
96+import Control.Monad.IO.Class (MonadIO)
93
97
94#include "DocTestDataMutArray.hs"
98#include "DocTestDataMutArray.hs"
99+
100+{-# DEPRECATED newPinned "Please use pinnedNew instead." #-}
101+{-# INLINE newPinned #-}
102+newPinned :: forall m a. (MonadIO m, Unbox a) => Int -> m (MutArray a)
103+newPinned = pinnedNew
core/src/Streamly/Internal/Data/Array/Generic/Mut/Type.hs+2−2
@@ -597,7 +597,7 @@ writeNUnsafe n = Fold step initial return
597writeN :: MonadIO m => Int -> Fold m a (MutArray a)
597writeN :: MonadIO m => Int -> Fold m a (MutArray a)
598writeN n = FL.take n $ writeNUnsafe n
598writeN n = FL.take n $ writeNUnsafe n
599
599
600−-- >>> f n = MutArray.writeAppendWith (* 2) (MutArray.newPinned n)
600+-- >>> f n = MutArray.writeAppendWith (* 2) (MutArray.pinnedNew n)
601-- >>> writeWith n = Fold.rmapM MutArray.rightSize (f n)
601-- >>> writeWith n = Fold.rmapM MutArray.rightSize (f n)
602-- >>> writeWith n = Fold.rmapM MutArray.fromArrayStreamK (MutArray.writeChunks n)
602-- >>> writeWith n = Fold.rmapM MutArray.fromArrayStreamK (MutArray.writeChunks n)
603
603
@@ -610,7 +610,7 @@ writeN n = FL.take n $ writeNUnsafe n
610-- /Pre-release/
610-- /Pre-release/
611{-# INLINE_NORMAL writeWith #-}
611{-# INLINE_NORMAL writeWith #-}
612writeWith :: MonadIO m => Int -> Fold m a (MutArray a)
612writeWith :: MonadIO m => Int -> Fold m a (MutArray a)
613−-- writeWith n = FL.rmapM rightSize $ writeAppendWith (* 2) (newPinned n)
613+-- writeWith n = FL.rmapM rightSize $ writeAppendWith (* 2) (pinnedNew n)
614writeWith elemCount = FL.rmapM extract $ FL.foldlM' step initial
614writeWith elemCount = FL.rmapM extract $ FL.foldlM' step initial
615
615
616 where
616 where
core/src/Streamly/Internal/Data/Array/Mut/Type.hs+18−36
@@ -42,15 +42,12 @@ module Streamly.Internal.Data.Array.Mut.Type
42 , nil
42 , nil
43
43
44 -- *** Uninitialized Arrays
44 -- *** Uninitialized Arrays
45− , newPinned
45+ , pinnedNew
46− , newPinnedBytes
46+ , pinnedNewBytes
47− , newAlignedPinned
47+ , pinnedNewAligned
48 , new
48 , new
49 , newArrayWith
49 , newArrayWith
50
50
51− -- *** Initialized Arrays
52− , withNewArrayUnsafePinned
53−
54 -- *** From streams
51 -- *** From streams
55 , ArrayUnsafe (..)
52 , ArrayUnsafe (..)
56 , writeNWithUnsafe
53 , writeNWithUnsafe
@@ -388,7 +385,7 @@ isPinned MutArray{..} = Unboxed.isPinned arrContents
388-- files and arrays.
385-- files and arrays.
389
386
390-- GHC always guarantees word-aligned memory, alignment is important only when
387-- GHC always guarantees word-aligned memory, alignment is important only when
391−-- we need more than that. See stg_newAlignedPinnedByteArrayzh and
388+-- we need more than that. See stg_pinnedNewAlignedByteArrayzh and
392-- allocatePinned in GHC source.
389-- allocatePinned in GHC source.
393
390
394-- | @newArrayWith allocator alignment count@ allocates a new array of zero
391-- | @newArrayWith allocator alignment count@ allocates a new array of zero
@@ -441,22 +438,22 @@ newBytesAs ps bytes = do
441-- 'Unboxed' instance of the type.
438-- 'Unboxed' instance of the type.
442--
439--
443-- /Pre-release/
440-- /Pre-release/
444−{-# INLINE newPinnedBytes #-}
441+{-# INLINE pinnedNewBytes #-}
445−newPinnedBytes :: MonadIO m =>
442+pinnedNewBytes :: MonadIO m =>
446#ifdef DEVBUILD
443#ifdef DEVBUILD
447 Unbox a =>
444 Unbox a =>
448#endif
445#endif
449 Int -> m (MutArray a)
446 Int -> m (MutArray a)
450−newPinnedBytes = newBytesAs Pinned
447+pinnedNewBytes = newBytesAs Pinned
451
448
452-- | Like 'newArrayWith' but using an allocator is a pinned memory allocator and
449-- | Like 'newArrayWith' but using an allocator is a pinned memory allocator and
453-- the alignment is dictated by the 'Unboxed' instance of the type.
450-- the alignment is dictated by the 'Unboxed' instance of the type.
454--
451--
455-- /Internal/
452-- /Internal/
456−{-# INLINE newAlignedPinned #-}
453+{-# INLINE pinnedNewAligned #-}
457−newAlignedPinned :: (MonadIO m, Unbox a) => Int -> Int -> m (MutArray a)
454+pinnedNewAligned :: (MonadIO m, Unbox a) => Int -> Int -> m (MutArray a)
458−newAlignedPinned =
455+pinnedNewAligned =
459− newArrayWith (\s a -> liftIO $ Unboxed.newAlignedPinnedBytes s a)
456+ newArrayWith (\s a -> liftIO $ Unboxed.pinnedNewAlignedBytes s a)
460
457
461{-# INLINE newAs #-}
458{-# INLINE newAs #-}
462newAs :: (MonadIO m, Unbox a) => PinnedState -> Int -> m (MutArray a)
459newAs :: (MonadIO m, Unbox a) => PinnedState -> Int -> m (MutArray a)
@@ -471,9 +468,9 @@ newAs ps =
471-- the array is uninitialized and the allocation is aligned as per the 'Unboxed'
468-- the array is uninitialized and the allocation is aligned as per the 'Unboxed'
472-- instance of the type.
469-- instance of the type.
473--
470--
474−{-# INLINE newPinned #-}
471+{-# INLINE pinnedNew #-}
475−newPinned :: forall m a. (MonadIO m, Unbox a) => Int -> m (MutArray a)
472+pinnedNew :: forall m a. (MonadIO m, Unbox a) => Int -> m (MutArray a)
476−newPinned = newAs Pinned
473+pinnedNew = newAs Pinned
477
474
478-- | Allocates an empty unpinned array that can hold 'count' items. The memory
475-- | Allocates an empty unpinned array that can hold 'count' items. The memory
479-- of the array is uninitialized.
476-- of the array is uninitialized.
@@ -482,21 +479,6 @@ newPinned = newAs Pinned
482new :: (MonadIO m, Unbox a) => Int -> m (MutArray a)
479new :: (MonadIO m, Unbox a) => Int -> m (MutArray a)
483new = newAs Unpinned
480new = newAs Unpinned
484
481
485−-- XXX This should create a full length uninitialzed array so that the pointer
486−-- can be used.
487−
488−-- | Allocate a pinned MutArray of the given size and run an IO action passing
489−-- the array start pointer.
490−--
491−-- /Internal/
492−{-# INLINE withNewArrayUnsafePinned #-}
493−withNewArrayUnsafePinned ::
494− (MonadIO m, Unbox a) => Int -> (Ptr a -> m ()) -> m (MutArray a)
495−withNewArrayUnsafePinned count f = do
496− arr <- newPinned count
497− asPtrUnsafe arr
498− $ \p -> f p >> return arr
499−
500-------------------------------------------------------------------------------
482-------------------------------------------------------------------------------
501-- Random writes
483-- Random writes
502-------------------------------------------------------------------------------
484-------------------------------------------------------------------------------
@@ -755,7 +737,7 @@ reallocExplicit elemSize newCapacityInBytes MutArray{..} = do
755 let newCapMaxInBytes = roundUpLargeArray newCapacityInBytes
737 let newCapMaxInBytes = roundUpLargeArray newCapacityInBytes
756 contents <-
738 contents <-
757 if Unboxed.isPinned arrContents
739 if Unboxed.isPinned arrContents
758− then Unboxed.newPinnedBytes newCapMaxInBytes
740+ then Unboxed.pinnedNewBytes newCapMaxInBytes
759 else Unboxed.newUnpinnedBytes newCapMaxInBytes
741 else Unboxed.newUnpinnedBytes newCapMaxInBytes
760 let !(MutableByteArray mbarrFrom#) = arrContents
742 let !(MutableByteArray mbarrFrom#) = arrContents
761 !(MutableByteArray mbarrTo#) = contents
743 !(MutableByteArray mbarrTo#) = contents
@@ -1864,15 +1846,15 @@ writeRevN = writeRevNWith new
1864-- | @pinnedWriteNAligned align n@ folds a maximum of @n@ elements from the
1846-- | @pinnedWriteNAligned align n@ folds a maximum of @n@ elements from the
1865-- input stream to a 'MutArray' aligned to the given size.
1847-- input stream to a 'MutArray' aligned to the given size.
1866--
1848--
1867−-- >>> pinnedWriteNAligned align = MutArray.writeNWith (MutArray.newAlignedPinned align)
1849+-- >>> pinnedWriteNAligned align = MutArray.writeNWith (MutArray.pinnedNewAligned align)
1868−-- >>> pinnedWriteNAligned align n = MutArray.writeAppendN n (MutArray.newAlignedPinned align n)
1850+-- >>> pinnedWriteNAligned align n = MutArray.writeAppendN n (MutArray.pinnedNewAligned align n)
1869--
1851--
1870-- /Pre-release/
1852-- /Pre-release/
1871--
1853--
1872{-# INLINE_NORMAL pinnedWriteNAligned #-}
1854{-# INLINE_NORMAL pinnedWriteNAligned #-}
1873pinnedWriteNAligned :: forall m a. (MonadIO m, Unbox a)
1855pinnedWriteNAligned :: forall m a. (MonadIO m, Unbox a)
1874 => Int -> Int -> Fold m a (MutArray a)
1856 => Int -> Int -> Fold m a (MutArray a)
1875−pinnedWriteNAligned align = writeNWith (newAlignedPinned align)
1857+pinnedWriteNAligned align = writeNWith (pinnedNewAligned align)
1876
1858
1877-- XXX Buffer to a list instead?
1859-- XXX Buffer to a list instead?
1878--
1860--
core/src/Streamly/Internal/Data/Fold.hs+1−1
@@ -2440,7 +2440,7 @@ bottomBy cmp n = Fold step initial extract
2440 where
2440 where
2441
2441
2442 initial = do
2442 initial = do
2443− arr <- MA.newPinned n
2443+ arr <- MA.pinnedNew n
2444 if n <= 0
2444 if n <= 0
2445 then return $ Done arr
2445 then return $ Done arr
2446 else return $ Partial (arr, 0)
2446 else return $ Partial (arr, 0)
core/src/Streamly/Internal/Data/Stream/Chunked.hs+3−3
@@ -476,7 +476,7 @@ splitAtArrayListRev n ls
476spliceArraysLenUnsafe :: (MonadIO m, Unbox a)
476spliceArraysLenUnsafe :: (MonadIO m, Unbox a)
477 => Int -> Stream m (MutArray a) -> m (MutArray a)
477 => Int -> Stream m (MutArray a) -> m (MutArray a)
478spliceArraysLenUnsafe len buffered = do
478spliceArraysLenUnsafe len buffered = do
479− arr <- liftIO $ MA.newPinned len
479+ arr <- liftIO $ MA.pinnedNew len
480 D.foldlM' MA.spliceUnsafe (return arr) buffered
480 D.foldlM' MA.spliceUnsafe (return arr) buffered
481
481
482{-# INLINE _spliceArrays #-}
482{-# INLINE _spliceArrays #-}
@@ -485,7 +485,7 @@ _spliceArrays :: (MonadIO m, Unbox a)
485_spliceArrays s = do
485_spliceArrays s = do
486 buffered <- D.foldr K.cons K.nil s
486 buffered <- D.foldr K.cons K.nil s
487 len <- K.fold FL.sum (fmap Array.length buffered)
487 len <- K.fold FL.sum (fmap Array.length buffered)
488− arr <- liftIO $ MA.newPinned len
488+ arr <- liftIO $ MA.pinnedNew len
489 final <- D.foldlM' writeArr (return arr) (toStream buffered)
489 final <- D.foldlM' writeArr (return arr) (toStream buffered)
490 return $ A.unsafeFreeze final
490 return $ A.unsafeFreeze final
491
491
@@ -507,7 +507,7 @@ spliceArraysRealloced :: forall m a. (MonadIO m, Unbox a)
507 => Stream m (Array a) -> m (Array a)
507 => Stream m (Array a) -> m (Array a)
508spliceArraysRealloced s = do
508spliceArraysRealloced s = do
509 let n = allocBytesToElemCount (undefined :: a) (4 * 1024)
509 let n = allocBytesToElemCount (undefined :: a) (4 * 1024)
510− idst = liftIO $ MA.newPinned n
510+ idst = liftIO $ MA.pinnedNew n
511
511
512 arr <- D.foldlM' MA.spliceExp idst (fmap A.unsafeThaw s)
512 arr <- D.foldlM' MA.spliceExp idst (fmap A.unsafeThaw s)
513 liftIO $ A.unsafeFreeze <$> MA.rightSize arr
513 liftIO $ A.unsafeFreeze <$> MA.rightSize arr
core/src/Streamly/Internal/Data/Unbox.hs+13−13
@@ -16,8 +16,8 @@ module Streamly.Internal.Data.Unbox
16 , unpin
16 , unpin
17 , newBytes
17 , newBytes
18 , newUnpinnedBytes
18 , newUnpinnedBytes
19− , newPinnedBytes
19+ , pinnedNewBytes
20− , newAlignedPinnedBytes
20+ , pinnedNewAlignedBytes
21 , nil
21 , nil
22
22
23 -- * Type Parser and Builder
23 -- * Type Parser and Builder
@@ -117,21 +117,21 @@ newUnpinnedBytes (I# nbytes) = IO $ \s ->
117 let c = MutableByteArray mbarr#
117 let c = MutableByteArray mbarr#
118 in (# s', c #)
118 in (# s', c #)
119
119
120−{-# INLINE newPinnedBytes #-}
120+{-# INLINE pinnedNewBytes #-}
121−newPinnedBytes :: Int -> IO MutableByteArray
121+pinnedNewBytes :: Int -> IO MutableByteArray
122−newPinnedBytes nbytes | nbytes < 0 =
122+pinnedNewBytes nbytes | nbytes < 0 =
123− errorWithoutStackTrace "newPinnedBytes: size must be >= 0"
123+ errorWithoutStackTrace "pinnedNewBytes: size must be >= 0"
124−newPinnedBytes (I# nbytes) = IO $ \s ->
124+pinnedNewBytes (I# nbytes) = IO $ \s ->
125 case newPinnedByteArray# nbytes s of
125 case newPinnedByteArray# nbytes s of
126 (# s', mbarr# #) ->
126 (# s', mbarr# #) ->
127 let c = MutableByteArray mbarr#
127 let c = MutableByteArray mbarr#
128 in (# s', c #)
128 in (# s', c #)
129
129
130−{-# INLINE newAlignedPinnedBytes #-}
130+{-# INLINE pinnedNewAlignedBytes #-}
131−newAlignedPinnedBytes :: Int -> Int -> IO MutableByteArray
131+pinnedNewAlignedBytes :: Int -> Int -> IO MutableByteArray
132−newAlignedPinnedBytes nbytes _align | nbytes < 0 =
132+pinnedNewAlignedBytes nbytes _align | nbytes < 0 =
133− errorWithoutStackTrace "newAlignedPinnedBytes: size must be >= 0"
133+ errorWithoutStackTrace "pinnedNewAlignedBytes: size must be >= 0"
134−newAlignedPinnedBytes (I# nbytes) (I# align) = IO $ \s ->
134+pinnedNewAlignedBytes (I# nbytes) (I# align) = IO $ \s ->
135 case newAlignedPinnedByteArray# nbytes align s of
135 case newAlignedPinnedByteArray# nbytes align s of
136 (# s', mbarr# #) ->
136 (# s', mbarr# #) ->
137 let c = MutableByteArray mbarr#
137 let c = MutableByteArray mbarr#
@@ -140,7 +140,7 @@ newAlignedPinnedBytes (I# nbytes) (I# align) = IO $ \s ->
140{-# INLINE newBytes #-}
140{-# INLINE newBytes #-}
141newBytes :: PinnedState -> Int -> IO MutableByteArray
141newBytes :: PinnedState -> Int -> IO MutableByteArray
142newBytes Unpinned = newUnpinnedBytes
142newBytes Unpinned = newUnpinnedBytes
143−newBytes Pinned = newPinnedBytes
143+newBytes Pinned = pinnedNewBytes
144
144
145-------------------------------------------------------------------------------
145-------------------------------------------------------------------------------
146-- Pinning & Unpinning
146-- Pinning & Unpinning
core/src/Streamly/Internal/FileSystem/Handle.hs+1−1
@@ -180,7 +180,7 @@ import qualified Streamly.Internal.Data.Stream.StreamK.Type as K (mkStream)
180{-# INLINABLE getChunk #-}
180{-# INLINABLE getChunk #-}
181getChunk :: MonadIO m => Int -> Handle -> m (Array Word8)
181getChunk :: MonadIO m => Int -> Handle -> m (Array Word8)
182getChunk size h = liftIO $ do
182getChunk size h = liftIO $ do
183− arr <- MArray.newPinnedBytes size
183+ arr <- MArray.pinnedNewBytes size
184 -- ptr <- mallocPlainForeignPtrAlignedBytes size (alignment (undefined :: Word8))
184 -- ptr <- mallocPlainForeignPtrAlignedBytes size (alignment (undefined :: Word8))
185 MArray.asPtrUnsafe arr $ \p -> do
185 MArray.asPtrUnsafe arr $ \p -> do
186 n <- hGetBufSome h p size
186 n <- hGetBufSome h p size
src/Streamly/Internal/FileSystem/FD.hs+2−2
@@ -146,7 +146,7 @@ import qualified Streamly.Internal.System.IOVec.Type as RawIO
146import qualified Streamly.Data.Array as A
146import qualified Streamly.Data.Array as A
147import qualified Streamly.Data.Fold as FL
147import qualified Streamly.Data.Fold as FL
148import qualified Streamly.Internal.Data.Array.Mut as MArray
148import qualified Streamly.Internal.Data.Array.Mut as MArray
149− (MutArray(..), newPinnedBytes, asPtrUnsafe)
149+ (MutArray(..), pinnedNewBytes, asPtrUnsafe)
150import qualified Streamly.Internal.Data.Stream.Chunked as AS
150import qualified Streamly.Internal.Data.Stream.Chunked as AS
151import qualified Streamly.Internal.Data.Stream as S
151import qualified Streamly.Internal.Data.Stream as S
152import qualified Streamly.Internal.Data.Stream.StreamD.Type as D
152import qualified Streamly.Internal.Data.Stream.StreamD.Type as D
@@ -217,7 +217,7 @@ openFile path mode = Handle . fst <$> FD.openFile path mode True
217{-# INLINABLE readArrayUpto #-}
217{-# INLINABLE readArrayUpto #-}
218readArrayUpto :: Int -> Handle -> IO (Array Word8)
218readArrayUpto :: Int -> Handle -> IO (Array Word8)
219readArrayUpto size (Handle fd) = do
219readArrayUpto size (Handle fd) = do
220− arr <- MArray.newPinnedBytes size
220+ arr <- MArray.pinnedNewBytes size
221 -- ptr <- mallocPlainForeignPtrAlignedBytes size (alignment (undefined :: Word8))
221 -- ptr <- mallocPlainForeignPtrAlignedBytes size (alignment (undefined :: Word8))
222 MArray.asPtrUnsafe arr $ \p -> do
222 MArray.asPtrUnsafe arr $ \p -> do
223 -- n <- hGetBufSome h p size
223 -- n <- hGetBufSome h p size
src/Streamly/Internal/Network/Socket.hs+2−2
@@ -101,7 +101,7 @@ import qualified Streamly.Internal.Data.Array.Type as A
101 , writeNAs
101 , writeNAs
102 )
102 )
103import qualified Streamly.Internal.Data.Array.Mut as MArray
103import qualified Streamly.Internal.Data.Array.Mut as MArray
104− (MutArray(..), newPinnedBytes, asPtrUnsafe)
104+ (MutArray(..), pinnedNewBytes, asPtrUnsafe)
105import qualified Streamly.Internal.Data.Stream as S
105import qualified Streamly.Internal.Data.Stream as S
106import qualified Streamly.Internal.Data.Stream.StreamD.Type as D
106import qualified Streamly.Internal.Data.Stream.StreamD.Type as D
107 (Stream(..), Step(..))
107 (Stream(..), Step(..))
@@ -260,7 +260,7 @@ readArrayUptoWith
260 -> h
260 -> h
261 -> IO (Array Word8)
261 -> IO (Array Word8)
262readArrayUptoWith f size h = do
262readArrayUptoWith f size h = do
263− arr <- MArray.newPinnedBytes size
263+ arr <- MArray.pinnedNewBytes size
264 -- ptr <- mallocPlainForeignPtrAlignedBytes size (alignment (undefined :: Word8))
264 -- ptr <- mallocPlainForeignPtrAlignedBytes size (alignment (undefined :: Word8))
265 MArray.asPtrUnsafe arr $ \p -> do
265 MArray.asPtrUnsafe arr $ \p -> do
266 n <- f h p size
266 n <- f h p size
test/Streamly/Test/Data/Array.hs+2−2
@@ -150,7 +150,7 @@ testBubbleWith asc =
150 else MA.bubble (flip compare) arr
150 else MA.bubble (flip compare) arr
151 return arr
151 return arr
152 )
152 )
153− (MA.newPinned $ length ls)
153+ (MA.pinnedNew $ length ls)
154
154
155testBubbleAsc :: Property
155testBubbleAsc :: Property
156testBubbleAsc = testBubbleWith True
156testBubbleAsc = testBubbleWith True
@@ -160,7 +160,7 @@ testBubbleDesc = testBubbleWith False
160
160
161testByteLengthWithMA :: forall a. Unbox a => a -> IO ()
161testByteLengthWithMA :: forall a. Unbox a => a -> IO ()
162testByteLengthWithMA _ = do
162testByteLengthWithMA _ = do
163− arrA <- MA.newPinned 100 :: IO (MutArray a)
163+ arrA <- MA.pinnedNew 100 :: IO (MutArray a)
164 let arrW8 = MA.castUnsafe arrA :: MutArray Word8
164 let arrW8 = MA.castUnsafe arrA :: MutArray Word8
165 MA.byteLength arrA `shouldBe` MA.length arrW8
165 MA.byteLength arrA `shouldBe` MA.length arrW8
166
166
test/Streamly/Test/Data/Array/Mut.hs+1−1
@@ -42,7 +42,7 @@ testAppend =
42
42
43 action ls = do
43 action ls = do
44 x <- Stream.fold
44 x <- Stream.fold
45− (MArray.writeAppend (MArray.newPinned 0))
45+ (MArray.writeAppend (MArray.pinnedNew 0))
46 (Stream.fromList (ls::[Int]))
46 (Stream.fromList (ls::[Int]))
47 lst <- MArray.toList x
47 lst <- MArray.toList x
48 assert (ls == lst)
48 assert (ls == lst)