on master help sign in
Change splice to spliceUnsafe
55cc5d0Harendra Kumar
core/src/Streamly/Internal/Data/MutArray/Stream.hs+17−7
@@ -111,8 +111,11 @@ packArraysChunksOf n (D.Stream step state) =
111 then return $
111 then return $
112 D.Skip (SpliceYielding buf (SpliceBuffering s arr))
112 D.Skip (SpliceYielding buf (SpliceBuffering s arr))
113 else do
113 else do
114− buf1 <- MArray.splice buf arr
114+ buf' <- if MArray.byteCapacity buf < n
115+ then liftIO $ MArray.realloc n buf
116+ else return buf
117+ buf'' <- MArray.spliceUnsafe buf' arr
115− return $ D.Skip (SpliceBuffering s buf1)
118+ return $ D.Skip (SpliceBuffering s buf'')
116 D.Skip s -> return $ D.Skip (SpliceBuffering s buf)
119 D.Skip s -> return $ D.Skip (SpliceBuffering s buf)
117 D.Stop -> return $ D.Skip (SpliceYielding buf SpliceFinish)
120 D.Stop -> return $ D.Skip (SpliceYielding buf SpliceFinish)
118
121
@@ -155,19 +158,22 @@ lpackArraysChunksOf n (Fold step1 initial1 _ final1) =
155
158
156 step (Tuple' (Just buf) r1) arr = do
159 step (Tuple' (Just buf) r1) arr = do
157 let len = MArray.byteLength buf + MArray.byteLength arr
160 let len = MArray.byteLength buf + MArray.byteLength arr
158− buf1 <- MArray.splice buf arr
161+ buf' <- if MArray.byteCapacity buf < len
162+ then liftIO $ MArray.realloc (max n len) buf
163+ else return buf
164+ buf'' <- MArray.spliceUnsafe buf' arr
159
165
160 -- XXX this is common in both the equations of step
166 -- XXX this is common in both the equations of step
161 if len >= n
167 if len >= n
162 then do
168 then do
163− r <- step1 r1 buf1
169+ r <- step1 r1 buf''
164 case r of
170 case r of
165 FL.Done _ -> return $ FL.Done ()
171 FL.Done _ -> return $ FL.Done ()
166 FL.Partial s -> do
172 FL.Partial s -> do
167 _ <- final1 s
173 _ <- final1 s
168 res <- initial1
174 res <- initial1
169 return $ first (Tuple' Nothing) res
175 return $ first (Tuple' Nothing) res
170− else return $ FL.Partial $ Tuple' (Just buf1) r1
176+ else return $ FL.Partial $ Tuple' (Just buf'') r1
171
177
172 -- XXX Several folds do extract >=> final, therefore, we need to make final
178 -- XXX Several folds do extract >=> final, therefore, we need to make final
173 -- return "m b" rather than using extract post it if we want extract to be
179 -- return "m b" rather than using extract post it if we want extract to be
@@ -232,8 +238,12 @@ compactLEParserD n = ParserD.Parser step initial extract
232 in if len > nBytes
238 in if len > nBytes
233 then return $ ParserD.Done 1 buf
239 then return $ ParserD.Done 1 buf
234 else do
240 else do
235− buf1 <- MArray.splice buf arr
241+ buf1 <-
242+ if MArray.byteCapacity buf < nBytes
243+ then liftIO $ MArray.realloc nBytes buf
244+ else return buf
245+ buf2 <- MArray.spliceUnsafe buf1 arr
236− return $ ParserD.Partial 0 (Just buf1)
246+ return $ ParserD.Partial 0 (Just buf2)
237
247
238 extract Nothing = return $ ParserD.Done 0 MArray.nil
248 extract Nothing = return $ ParserD.Done 0 MArray.nil
239 extract (Just buf) = return $ ParserD.Done 0 buf
249 extract (Just buf) = return $ ParserD.Done 0 buf