-
Notifications
You must be signed in to change notification settings - Fork 1.6k
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
Pass typed data between isolates using transferrables. #29480
Comments
Shouldn't this work to some degree if |
@zoechi We use the package https://pub.dartlang.org/packages/isolate |
Using |
Make a workaround through native extension is expensive. |
benchmark from @ilyaklyaus import 'dart:math';
import 'dart:convert';
import 'package:isolate/isolate.dart';
main(List<String> args) async {
IsolateRunner r = await IsolateRunner.spawn();
String str = generateString();
print("${str.length} bytes");
//decode
Stopwatch s = new Stopwatch()..start();
decode(str);
print("Decoded in ${s.elapsedMilliseconds}ms");
//decode in isolate
s = new Stopwatch()..start();
Map map = await r.run(decode, str);
print("Decoded in isolate in ${s.elapsedMilliseconds}ms");
//pass map to isolate and get back
s = new Stopwatch()..start();
await r.run(passObj, map);
print("Map obj passed in ${s.elapsedMilliseconds}ms");
//pass map to isolate and get back
s = new Stopwatch()..start();
await r.run(passMap, map);
print("Map passed in ${s.elapsedMilliseconds}ms");
//pass str to isolate and get back
s = new Stopwatch()..start();
await r.run(passObj, str);
print("String obj passed in ${s.elapsedMilliseconds}ms");
//pass empty map to isolate and get back
s = new Stopwatch()..start();
await r.run(passObj, {});
print("Empty map obj passed in ${s.elapsedMilliseconds}ms");
//pass empty map to isolate and get back
s = new Stopwatch()..start();
await r.run(passMap, {});
print("EMpty map passed in ${s.elapsedMilliseconds}ms");
}
Map decode(String str) {
return JSON.decode(str);
}
dynamic passObj(dynamic map) {
return map;
}
Map passMap(Map map) {
return map;
}
String generateString() {
Random rnd = new Random();
Map map = new Map();
for (int i=0; i<500000; i++) {
map[i.toString()] = [rnd.nextInt(1000), _randomString(10)];
}
return JSON.encode(map);
}
String _randomString(int length) {
var rand = new Random();
var codeUnits = new List.generate(
length,
(index){
return rand.nextInt(33)+89;
}
);
return new String.fromCharCodes(codeUnits);
} |
In our project we have to decode maps from strings which size is pretty big (up to 20mb) and JSON.decode is slow in this case. Passing such strings to isolate to decode and get it back is even slower because the data is copied.
Could you please consider the way to use transferrables to pass data between isolates. Something like that: https://developer.mozilla.org/en-US/docs/Web/API/Web_Workers_API/Using_web_workers#Passing_data_by_transferring_ownership_(transferable_objects)
Thanks
The text was updated successfully, but these errors were encountered: