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

intercalate-related cleanup #468

Merged
merged 4 commits into from
Jan 20, 2022
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 5 additions & 5 deletions Data/ByteString.hs
Original file line number Diff line number Diff line change
Expand Up @@ -277,9 +277,7 @@ import GHC.Word hiding (Word8)
-- | /O(1)/ Convert a 'Word8' into a 'ByteString'
singleton :: Word8 -> ByteString
singleton c = unsafeCreate 1 $ \p -> poke p c
{-# INLINE [1] singleton #-}

-- Inline [1] for intercalate rule
{-# INLINE singleton #-}

--
-- XXX The use of unsafePerformIO in allocating functions (unsafeCreate) is critical!
Expand Down Expand Up @@ -1210,6 +1208,7 @@ groupBy k xs = case uncons xs of
-- argument between each element of the list.
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
Expand All @@ -1224,8 +1223,9 @@ intercalate (BS fSepPtr sepLen) (BS fhPtr hLen : t) =
go (destPtr' `plusPtr` chunkLen) chunks
go (dstPtr0 `plusPtr` hLen) t
where
totalLen = List.foldl' (\acc (BS _ chunkLen) -> acc + chunkLen + sepLen) hLen t
{-# INLINE [1] intercalate #-}
clyring marked this conversation as resolved.
Show resolved Hide resolved
totalLen = List.foldl' (\acc chunk -> acc +! sepLen +! length chunk) hLen t
(+!) = checkedAdd "intercalate"
{-# INLINABLE intercalate #-}

-- ---------------------------------------------------------------------
-- Indexing ByteStrings
Expand Down