Discussion:
[dart-misc] WebWorkers, Isolates, and Transferable Objects support status
Alexey Knyazev
2016-11-22 00:16:53 UTC
Permalink
Hi everyone,

While developing WebGL-related stuff, which involves lots of heavy
computations, I was able to evaluate current (as of 1.20.1) status of
Isolate/WebWorker support in Dart.


*Running Native JS WebWorkers from Dart*

It's possible to run native JS WebWorker from Dart code, but several
*dart:html* bindings aren't correct:

- MessagePort.postMessage
<https://api.dartlang.org/stable/1.20.1/dart-html/MessagePort/postMessage.html>
- Window.postMessage
<https://api.dartlang.org/stable/1.20.1/dart-html/Window/postMessage.html>
- Worker.postMessage
<https://api.dartlang.org/stable/1.20.1/dart-html/Worker/postMessage.html>

All those functions should take List<Transferable
<https://developer.mozilla.org/en-US/docs/Web/API/Transferable>> instead of
List<MessagePort>. There're several open issues on GitHub about that.

Same applies to other similar functions (ServiceWorker API and others, btw
some of them are obsolete):

- Client.postMessage
<https://api.dartlang.org/stable/1.20.1/dart-html/Client/postMessage.html>
- CompositorWorker.postMessage
<https://api.dartlang.org/stable/1.20.1/dart-html/CompositorWorker/postMessage.html>
- CompositorWorkerGlobalScope.postMessage
<https://api.dartlang.org/stable/1.20.1/dart-html/CompositorWorkerGlobalScope/postMessage.html>
- CrossOriginServiceWorkerClient.postMessage
<https://api.dartlang.org/stable/1.20.1/dart-html/CrossOriginServiceWorkerClient/postMessage.html>
- DedicatedWorkerGlobalScope.postMessage
<https://api.dartlang.org/stable/1.20.1/dart-html/DedicatedWorkerGlobalScope/postMessage.html>
- ServicePort.postMessage
<https://api.dartlang.org/stable/1.20.1/dart-html/ServicePort/postMessage.html>

Here's an example of both cloning and transferring ByteBuffer to JS-based
Worker and back:

https://dartpad.dartlang.org/75b696ad9c79cf01b8b8bc3a87c262e2

Please, pay attention that buffer.lengthInBytes value remains intact after
buffer cloning, but changes to zero after transferring. It's correct.
In Dartium, buffer's length value remains untouched in all cases (likely a
bug).


*Running dart2js-compiled WebWorker from JS*

No success (at least for me). Calling Worker.postMessage from JS-based host
results in exception in isolate_helper.dart.
Moreover, such scheme isn't very useful, since each compiled dart file
include full dart runtime.


*Dart Isolates*

Usually work as expected, four issues here:

- Isolates-based ByteBuffer cloning round-trip is 10-20% slower, than
Dart host with JS Worker;
- there's no way to "transfer" objects - only cloning available;
- Isolates don't support sending RegExp, Blob, File, FileList, and
ImageData objects, while WebWorkers support them
<https://developer.mozilla.org/en-US/docs/Web/API/Web_Workers_API/Structured_clone_algorithm#Supported_types>
;
- no Isolates support in Dartium.


Most of the aforementioned issues are already on GitHub (some for several
years), so, is there any "roadmap" on Dart's threading/concurrency support?


Regards,
Alexey Knyazev
--
For other discussions, see https://groups.google.com/a/dartlang.org/

For HOWTO questions, visit http://stackoverflow.com/tags/dart

To file a bug report or feature request, go to http://www.dartbug.com/new
---
You received this message because you are subscribed to the Google Groups "Dart Misc" group.
To unsubscribe from this group and stop receiving emails from it, send an email to misc+***@dartlang.org.
Günter Zöchbauer
2016-11-22 12:00:14 UTC
Permalink
I guess this will be tackled when the migration to DDC is done.
Sounds like a lot of Dartium dependencies
Post by Alexey Knyazev
Hi everyone,
While developing WebGL-related stuff, which involves lots of heavy
computations, I was able to evaluate current (as of 1.20.1) status of
Isolate/WebWorker support in Dart.
*Running Native JS WebWorkers from Dart*
It's possible to run native JS WebWorker from Dart code, but several
- MessagePort.postMessage
<https://api.dartlang.org/stable/1.20.1/dart-html/MessagePort/postMessage.html>
- Window.postMessage
<https://api.dartlang.org/stable/1.20.1/dart-html/Window/postMessage.html>
- Worker.postMessage
<https://api.dartlang.org/stable/1.20.1/dart-html/Worker/postMessage.html>
All those functions should take List<Transferable
<https://developer.mozilla.org/en-US/docs/Web/API/Transferable>> instead
of List<MessagePort>. There're several open issues on GitHub about that.
Same applies to other similar functions (ServiceWorker API and others, btw
- Client.postMessage
<https://api.dartlang.org/stable/1.20.1/dart-html/Client/postMessage.html>
- CompositorWorker.postMessage
<https://api.dartlang.org/stable/1.20.1/dart-html/CompositorWorker/postMessage.html>
- CompositorWorkerGlobalScope.postMessage
<https://api.dartlang.org/stable/1.20.1/dart-html/CompositorWorkerGlobalScope/postMessage.html>
- CrossOriginServiceWorkerClient.postMessage
<https://api.dartlang.org/stable/1.20.1/dart-html/CrossOriginServiceWorkerClient/postMessage.html>
- DedicatedWorkerGlobalScope.postMessage
<https://api.dartlang.org/stable/1.20.1/dart-html/DedicatedWorkerGlobalScope/postMessage.html>
- ServicePort.postMessage
<https://api.dartlang.org/stable/1.20.1/dart-html/ServicePort/postMessage.html>
Here's an example of both cloning and transferring ByteBuffer to JS-based
https://dartpad.dartlang.org/75b696ad9c79cf01b8b8bc3a87c262e2
Please, pay attention that buffer.lengthInBytes value remains intact
after buffer cloning, but changes to zero after transferring. It's correct.
In Dartium, buffer's length value remains untouched in all cases (likely a
bug).
*Running dart2js-compiled WebWorker from JS*
No success (at least for me). Calling Worker.postMessage from JS-based
host results in exception in isolate_helper.dart.
Moreover, such scheme isn't very useful, since each compiled dart file
include full dart runtime.
*Dart Isolates*
- Isolates-based ByteBuffer cloning round-trip is 10-20% slower, than
Dart host with JS Worker;
- there's no way to "transfer" objects - only cloning available;
- Isolates don't support sending RegExp, Blob, File, FileList, and
ImageData objects, while WebWorkers support them
<https://developer.mozilla.org/en-US/docs/Web/API/Web_Workers_API/Structured_clone_algorithm#Supported_types>
;
- no Isolates support in Dartium.
Most of the aforementioned issues are already on GitHub (some for several
years), so, is there any "roadmap" on Dart's threading/concurrency support?
Regards,
Alexey Knyazev
--
For other discussions, see https://groups.google.com/a/dartlang.org/

For HOWTO questions, visit http://stackoverflow.com/tags/dart

To file a bug report or feature request, go to http://www.dartbug.com/new
---
You received this message because you are subscribed to the Google Groups "Dart Misc" group.
To unsubscribe from this group and stop receiving emails from it, send an email to misc+***@dartlang.org.
Filipe Morgado
2016-11-22 12:22:36 UTC
Permalink
Post by Günter Zöchbauer
I guess this will be tackled when the migration to DDC is done.
Sounds like a lot of Dartium dependencies
It should be noted that Dart is a general-purpose programming language, not
web-only.
DDC alone cannot "tackle" this problem.

Isolates should work the same way on all targets.
Personally, I wouldn't mind a performance penalty on the web for more power
on other targets (but I know that's just me).

Oh, and Transferables would be quite handy both on the server and mobile.
--
For other discussions, see https://groups.google.com/a/dartlang.org/

For HOWTO questions, visit http://stackoverflow.com/tags/dart

To file a bug report or feature request, go to http://www.dartbug.com/new
---
You received this message because you are subscribed to the Google Groups "Dart Misc" group.
To unsubscribe from this group and stop receiving emails from it, send an email to misc+***@dartlang.org.
Günter Zöchbauer
2016-11-22 13:02:40 UTC
Permalink
Post by Filipe Morgado
Post by Günter Zöchbauer
I guess this will be tackled when the migration to DDC is done.
Sounds like a lot of Dartium dependencies
It should be noted that Dart is a general-purpose programming language,
not web-only.
DDC alone cannot "tackle" this problem.
Isolates should work the same way on all targets.
When Dartium is discontinued as a target the scenario might be much easier
to be solved.
I didn't say it should **only** work with DDC.
DDC is mainly a development tool, therefore DDC only is similar to "not at
all" (except for the use cases where DDC is used to reused JS generated
from Dart in JS projects).
Post by Filipe Morgado
Personally, I wouldn't mind a performance penalty on the web for more
power on other targets (but I know that's just me).
Oh, and Transferables would be quite handy both on the server and mobile.
--
For other discussions, see https://groups.google.com/a/dartlang.org/

For HOWTO questions, visit http://stackoverflow.com/tags/dart

To file a bug report or feature request, go to http://www.dartbug.com/new
---
You received this message because you are subscribed to the Google Groups "Dart Misc" group.
To unsubscribe from this group and stop receiving emails from it, send an email to misc+***@dartlang.org.
Loading...