[mercury-users] Structure reuse

Ralph Becket rafe at cs.mu.OZ.AU
Tue Jul 15 11:01:37 AEST 2003

I believe the heap is checked for sufficient space on each allocation.
If there is insufficient space then garbage collection occurs.  GC
traverses the entire set of live data, so one does not want to do this 
too frequently (e.g. after every few allocations.)  Having garbage lying
around should not affect your program's performance, other than possibly
causing more CPU cache misses.  It's the size of the working set that is
important, not the size of the heap.

Michael Day, Tuesday, 15 July 2003:
> For example, given this predicate for updating a tree:
> update(T0, T) :-
>     update0(T0, T1),
>     update1(T1, T2),
>     ...
>     updateN(TN, T).
> Each of the updateN predicates will traverse the entire tree, modifying
> particular fields to create a new tree. The frequency of garbage
> collection will affect the memory usage of this code.

Have you considered using a store and referencing subtrees via mutvars?
This would mean you only perform allocations for the cells you actually
change, not the entire path from root to changed node.  In fact, the
store module also supplies the generic_ref/2 type which allows one to
update structure fields in place without *any* allocation.

One can't backtrack over these structures, but since you're interested
in destructive update that shouldn't be an issue.

-- Ralph
mercury-users mailing list
post:  mercury-users at cs.mu.oz.au
administrative address: owner-mercury-users at cs.mu.oz.au
unsubscribe: Address: mercury-users-request at cs.mu.oz.au Message: unsubscribe
subscribe:   Address: mercury-users-request at cs.mu.oz.au Message: subscribe

More information about the users mailing list