Download Reference Manual
The Developer's Library for D
About Wiki Forums Source Search Contact

Weak and soft references

Moderators: kris

Posted: 04/04/09 21:29:36

Hello!

It would be great if there was a way to create weak and soft references with Tango. A weak reference is an object-reference that is not taken into account during garbage-collection, and that is set to "null" when the referenced-object is deleted. A soft-reference is the same thing, except the reference is not set to "null" on object-deletion.

These two mechanisms are sometimes necessary if you want to prevent the garbage-collection from not collecting some allocated objects in some cases. For example, here is a code-sample that would require soft-references in order to work properly:

class Window {
   static LinkedList!(SoftRef!(Window)) s_allWindows;
   
   this() {
      s_allWindows.append(this);
   }
   
   ~this() {
      s_allWindows.remove(this);
   }
   
   static void showAll() {
      foreach (window; s_allWindows)
         window.show();
   }
}

Here, without soft-references, the windows would always be referenced by the static-list and thus would never been garbage-collected.

For the implementation of the weak/soft-reference mechanism, I think it could be based on Bill Baxter's implementation that can be found here: http://www.dsource.org/projects/scrapple/browser/trunk/weakref/weakref.d. It just needs to be fixed by xor'ing the pointer-address by a constant-mask in order to hide the reference from the garbage-collector.

I really think weak and soft references need to be implemented in Tango. Lots of cases need them in order to make the garbage-collection work properly.

Author Message

Posted: 04/05/09 13:04:22

I assume you mean that soft references are not supported, since weak references are. Bill's class just makes it more easy to use. Apart from that, I agree that it may be time to consider it more thouroughly for inclusion in Tango.

Posted: 04/05/09 13:24:26

Sorry I didn't know that weak-references were supported. Could you tell me how I can create a weak-reference with Tango please?

Posted: 04/05/09 13:38:30

See the version (Tango) part in Bill's weak reference class.

Posted: 04/05/09 13:58:47

rt_attachDisposeEvent() just lets you attach a delegate to call when an object is disposed. That is not enough to claim for weak-reference support. We also need to be able to hide this reference from the garbage-collector (which is not done currently by Bill's implementation), otherwise the object will actually never get disposed. This can be achieved by xor'ing the pointer to the object with a constant mask for example.

Posted: 04/05/09 14:32:17

Ah, there is a small detail about the GC you don't know (I almost forgot about it myself). Basic types such as size_t are not scanned (originally implemented as an optimization), and thus Bill's weakref class works perfectly well (try the unittest).

Posted: 05/05/09 05:16:21

Is that safe? It recalls me that according to the reference, the behavior of casting a pointer to a non-pointer type is undefined, maybe copying collector will change the address of an object...

Posted: 05/05/09 16:10:27

Eh, no, such casting is not undefined.

Copying/moving collectors are not supported as tings are now (compiler and library support).

Posted: 05/05/09 16:48:37

uhm....may this limitation (not supporting copying/moving collectors) suppress the possibility to optimize performance, or bring in incompatibility with other libs?

Posted: 05/05/09 20:06:03

Not really sure what you ask about - copying/moving collectors is about optimizing certain aspects of the memory management, but it has a cost is such that you'd rather want that type of collector for stability (avoiding stop-the-world collections etc) purposes and not to speed up your application overall.

As for incompatibilities; no idea what this means - it certainly isn't a Tango specific issue - we would love to support such collectors if possible (needs notable compiler support for most interesting features).

Posted: 05/06/09 03:10:07

some particular curcumstance, less page faults, faster allocation,etc (but gc time generally dominates :) )

what if a 3-party lib assumes moving memory is legal and manages its memory by that way ? is this possible?

Posted: 05/07/09 17:30:40

Moving memory would be a problem with a _different_ GC, not the current one (if I understand your question). In any case, an application is not required to allocate its memory from the GC if it thinks that that is a good idea. For some types of apps (e.g. games) this is a good idea, since it typically needs to allocate a lot up front for graphics, and that memory should in any case not be scanned by the GC.

Posted: 09/15/10 12:59:04

Tango has had weak references in tango.core.WeakRef? for some time now.

The version in scrapple should work well. You don't need the xor thing because the object is marked as NO_SCAN - there are no pointers in it. But there are has race conditions where you could get references to dead objects.

Not sure what you mean with soft references. Your example in the first post doesn't work. ~this is called asynchronous, and you'll get race conditions.