on master help sign in
Move putSliceUnsafe from MutArray to Unbox
commitab0c439e771d837ef4ce8d411a57b0c74042d351 authorAdithya Kumar <adithya@composewell.com> dateMon 21 Aug 2023, 12:49 (3 years ago) parentc4b717e
2 files changed+42−30
core/src/Streamly/Internal/Data/MutArray/Type.hs+9−30 core/src/Streamly/Internal/Data/Unbox.hs+33
core/src/Streamly/Internal/Data/MutArray/Type.hs+9−30
@@ -214,7 +214,6 @@ module Streamly.Internal.Data.MutArray.Type
214214 , splice
215215 , spliceExp
216216 , spliceUnsafe
217− , putSliceUnsafe
218217 -- , putSlice
219218 -- , appendSlice
220219 -- , appendSliceFrom
@@ -246,6 +245,7 @@ import Streamly.Internal.Data.Unbox
246245 , PinnedState(..)
247246 , getMutableByteArray#
248247 , touch
248+ , putSliceUnsafe
249249 )
250250import GHC.Base
251251 ( IO(..)
@@ -2099,10 +2099,9 @@ cloneAs ps src =
20992099 liftIO $ do
21002100 let startSrc = arrStart src
21012101 srcLen = arrEnd src - startSrc
2102− newArrDst = srcLen
2103− newArr <- newBytesAs ps srcLen
2104− putSliceUnsafe src startSrc newArr 0 srcLen
2105− return $ newArr {arrEnd = newArrDst}
2102+ newArrContents <- Unboxed.newBytesAs ps srcLen
2103+ putSliceUnsafe (arrContents src) startSrc newArrContents 0 srcLen
2104+ return $ MutArray newArrContents 0 srcLen srcLen
21062105
21072106{-# INLINE clone #-}
21082107clone :: MonadIO m => MutArray a -> m (MutArray a)
@@ -2116,26 +2115,6 @@ pinnedClone = cloneAs Pinned
21162115-- Combining
21172116-------------------------------------------------------------------------------
21182117
2119−-- XXX Change the signature of this function to
2120−-- putSliceUnsafe :: MonadIO m => MutableByteArray -> Int -> MutableByteArray -> Int -> Int -> m ()
2121−-- We don't use the relative indexes but the absolute ones. So we should MutableByteArray to remove any confusion.
2122−
2123−-- | Put a sub range of a source array into a subrange of a destination array.
2124−-- This is not safe as it does not check the bounds.
2125−{-# INLINE putSliceUnsafe #-}
2126−putSliceUnsafe :: MonadIO m => MutArray a -> Int -> MutArray a -> Int -> Int -> m ()
2127−putSliceUnsafe src srcStartBytes dst dstStartBytes lenBytes = liftIO $ do
2128− assertM(lenBytes <= arrBound dst - dstStartBytes)
2129− assertM(lenBytes <= arrEnd src - srcStartBytes)
2130− let !(I# srcStartBytes#) = srcStartBytes
2131− !(I# dstStartBytes#) = dstStartBytes
2132− !(I# lenBytes#) = lenBytes
2133− let arrS# = getMutableByteArray# (arrContents src)
2134− arrD# = getMutableByteArray# (arrContents dst)
2135− IO $ \s# -> (# copyMutableByteArray#
2136− arrS# srcStartBytes# arrD# dstStartBytes# lenBytes# s#
2137− , () #)
2138−
21392118-- | Copy two arrays into a newly allocated array. If the first array is pinned
21402119-- the spliced array is also pinned.
21412120{-# INLINE spliceCopy #-}
@@ -2155,10 +2134,9 @@ spliceCopy arr1 arr2 = liftIO $ do
21552134 then Unboxed.pinnedNewBytes newLen
21562135 else Unboxed.newBytes newLen
21572136 let len = len1 + len2
2158− newArr = MutArray newArrContents 0 len len
2159− putSliceUnsafe arr1 start1 newArr 0 len1
2160− putSliceUnsafe arr2 start2 newArr len1 len2
2161− return newArr
2137+ putSliceUnsafe (arrContents arr1) start1 newArrContents 0 len1
2138+ putSliceUnsafe (arrContents arr2) start2 newArrContents len1 len2
2139+ return $ MutArray newArrContents 0 len len
21622140
21632141-- | Really really unsafe, appends the second array into the first array. If
21642142-- the first array does not have enough space it may cause silent data
@@ -2172,7 +2150,8 @@ spliceUnsafe dst src =
21722150 srcLen = arrEnd src - startSrc
21732151 endDst = arrEnd dst
21742152 assertM(endDst + srcLen <= arrBound dst)
2175− putSliceUnsafe src startSrc dst endDst srcLen
2153+ putSliceUnsafe
2154+ (arrContents src) startSrc (arrContents dst) endDst srcLen
21762155 return $ dst {arrEnd = endDst + srcLen}
21772156
21782157-- | @spliceWith sizer dst src@ mutates @dst@ to append @src@. If there is no
core/src/Streamly/Internal/Data/Unbox.hs+33
@@ -26,6 +26,7 @@ module Streamly.Internal.Data.Unbox
2626 , pinnedNewBytes
2727 , pinnedNewAlignedBytes
2828 , nil
29+ , putSliceUnsafe
2930
3031 -- * Type Parser and Builder
3132 , BoundedPtr (..)
@@ -53,6 +54,7 @@ module Streamly.Internal.Data.Unbox
5354#include "MachDeps.h"
5455#include "ArrayMacros.h"
5556
57+import Control.Monad.IO.Class (MonadIO(..))
5658import Control.Monad (void, when)
5759import Data.Complex (Complex((:+)))
5860import Data.Functor ((<&>))
@@ -152,6 +154,37 @@ newBytesAs :: PinnedState -> Int -> IO MutableByteArray
152154newBytesAs Unpinned = newBytes
153155newBytesAs Pinned = pinnedNewBytes
154156
157+-------------------------------------------------------------------------------
158+-- Copying
159+-------------------------------------------------------------------------------
160+
161+-- | Put a sub range of a source array into a subrange of a destination array.
162+-- This is not safe as it does not check the bounds.
163+{-# INLINE putSliceUnsafe #-}
164+putSliceUnsafe ::
165+ MonadIO m
166+ => MutableByteArray
167+ -> Int
168+ -> MutableByteArray
169+ -> Int
170+ -> Int
171+ -> m ()
172+putSliceUnsafe src srcStartBytes dst dstStartBytes lenBytes = liftIO $ do
173+#ifdef DEBUG
174+ srcLen <- sizeOfMutableByteArray src
175+ dstLen <- sizeOfMutableByteArray src
176+ when (srcLen - srcStartBytes < lenBytes) $ error "SRC: Insufficient length."
177+ when (dstLen - dstStartBytes < lenBytes) $ error "DST: Insufficient length."
178+#endif
179+ let !(I# srcStartBytes#) = srcStartBytes
180+ !(I# dstStartBytes#) = dstStartBytes
181+ !(I# lenBytes#) = lenBytes
182+ let arrS# = getMutableByteArray# src
183+ arrD# = getMutableByteArray# dst
184+ IO $ \s# -> (# copyMutableByteArray#
185+ arrS# srcStartBytes# arrD# dstStartBytes# lenBytes# s#
186+ , () #)
187+
155188-------------------------------------------------------------------------------
156189-- Pinning & Unpinning
157190-------------------------------------------------------------------------------