[gclist] Extensions to weak pointers
17 Jul 1997 14:37:33 +0200
>From what I have read, weak pointers usually have the following
semantics: when an object is referenced only by weak pointers, the
object is garbage-collected and the weak pointers pointing to it are
replaced by a non-pointer value to indicate that they are not valid
anymore. This implies that before using the value of a weak pointer,
the programmer has to check whether it is valid or not.
An extension to that scheme could be to add a "strong" part to each
weak pointer. That is, each "semi-weak" pointer would be composed of
a weak part and a strong part. The weak part would behave mostly like
a weak pointer, that is it would not disable the garbage collection of
the pointed object; The strong part would behave mostly like a normal
pointer, that is it would disable the garbage collection of the
pointed object. Now each time the weak part points to an object
referenced only by weak pointers or weak part of "semi-weak" pointers,
the garbage collector replaces the weak part not by an invalid value
but by the strong part of the "semi-weak" pointer.
For these "semi-weak" pointers to be useful, the weak and the strong
part should point to objects that behave the same way. That is, these
pointers could be used mainly for caching purpose.
For example, one could imagine that the weak part of the pointer
points to an efficient (e.g. memoized) version of some procedure,
while the strong part points to the slow (but small) version of the
Of course, this behaviour could be obtained with weak pointers, but
that would be less efficient, because that would require a test before
each call to the cached version. Moreover, the solution with weak
pointers is more error-prone since the programmer might forget to test
the validity of its pointer before using it.
To extend the use of (semi-)weak pointers for caching purpose further,
a priority could be assigned to each (semi-)weak pointers. This
priority would be used a little like generation number in generational
GCs. That is, the objects with the lowest priority would be GCed
first, and the ones with greater priority would be GCed only when not
enough memory could be obtained from the lower priorities.
(To make things clear, if only weak pointers point to some object,
that object will be GCed *only* if the current garbage collection
has a priority at least equal to that of the object).
That way, a (semi-)weak pointer caching disk sectors (for example)
could be assigned a higher priority than (semi-)weak pointers caching
the values of a function.
I was wondering whether any of these two extensions has ever been
considered, or even implemented in a GC. (Some kind of priority was,
if I remember well, implemented in low-memory handlers in the latest
[and last] version of AmigaOS). I would also be glad to collect your
comments about these suggestions.