-
Notifications
You must be signed in to change notification settings - Fork 40
Description
This is a slightly speculative issue but I thought it worth raising - do you have any plans to support transferable objects?
For anyone not familiar: transferable objects allow you to transfer an ArrayBuffer
(and also a CanvasProxy
or MessagePort
, whatever they are!) to and from a web worker - rather than using the structured cloning algorithm, the reference to a transferable object is 'neutered' once it's transferred - the chunk of memory actually changes owner.
I'm using operative in a project that deals with large amounts of binary data. I've implemented support for transferables in my local copy of the library, and it's blazing fast. If it's useful I can tidy up the code and share it when I get a chance (it's probably not PR-worthy yet).
This is a heavily condensed illustration of how I'm using it:
projectGeometry = operative( function ( arrayBuffer ) {
var d = this.deferred();
float32Array = doSomeComplexMathsAndReturnATypedArray( arrayBuffer );
d.transfer( float32Array.buffer );
}, [ 'poly2tri.min.js' ]);
// `buffer` comes straight from an xhr with responseType: 'arraybuffer'
projectGeometry.transfer( buffer ).then( function ( resultBuffer ) {
var typedArray = new Float32Array( resultBuffer );
doSomethingWith( typedArray );
});
An alternative API is to specify the data and the transfer list separately (which is how it works under the hood with postMessage
:
w = operative( function ( foo, typedArray, anotherTypedArray ) {
var d = this.deferred();
/* some code happens... */
d.transfer({ aThing: 42, anotherThing: myTypedArray }, [
// list of objects to transfer from worker
myTypedArray.buffer
]);
});
w.transfer( foo, typedArray, anotherTypedArray, [
// list of objects to transfer to worker
typedArray.buffer,
anotherTypedArray.buffer
]).then( handleResult );
It's fairly easy to detect support for transferables (you just try transferring an array buffer and see if the byteLength
falls to zero - article), and falling back to structured cloning works just fine.