ForeignPtr performance

Simon Marlow simonmarhaskell at
Mon Dec 17 10:47:34 EST 2007

Scott Dillard wrote:
> Thanks for the response guys.
> I'm still curious as to the specific mechanism by which a strictly
> compile-time function affects the run-time behavior of the GC. Bulat
> says that touch# makes the runtime aware that the fptr object is still
> in use, but how does it do this if, at runtime, it doesn't do
> anything? Does if affect the scheduling of collections? Does it mark
> the fptr object with some flag when its allocated? What if, for
> example, if I put the touch# behind a branch that is conditional on
> run-time values? How would this affect things?

It's not as complicated as you think.  touch# takes a single argument, and 
does nothing with it; it's only purpose is to ensure that the value passed 
as its argument is "alive" as far as the rest of the compiler and runtime 
is concerned.

There's no special mechanism at work here: touch# is just like an ordinary 
IO operation, except that its implementation is empty, and the compiler 
cannot use this fact for optimisation purposes.  The compiler therefore 
arranges that the argument to touch# is available, just as it would for any 
other function.


More information about the Glasgow-haskell-users mailing list