diff --git a/Data/ByteString.hs b/Data/ByteString.hs index 61a249003..1f468bdba 100644 --- a/Data/ByteString.hs +++ b/Data/ByteString.hs @@ -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! @@ -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 @@ -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 #-} + totalLen = List.foldl' (\acc chunk -> acc +! sepLen +! length chunk) hLen t + (+!) = checkedAdd "intercalate" +{-# INLINABLE intercalate #-} -- --------------------------------------------------------------------- -- Indexing ByteStrings