javascriptweb-workersharedarraybuffer

Is passing Int32Array instead of SharedArrayBuffer supposed to work?


I have some code that creates a shared array buffer and passes the Int32Array and NOT the SharedArrayBuffer to the worker:

let worker = new Worker(...);
worker.postMessage({
  position: {
    x: createArray(),
    y: createArray()
  },
  velocity: {
    x: createArray(),
    y: createArray()
  }
});

function createArray(size = 1024) {
  let buffer = new SharedArrayBuffer(size);
  return new Int32Array(buffer);
}

In the worker I'm updating the resulting integers in a loop across the array.

let components = e.data;

let velocityX = Atomics.load(components.velocity.x, shipEid);
let velocityY = Atomics.load(components.velocity.y, shipEid);
Atomics.add(components.position.x, shipEid, velocityX);
Atomics.add(components.position.y, shipEid, velocityY);

Now, my question is, is it valid to pass the Int32Array to the worker instead of the SharedArrayBuffer? I honestly didn't even realize I was doing this until much later, and now that I realize it I'm wondering why it works. I thought you had to pass the SharedArrayBuffer to the worker in order to share the memory between the worker and the main process. Is this actually part of the spec to work this way, or is this something Chrome specific that will break for other browsers and possibly break in Chrome in the future?


Solution

  • Yes, this is valid. Notice that you're not actually passing the Int32Array either: you're passing an object which has properties that are objects which have properties that are typed arrays. This works because the argument to postMessage is actually structurally cloned, and when the cloning algorithm reaches the SharedArrayBuffer that is part of your structure then it is cloned into a new buffer object that shares the underlying memory.