Skip to content

Watcher to accept WeakRef to allow watched live Signals to be Garbage Collected #156

@btakita

Description

@btakita

If possible: A computed Signal should be garbage-collectable if nothing live is referencing it for possible future reads, even if it's linked into a broader graph which stays alive (e.g., by reading a state which remains live).

I created rmemo (reactive memo). A signal library. One of it's unique features is that it supports automatic Garbage Collection by using WeakRef.

My frustration with WeakRef is that it is relatively slow to instantiate. I would be preferable to have a single global WeakValueMap to manage the autosubscriber cache. Instead of instantiating a WeakRef for every live signal that has a dependency. An alternative way to manage the state in C/C++ would achieve the same result.

I believe a WeakValueMap would support primitive keys. WeakMap only allows object keys & object or primitive values. Since the key is allocated memory which would be subject to Garbage Collection. But a WeakValueMap is the other way around. Allowing object or primitive keys & object values. This would allow a numeric id for each Signal to be tracked by the WeakValueMap.

There is an implementation library called weak-value-map. It instantiates a WeakRef for each value, removing the performance benefit. See this discussion. There may be a valid use case for a WeakValueMap here.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions