Fwd: Garbage collection

Facundo Domínguez facundo.dominguez at tweag.io
Tue Nov 18 00:43:58 UTC 2014


Hello,
   While working in the StaticPointers language extension [1], we
found we have some unusual CAFs which can be accessed after some
periods of time where there is no reference to them.

   For instance, the following program when compiled contains no
reference to `g`. `g` is actually looked up at runtime in symbol
tables via the call to `deRefStaticPtr`.

g :: String
g = "hello"

main =
  deRefStaticPtr (static g) >>= putStrLn

Desugars to:

g :: String
g = "hello"

main =
  deRefStaticPtr (StaticPtr (StaticName "" "Main" "g")) >>= putStrLn

In principle, there is nothing stopping the garbage collector from
reclaiming the closure of `g` before it is dynamically looked up.

We are considering using StablePtrs to preserve `g`. So the code
desugars instead to:

g :: String
g = "hello"

main =
  deRefStaticPtr (let x = StaticPtr (StaticName "" "Main" "g")
                         in unsafePerformIO $ newStablePtr g >> return x
                        ) >>= putStrLn

This solution could be temporal though, until we implement the so
called static pointer table, which would keep the values alive.

Would you have any comments about such a solution or maybe would you
advice some other alternative?

Thanks,
Facundo

[1] https://ghc.haskell.org/trac/ghc/wiki/StaticPointers


More information about the ghc-devs mailing list