Skip to content

Commit 5c4d236

Browse files
authored
Builder: Re-use the old buffer after trimming (#581)
1 parent 54cd761 commit 5c4d236

File tree

1 file changed

+4
-3
lines changed

1 file changed

+4
-3
lines changed

Data/ByteString/Builder/Internal.hs

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1090,7 +1090,7 @@ buildStepToCIOS (AllocationStrategy nextBuffer bufSize trim) =
10901090
-- Checking for empty case avoids allocating 'n-1' empty
10911091
-- buffers for 'n' insertChunkH right after each other.
10921092
if isEmpty
1093-
then fill nextStep buf
1093+
then fill nextStep (Buffer fpbuf (BufferRange pbuf pe))
10941094
else do buf' <- nextBuffer (Just (buf, bufSize))
10951095
fill nextStep buf'
10961096

@@ -1101,8 +1101,9 @@ buildStepToCIOS (AllocationStrategy nextBuffer bufSize trim) =
11011101
| trim chunkSize size = do
11021102
bs <- S.createFp chunkSize $ \fpbuf' ->
11031103
S.memcpyFp fpbuf' fpbuf chunkSize
1104-
-- FIXME: We could reuse the trimmed buffer here.
1105-
return $ Yield1 bs (mkCIOS False)
1104+
-- Instead of allocating a new buffer after trimming,
1105+
-- we re-use the old buffer and consider it empty.
1106+
return $ Yield1 bs (mkCIOS True)
11061107
| otherwise =
11071108
return $ Yield1 (S.BS fpbuf chunkSize) (mkCIOS False)
11081109
where

0 commit comments

Comments
 (0)