on master help sign in
Stop exporting touch from Internal.Data.Unbox
628d6c4Harendra Kumar
core/src/Streamly/Data/Serialize.hs+5
@@ -43,6 +43,11 @@ module Streamly.Data.Serialize
43
43
44 -- * MutableByteArray
44 -- * MutableByteArray
45 MutableByteArray
45 MutableByteArray
46+ -- , isPinned
47+ -- , pin
48+ -- , unpin
49+ -- , newBytes -- XXX rename to newByteArray
50+ -- , pinnedNewBytes -- XXX rename to pinnedNewByteArray
46
51
47 -- * Unbox
52 -- * Unbox
48 , Unbox(..)
53 , Unbox(..)
core/src/Streamly/Internal/Data/Fold/Chunked.hs−2
@@ -67,7 +67,6 @@ import Data.Bifunctor (first)
67import Data.Proxy (Proxy(..))
67import Data.Proxy (Proxy(..))
68import Streamly.Internal.Data.Unbox (Unbox(..))
68import Streamly.Internal.Data.Unbox (Unbox(..))
69import GHC.Types (SPEC(..))
69import GHC.Types (SPEC(..))
70−import Streamly.Internal.Data.MutArray.Type (touch)
71import Streamly.Internal.Data.Array (Array(..))
70import Streamly.Internal.Data.Array (Array(..))
72import Streamly.Internal.Data.Parser (Initial(..), Step(..))
71import Streamly.Internal.Data.Parser (Initial(..), Step(..))
73import Streamly.Internal.Data.Tuple.Strict (Tuple'(..))
72import Streamly.Internal.Data.Tuple.Strict (Tuple'(..))
@@ -164,7 +163,6 @@ fromParserD (ParserD.Parser step1 initial1 extract1) =
164
163
165 goArray !_ !cur !fs = do
164 goArray !_ !cur !fs = do
166 x <- liftIO $ peekByteIndex cur contents
165 x <- liftIO $ peekByteIndex cur contents
167− liftIO $ touch contents
168 res <- step1 fs x
166 res <- step1 fs x
169 let elemSize = SIZE_OF(a)
167 let elemSize = SIZE_OF(a)
170 next = INDEX_NEXT(cur,a)
168 next = INDEX_NEXT(cur,a)
core/src/Streamly/Internal/Data/MutArray/Type.hs+10−18
@@ -32,7 +32,6 @@ module Streamly.Internal.Data.MutArray.Type
32 -- $arrayNotes
32 -- $arrayNotes
33 MutArray (..)
33 MutArray (..)
34 , MutableByteArray
34 , MutableByteArray
35− , touch
36 , pin
35 , pin
37 , unpin
36 , unpin
38 , isPinned
37 , isPinned
@@ -244,13 +243,11 @@ import Streamly.Internal.Data.Unbox
244 , Unbox(..)
243 , Unbox(..)
245 , PinnedState(..)
244 , PinnedState(..)
246 , getMutableByteArray#
245 , getMutableByteArray#
247− , touch
248 , putSliceUnsafe
246 , putSliceUnsafe
249 )
247 )
250import GHC.Base
248import GHC.Base
251 ( IO(..)
249 ( IO(..)
252 , Int(..)
250 , Int(..)
253− , byteArrayContents#
254 , compareByteArrays#
251 , compareByteArrays#
255 , copyMutableByteArray#
252 , copyMutableByteArray#
256 )
253 )
@@ -2325,13 +2322,12 @@ cast arr =
2325-- guarantees that unsafe calls will be performed in the calling thread. Making
2322-- guarantees that unsafe calls will be performed in the calling thread. Making
2326-- it safe to pass heap-allocated objects to unsafe functions.
2323-- it safe to pass heap-allocated objects to unsafe functions.
2327
2324
2328−-- Unsafe because of direct pointer operations. The user must ensure that they
2329−-- are writing within the legal bounds of the array. Should we just name it
2330−-- asPtr, the unsafety is implicit for any pointer operations. And we are safe
2325+-- Should we just name it asPtr, the unsafety is implicit for any pointer
2331−-- from Haskell perspective because we will be pinning the memory.
2326+-- operations. And we are safe from Haskell perspective because we will be
2327+-- pinning the memory.
2332
2328
2333−-- | Use an @MutArray a@ as @Ptr a@. This is useful when we want to pass an array
2329+-- | Use a @MutArray a@ as @Ptr a@. This is useful when we want to pass an
2334−-- as a pointer to some operating system call or to a "safe" FFI call.
2330+-- array as a pointer to some operating system call or to a "safe" FFI call.
2335--
2331--
2336-- If the array is not pinned it is copied to pinned memory before passing it
2332-- If the array is not pinned it is copied to pinned memory before passing it
2337-- to the monadic action.
2333-- to the monadic action.
@@ -2341,20 +2337,16 @@ cast arr =
2341-- opportunistically before this operation occurs, to avoid the cost of a copy
2337-- opportunistically before this operation occurs, to avoid the cost of a copy
2342-- if possible.
2338-- if possible.
2343--
2339--
2344−-- /Unsafe/
2340+-- /Unsafe/ because of direct pointer operations. The user must ensure that
2341+-- they are writing within the legal bounds of the array.
2345--
2342--
2346-- /Pre-release/
2343-- /Pre-release/
2347--
2344--
2348{-# INLINE asPtrUnsafe #-}
2345{-# INLINE asPtrUnsafe #-}
2349asPtrUnsafe :: MonadIO m => MutArray a -> (Ptr a -> m b) -> m b
2346asPtrUnsafe :: MonadIO m => MutArray a -> (Ptr a -> m b) -> m b
2350−asPtrUnsafe arr f = do
2347+asPtrUnsafe arr f =
2351− contents <- liftIO $ Unboxed.pin $ arrContents arr
2348+ Unboxed.asPtrUnsafe
2352− let !ptr = Ptr (byteArrayContents#
2353− (unsafeCoerce# (getMutableByteArray# contents)))
2354−
2355− r <- f (ptr `plusPtr` arrStart arr)
2349+ (arrContents arr) (\ptr -> f (ptr `plusPtr` arrStart arr))
2356− liftIO $ touch contents
2357− return r
2358
2350
2359-------------------------------------------------------------------------------
2351-------------------------------------------------------------------------------
2360-- Equality
2352-- Equality
core/src/Streamly/Internal/Data/Unbox.hs+44−6
@@ -17,9 +17,9 @@ module Streamly.Internal.Data.Unbox
17 MutableByteArray(..)
17 MutableByteArray(..)
18 , getMutableByteArray#
18 , getMutableByteArray#
19 , sizeOfMutableByteArray
19 , sizeOfMutableByteArray
20− , touch
21 , nil
20 , nil
22 , putSliceUnsafe
21 , putSliceUnsafe
22+ , asPtrUnsafe
23
23
24 -- ** Allocation
24 -- ** Allocation
25 , PinnedState(..)
25 , PinnedState(..)
@@ -106,11 +106,6 @@ data MutableByteArray = MutableByteArray (MutableByteArray# RealWorld)
106getMutableByteArray# :: MutableByteArray -> MutableByteArray# RealWorld
106getMutableByteArray# :: MutableByteArray -> MutableByteArray# RealWorld
107getMutableByteArray# (MutableByteArray mbarr) = mbarr
107getMutableByteArray# (MutableByteArray mbarr) = mbarr
108
108
109−{-# INLINE touch #-}
110−touch :: MutableByteArray -> IO ()
111−touch (MutableByteArray contents) =
112− IO $ \s -> case touch# contents s of s' -> (# s', () #)
113−
114-- | Return the size of the array in bytes.
109-- | Return the size of the array in bytes.
115{-# INLINE sizeOfMutableByteArray #-}
110{-# INLINE sizeOfMutableByteArray #-}
116sizeOfMutableByteArray :: MutableByteArray -> IO Int
111sizeOfMutableByteArray :: MutableByteArray -> IO Int
@@ -119,6 +114,49 @@ sizeOfMutableByteArray (MutableByteArray arr) =
119 case getSizeofMutableByteArray# arr s of
114 case getSizeofMutableByteArray# arr s of
120 (# s1, i #) -> (# s1, I# i #)
115 (# s1, i #) -> (# s1, I# i #)
121
116
117+{-# INLINE touch #-}
118+touch :: MutableByteArray -> IO ()
119+touch (MutableByteArray contents) =
120+ IO $ \s -> case touch# contents s of s' -> (# s', () #)
121+
122+-- XXX We can provide another API for "unsafe" FFI calls passing an unlifted
123+-- pointer to the FFI call. For unsafe calls we do not need to pin the array.
124+-- We can pass an unlifted pointer to the FFI routine to avoid GC kicking in
125+-- before the pointer is wrapped.
126+--
127+-- From the GHC manual:
128+--
129+-- GHC, since version 8.4, guarantees that garbage collection will never occur
130+-- during an unsafe call, even in the bytecode interpreter, and further
131+-- guarantees that unsafe calls will be performed in the calling thread. Making
132+-- it safe to pass heap-allocated objects to unsafe functions.
133+
134+-- | Use a @MutableByteArray@ as @Ptr a@. This is useful when we want to pass
135+-- an array as a pointer to some operating system call or to a "safe" FFI call.
136+--
137+-- If the array is not pinned it is copied to pinned memory before passing it
138+-- to the monadic action.
139+--
140+-- /Performance Notes:/ Forces a copy if the array is not pinned. It is advised
141+-- that the programmer keeps this in mind and creates a pinned array
142+-- opportunistically before this operation occurs, to avoid the cost of a copy
143+-- if possible.
144+--
145+-- /Unsafe/ because of direct pointer operations. The user must ensure that
146+-- they are writing within the legal bounds of the array.
147+--
148+-- /Pre-release/
149+--
150+{-# INLINE asPtrUnsafe #-}
151+asPtrUnsafe :: MonadIO m => MutableByteArray -> (Ptr a -> m b) -> m b
152+asPtrUnsafe arr f = do
153+ contents <- liftIO $ pin arr
154+ let !ptr = Ptr (byteArrayContents#
155+ (unsafeCoerce# (getMutableByteArray# contents)))
156+ r <- f ptr
157+ liftIO $ touch contents
158+ return r
159+
122--------------------------------------------------------------------------------
160--------------------------------------------------------------------------------
123-- Creation
161-- Creation
124--------------------------------------------------------------------------------
162--------------------------------------------------------------------------------