Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Improve performance of Data.ByteString.unlines #478

Closed
Bodigrim opened this issue Jan 30, 2022 · 0 comments · Fixed by #479
Closed

Improve performance of Data.ByteString.unlines #478

Bodigrim opened this issue Jan 30, 2022 · 0 comments · Fixed by #479
Milestone

Comments

@Bodigrim
Copy link
Contributor

Ideally unlines should be implemented in a style similar to intercalate, avoiding concat (which is quite ineffective):

bytestring/Data/ByteString.hs

Lines 1209 to 1227 in 5efd6b5

intercalate :: ByteString -> [ByteString] -> ByteString
intercalate _ [] = mempty
intercalate _ [x] = x -- This branch exists for laziness, not speed
intercalate (BS fSepPtr sepLen) (BS fhPtr hLen : t) =
unsafeCreate totalLen $ \dstPtr0 ->
unsafeWithForeignPtr fSepPtr $ \sepPtr -> do
unsafeWithForeignPtr fhPtr $ \hPtr ->
memcpy dstPtr0 hPtr hLen
let go _ [] = pure ()
go dstPtr (BS fChunkPtr chunkLen : chunks) = do
memcpy dstPtr sepPtr sepLen
let destPtr' = dstPtr `plusPtr` sepLen
unsafeWithForeignPtr fChunkPtr $ \chunkPtr ->
memcpy destPtr' chunkPtr chunkLen
go (destPtr' `plusPtr` chunkLen) chunks
go (dstPtr0 `plusPtr` hLen) t
where
totalLen = List.foldl' (\acc chunk -> acc +! sepLen +! length chunk) hLen t
(+!) = checkedAdd "intercalate"

Originally posted by @Bodigrim in #477 (comment)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants