[MLton] Re: [MLton-commit] r6608
Matthew Fluet
fluet at tti-c.org
Fri Apr 25 18:00:54 PDT 2008
On Thu, 24 Apr 2008, Vesa Karvonen wrote:
> Loop over lists (of declarations) rather than vectors (of ...) in the
> translation pass, because this allows the elements to be GC'ed, which
> reduces the memory requirements dramatically (verified by monitoring
> process memory usage).
>
> ----------------------------------------------------------------------
>
> U mlton/trunk/mlton/defunctorize/defunctorize.fun
>
> ----------------------------------------------------------------------
>
> Modified: mlton/trunk/mlton/defunctorize/defunctorize.fun
> ===================================================================
> --- mlton/trunk/mlton/defunctorize/defunctorize.fun 2008-04-23 00:23:08 UTC (rev 6607)
> +++ mlton/trunk/mlton/defunctorize/defunctorize.fun 2008-04-24 14:27:24 UTC (rev 6608)
> @@ -912,7 +912,10 @@
> end
> end
> and loopDecs (ds: Cdec.t vector, (e: Xexp.t, t: Xtype.t)): Xexp.t =
> - Vector.foldr (ds, e, fn (d, e) => loopDec (d, e, t))
> + loopDecsList (Vector.toList ds, (e, t))
> + (* Convert vector->list to allow processed Cdecs to be GC'ed. *)
> + and loopDecsList (ds: Cdec.t list, (e: Xexp.t, t: Xtype.t)): Xexp.t =
> + List.foldr (ds, e, fn (d, e) => loopDec (d, e, t))
> and loopExp (e: Cexp.t): Xexp.t * Xtype.t =
> let
> val (n, ty) = Cexp.dest e
It's an interesting observation, with a relatively simple fix. We
generally prefer vectors to lists, because vectors are more space
efficient. But, as observed, when mapping or folding over a vector, the
vector will remain live until the end of the map/fold.
There may be other places where breaking a vector into a list before
processing would allow elements to be GCed early. On the other hand, I
suspect that it is particularly bad with the defunctorize pass because the
top-level representation of a CoreML program is:
datatype t = T of {decs: Dec.t vector}
So, we would have retained the entire CoreML program while we built up the
entire XML program. (Worse than that, actually, as the XML program is
build up with the Xml.DirectExp functions, which do things in
continuation passing style, so we build up a lot of closures (presumable
larger than the XML program they denote).)
More information about the MLton
mailing list