[Haskell-cafe] How does the RTS pin ByteArray# objects during FFI calls?

winter drkoster at qq.com
Wed Jan 3 03:31:37 UTC 2018


You’d better use `UnliftedFFITypes` with unsafe FFI calls, since unsafe FFI calls act like fat prim-ops.

GC can happen during safe FFI calls, safe FFI calls are running on other OS threads rather than GHC scheduling threads.

Note: older version of GHCi used to implemented unsafe FFI using safe FFI, there’s an issue about this here: https://ghc.haskell.org/trac/ghc/ticket/8281 <https://ghc.haskell.org/trac/ghc/ticket/8281>


> On 3 Jan 2018, at 10:51 AM, Reiner Pope <reiner.pope at gmail.com> wrote:
> 
> Hi Haskell-Cafe,
> 
> I understand from https://mail.haskell.org/pipermail/haskell-cafe/2014-June/114763.html <https://mail.haskell.org/pipermail/haskell-cafe/2014-June/114763.html> that it is safe to pass ByteArray# objects to FFI calls, using the UnliftedFFITypes language extension. As I understand it, the implicit guarantee is that the RTS will pin the ByteArray#'s address in memory for the duration of the FFI call -- even if the ByteArray# wasn't allocated pinned.
> 
> I'm curious: how does the RTS achieve this "retroactive" pinning? The documentation at https://ghc.haskell.org/trac/ghc/wiki/Commentary/Rts/Storage/GC/Pinned <https://ghc.haskell.org/trac/ghc/wiki/Commentary/Rts/Storage/GC/Pinned> suggests that all ByteArrays are allocated pinned to account for the possibility that they will in future be passed to the FFI. Is this really the case? This seems like it forces an unreasonably slow allocator on small (say, ~10-byte) ByteArray allocations where the GC's usual bump-pointer allocator might otherwise be preferable.
> 
> Regards,
> Reiner
> _______________________________________________
> Haskell-Cafe mailing list
> To (un)subscribe, modify options or view archives go to:
> http://mail.haskell.org/cgi-bin/mailman/listinfo/haskell-cafe
> Only members subscribed via the mailman list are allowed to post.

-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.haskell.org/pipermail/haskell-cafe/attachments/20180103/9aa416d1/attachment.html>


More information about the Haskell-Cafe mailing list