@@ -111,8 +111,11 @@ packArraysChunksOf n (D.Stream step state) =
111111 then return $
112112 D.Skip (SpliceYielding buf (SpliceBuffering s arr))
113113 else do
114− buf1 <- MArray.splice buf arr
115− return $ D.Skip (SpliceBuffering s buf1)
114+ buf' <- if MArray.byteCapacity buf < n
115+ then liftIO $ MArray.realloc n buf
116+ else return buf
117+ buf'' <- MArray.spliceUnsafe buf' arr
118+ return $ D.Skip (SpliceBuffering s buf'')
116119 D.Skip s -> return $ D.Skip (SpliceBuffering s buf)
117120 D.Stop -> return $ D.Skip (SpliceYielding buf SpliceFinish)
118121
@@ -155,19 +158,22 @@ lpackArraysChunksOf n (Fold step1 initial1 _ final1) =
155158
156159 step (Tuple' (Just buf) r1) arr = do
157160 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
159165
160166 -- XXX this is common in both the equations of step
161167 if len >= n
162168 then do
163− r <- step1 r1 buf1
169+ r <- step1 r1 buf''
164170 case r of
165171 FL.Done _ -> return $ FL.Done ()
166172 FL.Partial s -> do
167173 _ <- final1 s
168174 res <- initial1
169175 return $ first (Tuple' Nothing) res
170− else return $ FL.Partial $ Tuple' (Just buf1) r1
176+ else return $ FL.Partial $ Tuple' (Just buf'') r1
171177
172178 -- XXX Several folds do extract >=> final, therefore, we need to make final
173179 -- 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
232238 in if len > nBytes
233239 then return $ ParserD.Done 1 buf
234240 else do
235− buf1 <- MArray.splice buf arr
236− return $ ParserD.Partial 0 (Just buf1)
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
246+ return $ ParserD.Partial 0 (Just buf2)
237247
238248 extract Nothing = return $ ParserD.Done 0 MArray.nil
239249 extract (Just buf) = return $ ParserD.Done 0 buf