Naming and subexpressions

Simon Marlow simonmar@microsoft.com
Thu, 8 May 2003 10:43:31 +0100


=20
> I have a program, structured approximately like so
>=20
>    main =3D do
>         let stage1 =3D e1...
>         let stage2 =3D e2...stage1...
>         stage3 <- e3...stage2
>         case something of
>                 foo -> output stage1
>                 bar -> output stage2
>                 baz -> output stage3
>=20
> I think this is an improvement from using multiple main functions,
> since it reveals the pipeline structure of the program and the
> modularity of the stages.
>=20
> It appears=B9, however, that naming the result of stage1=20
> prevents it from
> being garbage collected when subsequent stages are produced.  Since
> stage1 is a huge list, this leads to a space leak.

Hard to tell, but from the code above it looks like stage1 would be =
retained because it might need to be output after stage3.  Could you =
perhaps move the output of stage1 up the pipeline?

If you suspect there's a bug and something is being retained when it =
shouldn't be, then send us the code and we'll happily investigate.

> So, is there any way to avoid this?  Do I need to restructure my
> program?  I could conceivably do
>=20
>    main =3D do
>         let stage1 =3D e1...
>         let stage2 =3D e2...(e1...)...
>         :
>=20
> and so on, but an aggressive (but slightly dumb) compiler might
> rediscover the similarity and CSE it.
>=20
> Suggestions more than welcome.
>=20
> -kzm
>=20
> =B9 Memory retainer profiling shows the product being retained by
> SYSTEM, so I *think* this is the right interpretation.

A SYSTEM retainer is usually the stack.

Cheers,
	Simon