on master help sign in
Copy only the required slice while pinning/unpinning the array
commit6a3971955ca5ae4b6034752ef38fb8884c2db2ac authorAdithya Kumar <adithya@composewell.com> dateMon 21 Aug 2023, 12:53 (3 years ago) parentab0c439
1 file changed+8−6
core/src/Streamly/Internal/Data/MutArray/Type.hs+8−6
core/src/Streamly/Internal/Data/MutArray/Type.hs+8−6
@@ -362,17 +362,19 @@ data MutArray a =
362362-- overhead is a copy if the input array is unpinned.
363363{-# INLINE pin #-}
364364pin :: MutArray a -> IO (MutArray a)
365−pin arr@MutArray{..} = do
366− contents <- Unboxed.pin arrContents
367− return $ arr {arrContents = contents}
365+pin arr@MutArray{..} =
366+ if Unboxed.isPinned arrContents
367+ then pure arr
368+ else pinnedClone arr
368369
369370-- | Copy the 'MutArray' to unpinned memory if pinned, else do nothing. The
370371-- overhead is a copy if the input array is pinned.
371372{-# INLINE unpin #-}
372373unpin :: MutArray a -> IO (MutArray a)
373−unpin arr@MutArray{..} = do
374− contents <- Unboxed.unpin arrContents
375− return $ arr {arrContents = contents}
374+unpin arr@MutArray{..} =
375+ if Unboxed.isPinned arrContents
376+ then clone arr
377+ else pure arr
376378
377379{-# INLINE isPinned #-}
378380isPinned :: MutArray a -> Bool