-
Notifications
You must be signed in to change notification settings - Fork 1.7k
Implement shared native memory multithreading #56841
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
Comments
@leafpetersen @lrhn From my perspective this subset of changes does not really fundamentally change Dart language capabilities (pure Dart code remains isolated) so I would like to move forward, implement and ship this not guarded by any flag so that our interop efforts could rely on it. Any opposition to this plan? |
If you think the feature will be useful as written, then I have no problem with trying try to implement it. I'm not promising to change the implementation of platform libraries to be able to run without access to non-constant static state, or to make all such fields shared (which likely isn't viable anyway). That means that an unknown-size part of the platform libraries cannot be used in a shared isolate. I do fear that the that part might contain some essential APIs. If all the shared isolate code does is computation on input data to produce output data, then maybe that's not a problem. Maybe it's just a matter of managing expectations. |
I'm very excited by this feature! I believe it would speed up a lot of code u To visualize what would be the development of code with the shared memory, I wrote this quick sort with the shared fields and shared isolates. Edit : Not correct. See #56841 (comment) /**
* Quick sort
*/
import 'dart:isolate';
import 'dart:math';
import 'dart:typed_data';
Future<void> quickSort(Int64List array, int low, int high) async {
if (low < high) {
int partitionIndex = partition(array, low, high);
await Future.wait(
[
Isolate.runShared(
() => quickSort(array, low, partitionIndex - 1),
),
Isolate.runShared(
() => quickSort(array, partitionIndex + 1, high),
),
],
);
}
}
int partition(Int64List array, int low, int high) {
int pivot = array[high];
int i = (low - 1);
for (var j = low; j < high; j++) {
if (array[j] <= pivot) {
swap(array, ++i, j);
}
}
swap(array, i + 1, high);
return i + 1;
}
void swap(Int64List array, int i, int j) {
final tmp = array[i];
array[i] = array[j];
array[j] = tmp;
}
void main() async {
final random = Random();
final List<int> numbers = List.generate(10000, (i) => random.nextInt(999999));
final shared Int64List toSort = Int64List.fromList(numbers);
Isolate.runShared(() {
quickSort(toSort, 0, numbers.length - 1);
});
} I wonder if the |
I just want to point out a couple of things. First of all, the type of code you have written can already be written without any issues: you can allocate memory for Second, the code as written has a number of issues:
So this code is not really the best example for this feature. |
Thanks you for your explanation. I understand better now |
Hi @mraleph, I’ve been working with isolates and came across an issue — well, not really an issue, but more of a question regarding how Dart handles shared memory. Specifically, I have a socket.io instance that’s spawned by a native block of code, and I was looking for a way to access the same instance across the main isolate. Would using Shared Isolates or a shared static field help in this case? Or is my design approach flawed? The goal is to be able to access the same SocketIo instance across different isolates. For example: shared SocketIo instance = io(); That way, I could access this instance from any isolate. Any advice or guidance on this would be greatly appreciated! |
@venbrinoDev whether or not we will allow sharing arbitrary mutable objects between isolate is still not fully decided. The first step we are planning to make - shared native memory - will not help you, because Here is a question: why do you have two isolates both connecting the server? what is the second isolate doing? If you really need it, then you need to design this in a different way: one isolate creates and maintains the connection and all other isolates need to communicate with that isolate using ports. One isolate becomes kinda like a proxy for a connection. (Unfortunately Dart isolates don't facilitate this sort of thinking because they are designed as a coarse grained fork of the whole program). Footnotes
|
Are there currently any working on this? |
@aam is working on this. |
@mraleph , thanks for your response. Hope to see this feature in Dart soon. |
Allow dart code execution on mutator thread, do not require an isolate. It moves some states that was kept on an isolate to thread or isolate group. Bug: #54530 Bug: #56841 TEST=run_isolate_group_run_test CoreLibraryReviewExempt: only internal library is being updated Change-Id: I99df09e23954755387ea6230bfd166493d78e989 Reviewed-on: https://dart-review.googlesource.com/c/sdk/+/418503 Reviewed-by: Slava Egorov <[email protected]> Reviewed-by: Ryan Macnak <[email protected]> Commit-Queue: Alexander Aprelev <[email protected]>
…ic fields. Sample snapshot size comparison before/after: === dart2js_aot.dart.snapshot before: 19946368 after: 19998800 (with flag turned on) delta: 52432 0.26% === Bug: #54530 Bug: #56841 CoreLibraryReviewExempt: internal library change only Change-Id: I34b1945c040bbad22cb3ec6fdb6e6776df31a82f TEST=run_isolate_group_run_test.dart Reviewed-on: https://dart-review.googlesource.com/c/sdk/+/422360 Reviewed-by: Alexander Markov <[email protected]> Commit-Queue: Alexander Aprelev <[email protected]> Reviewed-by: Slava Egorov <[email protected]>
This method allows for synchronous execution of dart callbacks from native code. The execution happens on dart mutator thread, from which dart code can only access isolate-group variables - those which are tagged with . Bug: #54530 Bug: #56841 Change-Id: Ia1a6b01327be493f003f1eea82e558bb6b147dd3 CoreLibraryReviewExempt: only internal library change TEST=isolate_group_shared_callback_test Reviewed-on: https://dart-review.googlesource.com/c/sdk/+/422920 Reviewed-by: Slava Egorov <[email protected]> Commit-Queue: Alexander Aprelev <[email protected]>
The Shared Memory Multithreading for Dart proposal outlines a number of modifications to the Dart native runtime and core libraries which would unlock interoperability use cases which currently require developers to writing additional native code.
These modifications include:
dart:ffi
:NativeCallable.shared
There might be other changes to core libraries which would help the cause but is not necessarily required:
dart:ffi
.dart:isolate
Isolate.shared
Isolate.postTask
.Isolate.runSync
.dart:concurrent
.AtomicRef
,AtomicInt32
,AtomicInt64
Mutex
andCondition
.These changes do not fundamentally alter Dart's isolate model - pure Dart code running in different isolates remains isolated, but introduce more straightforward way to bridge concurrency chasm between native world (which is structured around threads) and Dart (which is structured around isolates).
The text was updated successfully, but these errors were encountered: