Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Comlink for Long Running Workers? #400

Closed
torch2424 opened this issue Nov 14, 2019 · 3 comments
Closed

Comlink for Long Running Workers? #400

torch2424 opened this issue Nov 14, 2019 · 3 comments

Comments

@torch2424
Copy link
Contributor

Yo @surma ! I'm following up from our chat at CDS with some of the ideas / thoughts I had for Comllink 😄

So just as a text explanation: what about especially performance-constrained applications? In this case, rather than chunking off one of commands, re-instantiating the worker every time, does it make more sense to one long-running worker? I know you suggested you could have a worker that uses callbacks to run a command and return a response everytime. But to give some context, in WasmBoy I explicitly need to ensure the data is a transferrable or else the transfer will not be fast enough to achieve the performance gains from workerizing, or even hit 60fps in some cases, which I know from @JamesLMilner 's amazing Web Worker Benchmark Article. Thus I am not entirely sure if that is how the callback system works?

Let me know what you think! 😄

@surma
Copy link
Collaborator

surma commented Nov 15, 2019

Hm, I'm not sure I follow. I always advocate for long-running workers, especially when Comlink is in use :D

Regarding your callback + transferable question: That should work fine!

// main.js
const api = Comlink.wrap(worker);
api.addStateChangeListener(Comlink.proxy(state => {
  // ...
}));
// worker.js
const cbs = [];
Comlink.expose({
  addStateChangeListener(cb) {
    cbs.push(cb);
  }
});

// Transfer some ArrayBuffer every 16ms or something.
setInterval(() => {
  for(const cb of cbs) {
    const val = new Uint8Array([1,2,3]);
    cb(Comlink.transfer(val, [val.buffer]));
  }
}, 16);

Does that make sense/help?

@torch2424
Copy link
Contributor Author

@surma

Hm, I'm not sure I follow. I always advocate for long-running workers, especially when Comlink is in use :D

Oh dude, please excuse me, for some reason when I incorrectly had this though, I though Comlink worked like greenlet. 😂 It was like one of those thoughts you have while walking, and it's been in the back of my head, but yeah that totally is wrong my bad haha!

Regarding your callback + transferable question: That should work fine!

Oh this is perfect! I really need to read the docs haha! 😂

Please excuse this issue, and I went ahead and opened this issue on WasmBoy, because I think it'd be a really cool use case for that 😄

Thanks again!

@surma
Copy link
Collaborator

surma commented Nov 16, 2019

No worries! Let me know if you run into any DX issues :)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants