From 0c030bb63999117a6b1cb1275245a156313c0e49 Mon Sep 17 00:00:00 2001 From: Matthew Craven Date: Sat, 20 May 2023 05:43:17 -0400 Subject: [PATCH] Builder: Re-use the old buffer after trimming (#581) --- Data/ByteString/Builder/Internal.hs | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) diff --git a/Data/ByteString/Builder/Internal.hs b/Data/ByteString/Builder/Internal.hs index a05beead9..abe0d43b2 100644 --- a/Data/ByteString/Builder/Internal.hs +++ b/Data/ByteString/Builder/Internal.hs @@ -1090,7 +1090,7 @@ buildStepToCIOS (AllocationStrategy nextBuffer bufSize trim) = -- Checking for empty case avoids allocating 'n-1' empty -- buffers for 'n' insertChunkH right after each other. if isEmpty - then fill nextStep buf + then fill nextStep (Buffer fpbuf (BufferRange pbuf pe)) else do buf' <- nextBuffer (Just (buf, bufSize)) fill nextStep buf' @@ -1101,8 +1101,9 @@ buildStepToCIOS (AllocationStrategy nextBuffer bufSize trim) = | trim chunkSize size = do bs <- S.createFp chunkSize $ \fpbuf' -> S.memcpyFp fpbuf' fpbuf chunkSize - -- FIXME: We could reuse the trimmed buffer here. - return $ Yield1 bs (mkCIOS False) + -- Instead of allocating a new buffer after trimming, + -- we re-use the old buffer and consider it empty. + return $ Yield1 bs (mkCIOS True) | otherwise = return $ Yield1 (S.BS fpbuf chunkSize) (mkCIOS False) where