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

Efficient implementation of stimes #299

Closed
Bodigrim opened this issue Oct 7, 2020 · 1 comment · Fixed by #301
Closed

Efficient implementation of stimes #299

Bodigrim opened this issue Oct 7, 2020 · 1 comment · Fixed by #301
Milestone

Comments

@Bodigrim
Copy link
Contributor

Bodigrim commented Oct 7, 2020

Both strict and lazy bytestrings provide instance Semigroup, but rely on the default definition of Data.Semigroup.stimes. It would be better to provide an optimized definition.

For a strict bytestring, we can immediately allocate all required memory and memcpy in a loop. This is a simple extension of the implementation for (<>):

append (BS fp1 len1) (BS fp2 len2) =
unsafeCreate (len1+len2) $ \destptr1 -> do
let destptr2 = destptr1 `plusPtr` len1
withForeignPtr fp1 $ \p1 -> memcpy destptr1 p1 len1
withForeignPtr fp2 $ \p2 -> memcpy destptr2 p2 len2

For lazy bytestrings no low level manipulations needed, one can write something similar to sconcat:

concat :: [ByteString] -> ByteString
concat css0 = to css0
where
go Empty css = to css
go (Chunk c cs) css = Chunk c (go cs css)
to [] = Empty
to (cs:css) = go cs css

@elikoga
Copy link
Contributor

elikoga commented Oct 9, 2020

Looking into it

@Bodigrim Bodigrim linked a pull request Oct 28, 2020 that will close this issue
@Bodigrim Bodigrim added this to the 0.11.1.0 milestone Nov 19, 2020
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