3

In an attempt to parallelize treatment on large TypedArrays, I tried to use the mozilla extension named SharedArrayBuffer.

These objects allow concurrent workers (or main thread) to handle the same memory (which is not made unavailable to caller of postMessage).

Unfortunately, it appears I can't read the memory of those objects I can't use DataViews on them.

I tried the following with DataViews to avoid entire buffer copies:

var arrayBuffer = new SharedArrayBuffer(4); // 4 bytes
var dataView = new DataView(new SharedArrayBuffer(4)); // fails with error below
// some usage here
var first = dataView.getFloat32(0)

which fails with the error:

TypeError: DataView: expected ArrayBuffer, got SharedArrayBuffer

I could read the memory by making a TypedArray back from the buffer, but this would clone the memory, and therefore ruin the performance gain of sharing memory as it appears to have a small overhead, such as suggested by this bug answer but then I have to know the type in advance.

Is there any solution to this?

Regis Portalez
  • 4,675
  • 1
  • 29
  • 41
  • 2
    Lars Hansen’s response at https://bugzilla.mozilla.org/show_bug.cgi?id=1246597#c3 seems useful for posting as an answer or update/edit to this question. – sideshowbarker May 25 '16 at 07:42
  • fixed question. I tested that new Float32Array() has indeed a negligeable overhead – Regis Portalez May 25 '16 at 09:02
  • 1
    bug is fixed in firefox 53 : https://bugzilla.mozilla.org/show_bug.cgi?id=1246597 – Regis Portalez Mar 06 '17 at 13:23
  • @RegisPortalez After a year... wow! –  Sep 26 '17 at 12:17
  • [Note that](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/SharedArrayBuffer) `SharedArrayBuffer` was disabled by default in all major browsers on 5 January, 2018 in response to [Spectre](https://meltdownattack.com/). – Pablo Bianchi Jan 31 '18 at 03:19

1 Answers1

3

I believe the answer is no, at least for now.

There's a related bug on Bugzilla:

The SharedArrayBuffer spec says that DataView is allowed on SharedArrayBuffer, and that SharedArrayBuffer.isView() on a DataView should return true. Neither is the case in Firefox at the moment.