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

Possible optimization of useAsCString? #515

Closed
Lysxia opened this issue May 16, 2022 · 1 comment · Fixed by #516
Closed

Possible optimization of useAsCString? #515

Lysxia opened this issue May 16, 2022 · 1 comment · Fixed by #516
Milestone

Comments

@Lysxia
Copy link
Contributor

Lysxia commented May 16, 2022

useAsCString uses withForeignPtr, however the pointer is only used for the initial copy. Would lifting the subsequent actions out of that block allow you to use unsafeWithForeignPtr instead?

bytestring/Data/ByteString.hs

Lines 1735 to 1742 in 0f49510

useAsCString :: ByteString -> (CString -> IO a) -> IO a
useAsCString (BS fp l) action =
allocaBytes (l+1) $ \buf ->
-- Cannot use unsafeWithForeignPtr, because action can diverge
withForeignPtr fp $ \p -> do
memcpy buf p l
pokeByteOff buf l (0::Word8)
action (castPtr buf)

allocaBytes (l+1) $ \buf -> do
 unsafeWithForeignPtr fp $ \p -> memcpy buf p l
 pokeByteOff buf l (0::Word8) 
 action (castPtr buf) 
@clyring
Copy link
Member

clyring commented May 17, 2022

It looks that way to me. A pull request would be welcome, I believe.

@Bodigrim Bodigrim added this to the 0.11.4.0 milestone Nov 28, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants