google / google/neuroglancer

Use FinalizationRegistry to manage inter-worker object mappings without reference counting

Open
#512 0 comments 0 reactions 0 assignees View on GitHub
Dominant language
TypeScript
Stars
1.5k
Forks
389
Avg merge
3d 21h
Merged PRs (30d)
3

Description

Currently Neuroglancer relies extensively on manual reference counting for two main reasons:
- Managing Signal callbacks
- Maintaining the mapping between integer ids and SharedObject objects that are kept in sync between the main UI thread and the chunk worker thread.

The need for reference counting to handle Signal callbacks can be mostly avoided by using SolidJS/MobX-style state management (see #511).

For shared objects between the main UI thread and the chunk worker thread, it will likely be possible to use the `FinalizationRegistry` feature that is now available in all supported browsers, and instead rely on the browser's own garbage collection to determine when integer ids can be unregistered. There is a caveat: while currently we have cross-worker reference counting, it is not possible to do true cross-worker garbage collection using `FinalizationRegistry` or `WeakRef`. Instead, one side (main UI thread or chunk worker thread) would need to be the owner of the SharedObject, and as soon as it is garbage collected on that side, it would be destroyed on the opposite side regardless of whether it is still referenced by any other objects. This should be sufficient for Neuroglancer's use cases, though.

This in combination with the use of SolidJS would mean that manual reference counting can be fully, or almost fully, eliminate from Neuroglancer, which would greatly simplify the code and likely eliminate some existing reference counting bugs.

Contributor guide

Open the contributing guide

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.