Skip to content

Commit

Permalink
Strict stimes now calls memcpy log n instead of n times
Browse files Browse the repository at this point in the history
  • Loading branch information
elikoga committed Oct 10, 2020
1 parent b5fa252 commit 6941dac
Showing 1 changed file with 12 additions and 8 deletions.
20 changes: 12 additions & 8 deletions Data/ByteString/Internal.hs
Original file line number Diff line number Diff line change
Expand Up @@ -652,15 +652,19 @@ concat = \bss0 -> goLen0 bss0 bss0
-- | Repeats given ByteString n times. More efficient than default definition.
times :: Integral a => a -> ByteString -> ByteString
times n (BS fp len) =
unsafeCreate (len * (fromIntegral n)) $ \destptr ->
withForeignPtr fp $ \p ->
go p destptr (fromIntegral n)
unsafeCreate size $ \destptr ->
withForeignPtr fp $ \p -> do
memcpy p destptr len
fill_from destptr len
where
go :: Ptr Word8 -> Ptr Word8 -> Int -> IO ()
go _ _ 0 = return ()
go fromptr destptr i = do
memcpy fromptr destptr len
go fromptr (destptr `plusPtr` len) (i-1)
size = len * (fromIntegral n)

fill_from :: Ptr Word8 -> Int -> IO ()
fill_from destptr copied
| 2 * copied < size = do
memcpy destptr (destptr `plusPtr` copied) copied
fill_from destptr (copied * 2)
| otherwise = memcpy destptr (destptr `plusPtr` copied) (size - copied)

-- | Add two non-negative numbers. Errors out on overflow.
checkedAdd :: String -> Int -> Int -> Int
Expand Down

0 comments on commit 6941dac

Please sign in to comment.