[vm, gc] Add some documentation about weakness.

Change-Id: I10ca59a29e3ac951b3e7830e6b90a36a6ac55a75
Reviewed-on: https://dart-review.googlesource.com/c/sdk/+/311524
Reviewed-by: Alexander Aprelev <aam@google.com>
This commit is contained in:
Ryan Macnak 2023-06-26 23:14:26 +00:00
parent 95bd742546
commit bbeed85932

View file

@ -205,6 +205,22 @@ container <- AllocateObject
StoreInstanceField(container, value, NoBarrier)
```
## Weakness
The GC supports a variety of weak and ephemeral references.
* Heap objects
* WeakReference - Weakly references a single object.
* WeakProperty - Weakly references a key. If the key is reachable, then the value is reachable. Also called an [ephemeron](https://en.wikipedia.org/wiki/Ephemeron). It is used to implement Expando (also called WeakMap).
* WeakArray - Weakly references a variable-number of objects. Used to implement weak canonical sets.
* Finalizers - Weakly references a single object, runs a callback when the object is collected.
* Non heap objects
* Dart\_WeakPersistentHandle / Dart\_FinalizableHandle - Weakly references a single object, runs a callback when the object is collected.
* The object id ring - References objects that were assigned ids by the VM service. The references are strong during a minor GC and weak during a major GC.
* Weak tables - Weakly associate objects to integers. Used to implement Dart_GetPeer, identity hashes on 32-bit architectures, and canonicalization hashes.
As GC traces through strong references, it maintains a set of weaklings encountered. Once the worklist of strong references is empty, the set of WeakProperties is examined for any reachable keys, which add the corresponding values to the worklist. This repeats until the worklist is empty. (This is quadratic in the worst case. Previous versions of the Dart VM used the watched set optimization. In practice, things are flat.)
## Finalizers
The GC is aware of two types of objects for the purposes of running finalizers.