Add SharedSymbol

ArrayBuffer has a shared version SharedArrayBuffer.
But Symbol doesn't have a shared version.
Shared symbols are useful in keeping the equality/inequality logic consistent among threads.

There already is Symbol.for(…), which shares symbols globally across threads - by name, without any message passing.
Passing symbols between threads, e.g. via postMessage, should keep their identity, does it not?

On the web symbols can’t be passed to workers as they are (at least currently) not supported in the StructuredClone operation.

1 Like

FYI, preserving the identity of unique symbols through postMessage between agent clusters (iframe of different origins), combined with symbols as WeakMap keys, would require engines to implement distributed garbage collection (or leak). I doubt it'd happen on the web.

Edit: to clarify, I doubt sharing unique symbols between agent clusters would ever be supported. It's entirely possible that symbols could be shared inside a cluster (requiring engines to do GC per cluster, as they somewhat already plan to), and unique symbols could just throw when sent to another cluster (like trying to send a SAB cross origin). And you could even imagine registered symbols being seamlessly re-registered between clusters, as there'd be no way to observe their actual identity between clusters.

1 Like

Oh that's a shame, I had not expected that. Should Symbols be supported in StructuredClone? Β· Issue #1862 Β· whatwg/html Β· GitHub appears to be the relevant issue.

It shouldn't be a problem for symbols in the global symbol registry, using those as WeakMap keys already does mean the values can never get garbage-collected.

1 Like

Then we can disallow using SharedSymbol as WeakMap keys, no problem.

What about cases where symbols don't have associated keys?

Registered symbols are not going to be permitted as weakmap keys.

1 Like

Using Symbol.for across threads requires that all symbols to have associated keys. Symbols generated simply by new Symbol() can't be shared. Requiring all symbols to have keys loses the significance of symbols, it's just like using string to identify things.

I edited my post above to clarify that:

  • registered symbols could be shared between any agents fairly easily.
  • unique symbols could be shared within an agent cluster (like SAB), if the engine switches their GC implementation to be per agent cluster (which from what I understand is on their radar anyway)
1 Like

I had missed your SharedSymbol distinction. I doubt we will see another kind of Symbol in the language at this point (edit: especially one that wouldn't be allowed in weakmap keys). That said, as mentioned above, sharing existing unique symbols through postMessage is a decision entirely within the Web's prerogative, nothing in ECMAScript prevents it.

1 Like

Shared symbols are useful in keeping the equality/inequality logic consistent among threads.

Something worth noting is that there is the shared structs part of the structs proposal, if any form of shared objects like in that proposal landed you would just be able to use those objects rather than needing an indirection through a symbol (and if you needed real symbols for whatever reason, you could just store a WeakMap of shared-object -> symbol).

Apparently both v8 and webkit are working on cross-thread GCs anyway, so (some version) of the proposal seems at least somewhat likely to happen eventually.

1 Like

Thanks for the informative reply. Will be following up the proposal.