on master help sign in
Move putSliceUnsafe from MutArray to Unbox
ab0c439Adithya Kumar
core/src/Streamly/Internal/Data/MutArray/Type.hs+9−30
@@ -214,7 +214,6 @@ module Streamly.Internal.Data.MutArray.Type
214 , splice
214 , splice
215 , spliceExp
215 , spliceExp
216 , spliceUnsafe
216 , spliceUnsafe
217− , putSliceUnsafe
218 -- , putSlice
217 -- , putSlice
219 -- , appendSlice
218 -- , appendSlice
220 -- , appendSliceFrom
219 -- , appendSliceFrom
@@ -246,6 +245,7 @@ import Streamly.Internal.Data.Unbox
246 , PinnedState(..)
245 , PinnedState(..)
247 , getMutableByteArray#
246 , getMutableByteArray#
248 , touch
247 , touch
248+ , putSliceUnsafe
249 )
249 )
250import GHC.Base
250import GHC.Base
251 ( IO(..)
251 ( IO(..)
@@ -2099,10 +2099,9 @@ cloneAs ps src =
2099 liftIO $ do
2099 liftIO $ do
2100 let startSrc = arrStart src
2100 let startSrc = arrStart src
2101 srcLen = arrEnd src - startSrc
2101 srcLen = arrEnd src - startSrc
2102− newArrDst = srcLen
2103− newArr <- newBytesAs ps srcLen
2102+ newArrContents <- Unboxed.newBytesAs ps srcLen
2104− putSliceUnsafe src startSrc newArr 0 srcLen
2103+ putSliceUnsafe (arrContents src) startSrc newArrContents 0 srcLen
2105− return $ newArr {arrEnd = newArrDst}
2104+ return $ MutArray newArrContents 0 srcLen srcLen
2106
2105
2107{-# INLINE clone #-}
2106{-# INLINE clone #-}
2108clone :: MonadIO m => MutArray a -> m (MutArray a)
2107clone :: MonadIO m => MutArray a -> m (MutArray a)
@@ -2116,26 +2115,6 @@ pinnedClone = cloneAs Pinned
2116-- Combining
2115-- Combining
2117-------------------------------------------------------------------------------
2116-------------------------------------------------------------------------------
2118
2117
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−
2139-- | Copy two arrays into a newly allocated array. If the first array is pinned
2118-- | Copy two arrays into a newly allocated array. If the first array is pinned
2140-- the spliced array is also pinned.
2119-- the spliced array is also pinned.
2141{-# INLINE spliceCopy #-}
2120{-# INLINE spliceCopy #-}
@@ -2155,10 +2134,9 @@ spliceCopy arr1 arr2 = liftIO $ do
2155 then Unboxed.pinnedNewBytes newLen
2134 then Unboxed.pinnedNewBytes newLen
2156 else Unboxed.newBytes newLen
2135 else Unboxed.newBytes newLen
2157 let len = len1 + len2
2136 let len = len1 + len2
2158− newArr = MutArray newArrContents 0 len len
2159− putSliceUnsafe arr1 start1 newArr 0 len1
2137+ putSliceUnsafe (arrContents arr1) start1 newArrContents 0 len1
2160− putSliceUnsafe arr2 start2 newArr len1 len2
2138+ putSliceUnsafe (arrContents arr2) start2 newArrContents len1 len2
2161− return newArr
2139+ return $ MutArray newArrContents 0 len len
2162
2140
2163-- | Really really unsafe, appends the second array into the first array. If
2141-- | Really really unsafe, appends the second array into the first array. If
2164-- the first array does not have enough space it may cause silent data
2142-- the first array does not have enough space it may cause silent data
@@ -2172,7 +2150,8 @@ spliceUnsafe dst src =
2172 srcLen = arrEnd src - startSrc
2150 srcLen = arrEnd src - startSrc
2173 endDst = arrEnd dst
2151 endDst = arrEnd dst
2174 assertM(endDst + srcLen <= arrBound dst)
2152 assertM(endDst + srcLen <= arrBound dst)
2153+ putSliceUnsafe
2175− putSliceUnsafe src startSrc dst endDst srcLen
2154+ (arrContents src) startSrc (arrContents dst) endDst srcLen
2176 return $ dst {arrEnd = endDst + srcLen}
2155 return $ dst {arrEnd = endDst + srcLen}
2177
2156
2178-- | @spliceWith sizer dst src@ mutates @dst@ to append @src@. If there is no
2157-- | @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
26 , pinnedNewBytes
26 , pinnedNewBytes
27 , pinnedNewAlignedBytes
27 , pinnedNewAlignedBytes
28 , nil
28 , nil
29+ , putSliceUnsafe
29
30
30 -- * Type Parser and Builder
31 -- * Type Parser and Builder
31 , BoundedPtr (..)
32 , BoundedPtr (..)
@@ -53,6 +54,7 @@ module Streamly.Internal.Data.Unbox
53#include "MachDeps.h"
54#include "MachDeps.h"
54#include "ArrayMacros.h"
55#include "ArrayMacros.h"
55
56
57+import Control.Monad.IO.Class (MonadIO(..))
56import Control.Monad (void, when)
58import Control.Monad (void, when)
57import Data.Complex (Complex((:+)))
59import Data.Complex (Complex((:+)))
58import Data.Functor ((<&>))
60import Data.Functor ((<&>))
@@ -152,6 +154,37 @@ newBytesAs :: PinnedState -> Int -> IO MutableByteArray
152newBytesAs Unpinned = newBytes
154newBytesAs Unpinned = newBytes
153newBytesAs Pinned = pinnedNewBytes
155newBytesAs Pinned = pinnedNewBytes
154
156
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+
155-------------------------------------------------------------------------------
188-------------------------------------------------------------------------------
156-- Pinning & Unpinning
189-- Pinning & Unpinning
157-------------------------------------------------------------------------------
190-------------------------------------------------------------------------------