on master help sign in
Use the pinned prefix over pinned suffix in array combinators

- Remove withNewArrayUnsafePinned (asPtrUnsafe can be used) - Deprecate newPinned

commit3a3436c3934e57da48a6892d06647bac942a45c0 authorAdithya Kumar <adithya@composewell.com> dateWed 26 Jul 2023, 17:31 (3 years ago) parentd29030f
11 files changed+55−64
core/src/Streamly/Data/MutArray.hs+10−1 core/src/Streamly/Internal/Data/Array/Generic/Mut/Type.hs+2−2 core/src/Streamly/Internal/Data/Array/Mut/Type.hs+18−36 core/src/Streamly/Internal/Data/Fold.hs+1−1 core/src/Streamly/Internal/Data/Stream/Chunked.hs+3−3 core/src/Streamly/Internal/Data/Unbox.hs+13−13 core/src/Streamly/Internal/FileSystem/Handle.hs+1−1 src/Streamly/Internal/FileSystem/FD.hs+2−2 src/Streamly/Internal/Network/Socket.hs+2−2 test/Streamly/Test/Data/Array.hs+2−2 test/Streamly/Test/Data/Array/Mut.hs+1−1
core/src/Streamly/Data/MutArray.hs+10−1
@@ -33,7 +33,7 @@ module Streamly.Data.MutArray
3333
3434 -- Uninitialized Arrays
3535 , new
36− , newPinned
36+ , pinnedNew
3737
3838 -- From containers
3939 , fromListN
@@ -84,11 +84,20 @@ module Streamly.Data.MutArray
8484 -- * Pinning & Unpinning
8585 , pin
8686 , unpin
87+
88+ -- * Deprecated
89+ , newPinned
8790 )
8891where
8992
9093import Prelude hiding (length, read)
9194import Streamly.Internal.Data.Array.Mut
9295import Streamly.Internal.Data.Unbox (Unbox (..))
96+import Control.Monad.IO.Class (MonadIO)
9397
9498#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
597597writeN :: MonadIO m => Int -> Fold m a (MutArray a)
598598writeN n = FL.take n $ writeNUnsafe n
599599
600−-- >>> f n = MutArray.writeAppendWith (* 2) (MutArray.newPinned n)
600+-- >>> f n = MutArray.writeAppendWith (* 2) (MutArray.pinnedNew n)
601601-- >>> writeWith n = Fold.rmapM MutArray.rightSize (f n)
602602-- >>> writeWith n = Fold.rmapM MutArray.fromArrayStreamK (MutArray.writeChunks n)
603603
@@ -610,7 +610,7 @@ writeN n = FL.take n $ writeNUnsafe n
610610-- /Pre-release/
611611{-# INLINE_NORMAL writeWith #-}
612612writeWith :: 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)
614614writeWith elemCount = FL.rmapM extract $ FL.foldlM' step initial
615615
616616 where
core/src/Streamly/Internal/Data/Array/Mut/Type.hs+18−36
@@ -42,15 +42,12 @@ module Streamly.Internal.Data.Array.Mut.Type
4242 , nil
4343
4444 -- *** Uninitialized Arrays
45− , newPinned
46− , newPinnedBytes
47− , newAlignedPinned
45+ , pinnedNew
46+ , pinnedNewBytes
47+ , pinnedNewAligned
4848 , new
4949 , newArrayWith
5050
51− -- *** Initialized Arrays
52− , withNewArrayUnsafePinned
53−
5451 -- *** From streams
5552 , ArrayUnsafe (..)
5653 , writeNWithUnsafe
@@ -388,7 +385,7 @@ isPinned MutArray{..} = Unboxed.isPinned arrContents
388385-- files and arrays.
389386
390387-- 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
392389-- allocatePinned in GHC source.
393390
394391-- | @newArrayWith allocator alignment count@ allocates a new array of zero
@@ -441,22 +438,22 @@ newBytesAs ps bytes = do
441438-- 'Unboxed' instance of the type.
442439--
443440-- /Pre-release/
444−{-# INLINE newPinnedBytes #-}
445−newPinnedBytes :: MonadIO m =>
441+{-# INLINE pinnedNewBytes #-}
442+pinnedNewBytes :: MonadIO m =>
446443#ifdef DEVBUILD
447444 Unbox a =>
448445#endif
449446 Int -> m (MutArray a)
450−newPinnedBytes = newBytesAs Pinned
447+pinnedNewBytes = newBytesAs Pinned
451448
452449-- | Like 'newArrayWith' but using an allocator is a pinned memory allocator and
453450-- the alignment is dictated by the 'Unboxed' instance of the type.
454451--
455452-- /Internal/
456−{-# INLINE newAlignedPinned #-}
457−newAlignedPinned :: (MonadIO m, Unbox a) => Int -> Int -> m (MutArray a)
458−newAlignedPinned =
459− newArrayWith (\s a -> liftIO $ Unboxed.newAlignedPinnedBytes s a)
453+{-# INLINE pinnedNewAligned #-}
454+pinnedNewAligned :: (MonadIO m, Unbox a) => Int -> Int -> m (MutArray a)
455+pinnedNewAligned =
456+ newArrayWith (\s a -> liftIO $ Unboxed.pinnedNewAlignedBytes s a)
460457
461458{-# INLINE newAs #-}
462459newAs :: (MonadIO m, Unbox a) => PinnedState -> Int -> m (MutArray a)
@@ -471,9 +468,9 @@ newAs ps =
471468-- the array is uninitialized and the allocation is aligned as per the 'Unboxed'
472469-- instance of the type.
473470--
474−{-# INLINE newPinned #-}
475−newPinned :: forall m a. (MonadIO m, Unbox a) => Int -> m (MutArray a)
476−newPinned = newAs Pinned
471+{-# INLINE pinnedNew #-}
472+pinnedNew :: forall m a. (MonadIO m, Unbox a) => Int -> m (MutArray a)
473+pinnedNew = newAs Pinned
477474
478475-- | Allocates an empty unpinned array that can hold 'count' items. The memory
479476-- of the array is uninitialized.
@@ -482,21 +479,6 @@ newPinned = newAs Pinned
482479new :: (MonadIO m, Unbox a) => Int -> m (MutArray a)
483480new = newAs Unpinned
484481
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−
500482-------------------------------------------------------------------------------
501483-- Random writes
502484-------------------------------------------------------------------------------
@@ -755,7 +737,7 @@ reallocExplicit elemSize newCapacityInBytes MutArray{..} = do
755737 let newCapMaxInBytes = roundUpLargeArray newCapacityInBytes
756738 contents <-
757739 if Unboxed.isPinned arrContents
758− then Unboxed.newPinnedBytes newCapMaxInBytes
740+ then Unboxed.pinnedNewBytes newCapMaxInBytes
759741 else Unboxed.newUnpinnedBytes newCapMaxInBytes
760742 let !(MutableByteArray mbarrFrom#) = arrContents
761743 !(MutableByteArray mbarrTo#) = contents
@@ -1864,15 +1846,15 @@ writeRevN = writeRevNWith new
18641846-- | @pinnedWriteNAligned align n@ folds a maximum of @n@ elements from the
18651847-- input stream to a 'MutArray' aligned to the given size.
18661848--
1867−-- >>> pinnedWriteNAligned align = MutArray.writeNWith (MutArray.newAlignedPinned align)
1868−-- >>> pinnedWriteNAligned align n = MutArray.writeAppendN n (MutArray.newAlignedPinned align n)
1849+-- >>> pinnedWriteNAligned align = MutArray.writeNWith (MutArray.pinnedNewAligned align)
1850+-- >>> pinnedWriteNAligned align n = MutArray.writeAppendN n (MutArray.pinnedNewAligned align n)
18691851--
18701852-- /Pre-release/
18711853--
18721854{-# INLINE_NORMAL pinnedWriteNAligned #-}
18731855pinnedWriteNAligned :: forall m a. (MonadIO m, Unbox a)
18741856 => Int -> Int -> Fold m a (MutArray a)
1875−pinnedWriteNAligned align = writeNWith (newAlignedPinned align)
1857+pinnedWriteNAligned align = writeNWith (pinnedNewAligned align)
18761858
18771859-- XXX Buffer to a list instead?
18781860--
core/src/Streamly/Internal/Data/Fold.hs+1−1
@@ -2440,7 +2440,7 @@ bottomBy cmp n = Fold step initial extract
24402440 where
24412441
24422442 initial = do
2443− arr <- MA.newPinned n
2443+ arr <- MA.pinnedNew n
24442444 if n <= 0
24452445 then return $ Done arr
24462446 else return $ Partial (arr, 0)
core/src/Streamly/Internal/Data/Stream/Chunked.hs+3−3
@@ -476,7 +476,7 @@ splitAtArrayListRev n ls
476476spliceArraysLenUnsafe :: (MonadIO m, Unbox a)
477477 => Int -> Stream m (MutArray a) -> m (MutArray a)
478478spliceArraysLenUnsafe len buffered = do
479− arr <- liftIO $ MA.newPinned len
479+ arr <- liftIO $ MA.pinnedNew len
480480 D.foldlM' MA.spliceUnsafe (return arr) buffered
481481
482482{-# INLINE _spliceArrays #-}
@@ -485,7 +485,7 @@ _spliceArrays :: (MonadIO m, Unbox a)
485485_spliceArrays s = do
486486 buffered <- D.foldr K.cons K.nil s
487487 len <- K.fold FL.sum (fmap Array.length buffered)
488− arr <- liftIO $ MA.newPinned len
488+ arr <- liftIO $ MA.pinnedNew len
489489 final <- D.foldlM' writeArr (return arr) (toStream buffered)
490490 return $ A.unsafeFreeze final
491491
@@ -507,7 +507,7 @@ spliceArraysRealloced :: forall m a. (MonadIO m, Unbox a)
507507 => Stream m (Array a) -> m (Array a)
508508spliceArraysRealloced s = do
509509 let n = allocBytesToElemCount (undefined :: a) (4 * 1024)
510− idst = liftIO $ MA.newPinned n
510+ idst = liftIO $ MA.pinnedNew n
511511
512512 arr <- D.foldlM' MA.spliceExp idst (fmap A.unsafeThaw s)
513513 liftIO $ A.unsafeFreeze <$> MA.rightSize arr
core/src/Streamly/Internal/Data/Unbox.hs+13−13
@@ -16,8 +16,8 @@ module Streamly.Internal.Data.Unbox
1616 , unpin
1717 , newBytes
1818 , newUnpinnedBytes
19− , newPinnedBytes
20− , newAlignedPinnedBytes
19+ , pinnedNewBytes
20+ , pinnedNewAlignedBytes
2121 , nil
2222
2323 -- * Type Parser and Builder
@@ -117,21 +117,21 @@ newUnpinnedBytes (I# nbytes) = IO $ \s ->
117117 let c = MutableByteArray mbarr#
118118 in (# s', c #)
119119
120−{-# INLINE newPinnedBytes #-}
121−newPinnedBytes :: Int -> IO MutableByteArray
122−newPinnedBytes nbytes | nbytes < 0 =
123− errorWithoutStackTrace "newPinnedBytes: size must be >= 0"
124−newPinnedBytes (I# nbytes) = IO $ \s ->
120+{-# INLINE pinnedNewBytes #-}
121+pinnedNewBytes :: Int -> IO MutableByteArray
122+pinnedNewBytes nbytes | nbytes < 0 =
123+ errorWithoutStackTrace "pinnedNewBytes: size must be >= 0"
124+pinnedNewBytes (I# nbytes) = IO $ \s ->
125125 case newPinnedByteArray# nbytes s of
126126 (# s', mbarr# #) ->
127127 let c = MutableByteArray mbarr#
128128 in (# s', c #)
129129
130−{-# INLINE newAlignedPinnedBytes #-}
131−newAlignedPinnedBytes :: Int -> Int -> IO MutableByteArray
132−newAlignedPinnedBytes nbytes _align | nbytes < 0 =
133− errorWithoutStackTrace "newAlignedPinnedBytes: size must be >= 0"
134−newAlignedPinnedBytes (I# nbytes) (I# align) = IO $ \s ->
130+{-# INLINE pinnedNewAlignedBytes #-}
131+pinnedNewAlignedBytes :: Int -> Int -> IO MutableByteArray
132+pinnedNewAlignedBytes nbytes _align | nbytes < 0 =
133+ errorWithoutStackTrace "pinnedNewAlignedBytes: size must be >= 0"
134+pinnedNewAlignedBytes (I# nbytes) (I# align) = IO $ \s ->
135135 case newAlignedPinnedByteArray# nbytes align s of
136136 (# s', mbarr# #) ->
137137 let c = MutableByteArray mbarr#
@@ -140,7 +140,7 @@ newAlignedPinnedBytes (I# nbytes) (I# align) = IO $ \s ->
140140{-# INLINE newBytes #-}
141141newBytes :: PinnedState -> Int -> IO MutableByteArray
142142newBytes Unpinned = newUnpinnedBytes
143−newBytes Pinned = newPinnedBytes
143+newBytes Pinned = pinnedNewBytes
144144
145145-------------------------------------------------------------------------------
146146-- 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)
180180{-# INLINABLE getChunk #-}
181181getChunk :: MonadIO m => Int -> Handle -> m (Array Word8)
182182getChunk size h = liftIO $ do
183− arr <- MArray.newPinnedBytes size
183+ arr <- MArray.pinnedNewBytes size
184184 -- ptr <- mallocPlainForeignPtrAlignedBytes size (alignment (undefined :: Word8))
185185 MArray.asPtrUnsafe arr $ \p -> do
186186 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
146146import qualified Streamly.Data.Array as A
147147import qualified Streamly.Data.Fold as FL
148148import qualified Streamly.Internal.Data.Array.Mut as MArray
149− (MutArray(..), newPinnedBytes, asPtrUnsafe)
149+ (MutArray(..), pinnedNewBytes, asPtrUnsafe)
150150import qualified Streamly.Internal.Data.Stream.Chunked as AS
151151import qualified Streamly.Internal.Data.Stream as S
152152import qualified Streamly.Internal.Data.Stream.StreamD.Type as D
@@ -217,7 +217,7 @@ openFile path mode = Handle . fst <$> FD.openFile path mode True
217217{-# INLINABLE readArrayUpto #-}
218218readArrayUpto :: Int -> Handle -> IO (Array Word8)
219219readArrayUpto size (Handle fd) = do
220− arr <- MArray.newPinnedBytes size
220+ arr <- MArray.pinnedNewBytes size
221221 -- ptr <- mallocPlainForeignPtrAlignedBytes size (alignment (undefined :: Word8))
222222 MArray.asPtrUnsafe arr $ \p -> do
223223 -- 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
101101 , writeNAs
102102 )
103103import qualified Streamly.Internal.Data.Array.Mut as MArray
104− (MutArray(..), newPinnedBytes, asPtrUnsafe)
104+ (MutArray(..), pinnedNewBytes, asPtrUnsafe)
105105import qualified Streamly.Internal.Data.Stream as S
106106import qualified Streamly.Internal.Data.Stream.StreamD.Type as D
107107 (Stream(..), Step(..))
@@ -260,7 +260,7 @@ readArrayUptoWith
260260 -> h
261261 -> IO (Array Word8)
262262readArrayUptoWith f size h = do
263− arr <- MArray.newPinnedBytes size
263+ arr <- MArray.pinnedNewBytes size
264264 -- ptr <- mallocPlainForeignPtrAlignedBytes size (alignment (undefined :: Word8))
265265 MArray.asPtrUnsafe arr $ \p -> do
266266 n <- f h p size
test/Streamly/Test/Data/Array.hs+2−2
@@ -150,7 +150,7 @@ testBubbleWith asc =
150150 else MA.bubble (flip compare) arr
151151 return arr
152152 )
153− (MA.newPinned $ length ls)
153+ (MA.pinnedNew $ length ls)
154154
155155testBubbleAsc :: Property
156156testBubbleAsc = testBubbleWith True
@@ -160,7 +160,7 @@ testBubbleDesc = testBubbleWith False
160160
161161testByteLengthWithMA :: forall a. Unbox a => a -> IO ()
162162testByteLengthWithMA _ = do
163− arrA <- MA.newPinned 100 :: IO (MutArray a)
163+ arrA <- MA.pinnedNew 100 :: IO (MutArray a)
164164 let arrW8 = MA.castUnsafe arrA :: MutArray Word8
165165 MA.byteLength arrA `shouldBe` MA.length arrW8
166166
test/Streamly/Test/Data/Array/Mut.hs+1−1
@@ -42,7 +42,7 @@ testAppend =
4242
4343 action ls = do
4444 x <- Stream.fold
45− (MArray.writeAppend (MArray.newPinned 0))
45+ (MArray.writeAppend (MArray.pinnedNew 0))
4646 (Stream.fromList (ls::[Int]))
4747 lst <- MArray.toList x
4848 assert (ls == lst)