Question says it all. I have a data structure I can't Marshal because of a weak hashtable .. wondering if I can get rid of it :)
A weak array is an array of weak pointers. A weak pointer is a reference on a value that may be garbage collected.
If you use a regular pointer on a value you will prevent its garbage collection until the referee is itself garbage collected. With a weak reference the value may be collected before the referee.
An example of use is a source that feeds data to multiple sinks. If the source holds regular pointers to the sinks, whenever a sink is no longer needed it won't be garbage collected until the source is (which may for example never happen). If the source uses weak references to the sinks, given sinks may be garbage collected before the source.
Another example is hashconsing for a type which uses weak hashtables (which involve weak arrays). Quickly, hashconsing is a way to remember all values of a given type that are created and living in the program. Together with an appropriate value constructor this can ensure maximal sharing of values of that type and allows to implement structural equality on that type as physical equality. In that case if a non-weak hashtable is used, values no longer used by the program would never be garbage collected.
Finally, many people think (wrongly) that weak references are useful to implement caches. Keep a weak ref on a value, if it was garbage collected, reload/recompute the value. This is not a good cache algorithm because a major garbage collection reclaims any value no longer referenced. So your caching algorithm has no predictability or useful property like, for example, size of cache / available memory doesn't exceed a given ratio.
Use a bijective pair of map functions between your data structure and its structurally congruent representation that's compatible with the Marshall module.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With