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