google / neuroglancer

WebGL-based viewer for volumetric data

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

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

jbms opened this issue · comments

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.