Skip to content

Commit

Permalink
Builder: Re-use the old buffer after trimming (haskell#581)
Browse files Browse the repository at this point in the history
  • Loading branch information
clyring authored and Bodigrim committed May 28, 2023
1 parent 346ad3c commit 0c030bb
Showing 1 changed file with 4 additions and 3 deletions.
7 changes: 4 additions & 3 deletions Data/ByteString/Builder/Internal.hs
Original file line number Diff line number Diff line change
Expand Up @@ -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'

Expand All @@ -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
Expand Down

0 comments on commit 0c030bb

Please sign in to comment.