Vector3 backed by ArrayBuffer?

Use case: I want to be able to access the player’s position from a Web Worker. Ideally, I would use this for picking, but also maybe for some ticks in an ECS

Rather than copy Vector3 objects frequently, would it be possible for the x, y, and z values to come from an ArrayBuffer? If I just subclass Vector3 and swap out _x, _y, and _z, and assign the new Vector3 subclass to the meshes, will it work or will there be weird bugs?

I want to use SharedArrayBuffer when possible

Excellent question! We are currently thinking about it as well. We already moved Vector structure to a get/set approach as a first step

I think this should work but so far the performance of backing the data inside an array where really bad so we stopped our investigations

Ah that makes sense

Do you remember if each Vector3 backed by an array was its own independent ArrayBuffer or was it one big pooled ArrayBuffer and each Float32Array a length / offset of that single ArrayBuffer? I’ve read fewer, larger ArrayBuffers are faster since you allocate fewer objects, but it probably won’t make the getters any faster

That would also be potentially a good optimization for Vector3 objects since it would make it easier to recycle them if you have thousands of them instead of creating new ones each time

We did the test with one single big array but both read and write where really slower than pure local properties

Darn

I wonder if calls between JS and WASM are fast enough to use a wasm-based vec3 implementation via
https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/SharedArrayBuffer/Planned_changes#WebAssembly_Shared_Memory

We also initiated that without a lot of success: