Questions about sharing

Adrian Hey [email protected]
Sat, 8 Dec 2001 00:32:29 +0000


On Friday 07 December 2001  2:27 pm, D. Tweed wrote:
> On Fri, 7 Dec 2001, Adrian Hey wrote:
> > The first is..
> > Does the compiler keep a unique copy of expressions which consist of just
> > a single zero arity constructor (eg. [],True,Nothing..) as a CAF which is
> > referenced each time the constructor appears in an expression, or does it
> > duplicate the constructor (expression) each time it's used.
> > Maybe I should define my own CAF at the top level and use it instead?
> > (or perhaps they're unboxed somehow?)
>
> Really idle curiosity... why would having a single copy of a zero arity
> constructor be more efficient than have multiple copies? Wouldn't they fit
> into a `cell' which wouldn't be larger than the one that would
> (IIRC) be used for the indirection to the CAF? (I can understand a larger
> CAF being a win, but one this small?)

Well I suppose if it's necessary to create a new indirection heap record
for each reference, then there's not really any point in having a single
copy of the value itself. But I don't see why that should be so. Even
if it is indirected for some reason it should still be possible to
share the indirection record I think.

Maybe CAF is the wrong word to use here since there's no application
involved. What I mean is.. are zero arity constructors referenced the
same way as a top level constant would be? (Not that I know how
that's done, but I presume there's only 1 copy of top level
constants in memory at any one time.)

I was thinking of data types like binary trees. If the tree was
balanced, and a new copy of a leaf (zero arity empty tree) was
constructed on the heap every time a function returned this value,
then this would double the No. of heap records associated with the
tree. This would waste memory and slow down garbage collection.

I just wanted to make sure I don't need to use any weird
programming style to ensure this doesn't happen.

Regards
-- 
Adrian Hey