Regis Portalez
Regis Portalez

Reputation: 4860

Read Memory in SharedArrayBuffer

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?

Upvotes: 3

Views: 812

Answers (1)

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.

Upvotes: 3

Related Questions