[MLton] globals and GC

Matthew Fluet fluet at tti-c.org
Mon Mar 5 08:07:17 PST 2007


> I'm trying to understand how globals work in MLton. 

O.k.

 > Do I understand
> correctly that mutable data may be allocated into globals? 

Yes.  It is possible to have global a global ref.  It is also possible 
to have a global array, which is technically in immutable global, 
pointing to a mutable object.

 > I see that
> in backend.fun, constants are put in globals, and also all the
> variables of the main function--looks like earlier passes hoist global
> refs into the main function. Is that right?

Correct.  The ssa/constant-propagation.fun pass does the main lifting of 
globals.  In the SSA and SSA2 ILs, globals are simply variables that are 
in scope in every function.  In the later backend stages, we create a 
special (main) function to evaluate all of the globals (because later 
globals can refer to earlier ones).

> Why are the constants reallocated and copied into the heap on startup?
> Would it not be possible to keep them in a separate area which is not
> traced?

Perhaps you are referring to the string and IntInf constants.  I believe 
that it was just always felt that keeping all ML objects in the ML heap 
made things easier.  As you noted above, globals can be mutable, so one 
would still need to distinguish between globals pointing to immutable 
constants and globals that have been updated to point to ML objects. 
So, the list of globals is still traced.

Now, it is the case that for space safety, MLton is very careful to not 
globalize large objects.  You may be able to leverage that for the 
realtime guarantee.

> This is another area where special treatment is needed for the
> realtime bound in the Cheng-Blelloch collector--the global roots have
> to be swapped in bounded time to shut down the collector. Their
> solution is to keep two sets of globals and swap a base pointer.

I don't see why that wouldn't work in MLton.




More information about the MLton mailing list