webworkerUtil

A little tool who can be usefull !

Talking to my friend Pascal about webworker and gpujs, we had like a brainstorming.
“It will be nice if we can transform a code without sideeffect on gpu and/or webworker!”

I thinked a bit about it, funny idea…
After it I looked about how is made GPUJS, fastly… It use a library for make an AST of your code, and transform it in a shader.
Very good, but shader have lot of limitation:

  • in input, you can have just constant, and until 3 arrays of 1 or 2 dimensions.
  • avoid “if” instruction as hell, and when you write in JS, it is not the natural way to think.

But that’s still possible: give to GPUJS a code, and if it fail to compile, was not good!

but it need prepare the input parameters…Bit difficult.

But for webworker ? and it is usefull ?

It is very easy in fact !

I did it, it is not perfect, but that’s work :

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
function makeWorker(workerFunction, cb) {
// Create worker
var tplFun = "onmessage = function(e){console.log(e); var args = Array.prototype.slice.call(e.data); var res=___.apply(this,args);postMessage(res);}"
var fnTxt = workerFunction.toString().replace('"use strict";', '');
var final = tplFun.replace("___", fnTxt);
let workerBlob = new Blob([final]);
let workerBlobURL = window.URL.createObjectURL(workerBlob, { type: 'application/javascript; charset=utf-8' });
let worker = new Worker(workerBlobURL);
return function () {
var args = Array.prototype.slice.call(arguments);
console.log(args)
worker.postMessage(args);
worker.onmessage = function (e) {
console.log(e.data);
cb(e.data);
}
}
}

function myTask(a, b, c) {
return a * b * c;
}

function onresult(e) {
alert(e);
}
var fn = makeWorker(myTask, onresult)
fn(1, 2, 3);

What we can improve, and when that’s can be usefull ?
First, in one case,we have a problem : if the function use global variable.

We can detect it by try call comand in a try catch, and error will appear in such case.

More difficult: if it use gloabl variable BUT don’t change them… need an AST tool in such case.

We can also make it a “promise” in easy way.

Another bonus: we can make it webworkker AND memoize ( just a strange word for say: “if this function is pure, same arguments, will give same result, let’s do a cache !!!”)

but more !

As I say we can make them look like a promise…with a big benefits on real promise !

Let’s look to the API Promise in javascript :
Most of the time, just await and async are enough, but not in all case as :
mdn doc on Promise.any

In such case, we continu execution if ONE function is finish….but the other continue !
Let’s imagine a sitation as:

  • try get a data on indexedDB (fast,but not sure)
  • or get by AJAX to cdn1
  • same cdn2
  • take it a complicate and slow way

With promise.any …. yes that’s work, you have your data as soon as possible but the other stuff continue !!!
As webworker have a “terminate()” function, we don’t have this problem.

Thank you for reading ! I will try post on such subject soon !