-
Notifications
You must be signed in to change notification settings - Fork 1.7k
Find a way to do less copying when sending large typed data #34796
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
…through a message port Closes #34752 Issue flutter/flutter#22796 Issue #34796 Change-Id: Ia769ee7d828f37a5ca36fedebcf609c3bab8c38c Reviewed-on: https://dart-review.googlesource.com/c/79825 Commit-Queue: Alexander Markov <[email protected]> Commit-Queue: Ryan Macnak <[email protected]> Reviewed-by: Ryan Macnak <[email protected]> Auto-Submit: Alexander Markov <[email protected]>
The original issue that triggered this optimization is here #31959 |
If Dart isolate support transferable Data. #4149 |
Note that transferrables on the Web are implemented by externalizing typed arrays behind the scenes. This is the kind of implementation we want to avoid, because it introduces the problem that type of the object changes dynamically: you take a normal typed array and send it to another worker - it suddenly becomes an empty typed array on the sender side. |
Maybe we can introduce transferable typed data as separate classes (they could be private in dart:typed_data), created with separate public factory constructors (e.g. Int8List.transferable(int length)). Unlike other typed data classes, transferable could have a mutable length (if needed), or an extra flag indicating that contents was moved to another isolate and should not be accessed anymore. This way it will be possible to avoid both extra copying and dynamic changes of object types. The disadvantage of this approach is that users will need to change their code and use the new API to create transferable typed data. /cc @aam |
Why is this a problem? |
Because right now we can treat type array length as a constant value - and optimize based on that, independent of whether it escape anywhere or not. You are loosing this property if you start changing array length when you send the data over. e.g. right now class Matrix3D {
final storage = new Float32Array(9)
}
Matrix3D smth;
smth.storage[1]; // No bounds check needed. Static guarantees of length |
You could just "0-out" the array, instead of clearing all elements, right? |
Ah, nice! Didn't know you did that.
That's is as bad as copying :) |
FWIW, in AOT mode, it might be reasonable to segfault if you attempt to access a typed array that has been transferred. In JIT mode you could throw an exception? I don't know if that makes sense, but I imagine the number of folks that will be using transferable arrays is rather small, and it is probably OK to give them undefined behavior if you transfer the array and then try to use it. |
That means inserting a check before every access to a typed array. Since typed arrays tend to be used in performance-critical parts of the code, I'd try to avoid adding checks. |
Oh, I mean don't insert a check, and let the program just crash :) |
The thing is, it won't crash. Because the memory is not copied/moved anywhere, it's still there. So the crash is not guaranteed to happen predictably. |
Hmm. Could the pointer to the array be pointed to a null location, or something? 💥 |
That's a @mraleph question. Your isolate could have multiple pointers to the array. You'd need some indirection for that to work, which, again, adds overhead. |
This is the first step towards unification of TypedData and ExternalTypedData. Issue: #34796 Change-Id: I8aec72030e251f9cd9f00fc5073f8e42b83a8d17 Reviewed-on: https://dart-review.googlesource.com/c/sdk/+/79430 Commit-Queue: Alexander Markov <[email protected]> Reviewed-by: Martin Kustermann <[email protected]> Reviewed-by: Ryan Macnak <[email protected]>
This reverts commit d1085e8. Reason for revert: performance regressions. Original change's description: > [vm] Add data field to TypedData > > This is the first step towards unification of TypedData and > ExternalTypedData. > > Issue: #34796 > Change-Id: I8aec72030e251f9cd9f00fc5073f8e42b83a8d17 > Reviewed-on: https://dart-review.googlesource.com/c/sdk/+/79430 > Commit-Queue: Alexander Markov <[email protected]> > Reviewed-by: Martin Kustermann <[email protected]> > Reviewed-by: Ryan Macnak <[email protected]> [email protected],[email protected],[email protected],[email protected],[email protected] # Not skipping CQ checks because original CL landed > 1 day ago. Issue: #34796 Change-Id: Ia11d8c3af0af29d49c3dbf36a3fe7041fbf9aae3 Reviewed-on: https://dart-review.googlesource.com/c/sdk/+/97166 Reviewed-by: Alexander Markov <[email protected]> Commit-Queue: Alexander Markov <[email protected]>
This is the first step towards unification of TypedData and ExternalTypedData. Issue: dart-lang#34796 Change-Id: I8aec72030e251f9cd9f00fc5073f8e42b83a8d17 Reviewed-on: https://dart-review.googlesource.com/c/sdk/+/79430 Commit-Queue: Alexander Markov <[email protected]> Reviewed-by: Martin Kustermann <[email protected]> Reviewed-by: Ryan Macnak <[email protected]>
This reverts commit d1085e8. Reason for revert: performance regressions. Original change's description: > [vm] Add data field to TypedData > > This is the first step towards unification of TypedData and > ExternalTypedData. > > Issue: dart-lang#34796 > Change-Id: I8aec72030e251f9cd9f00fc5073f8e42b83a8d17 > Reviewed-on: https://dart-review.googlesource.com/c/sdk/+/79430 > Commit-Queue: Alexander Markov <[email protected]> > Reviewed-by: Martin Kustermann <[email protected]> > Reviewed-by: Ryan Macnak <[email protected]> [email protected],[email protected],[email protected],[email protected],[email protected] # Not skipping CQ checks because original CL landed > 1 day ago. Issue: dart-lang#34796 Change-Id: Ia11d8c3af0af29d49c3dbf36a3fe7041fbf9aae3 Reviewed-on: https://dart-review.googlesource.com/c/sdk/+/97166 Reviewed-by: Alexander Markov <[email protected]> Commit-Queue: Alexander Markov <[email protected]>
TransferableTypedData was introduced. |
When sending a large typed data through a message port, there is an optimization to convert it to an external typed data (
sdk/runtime/vm/raw_object_snapshot.cc
Lines 1945 to 1961 in f3c51a4
As a workaround to these problems, the optimization of converting typed data to external will be put under a flag and disabled by default. However, we still need to find a way to do less copying when sending large amount of data through message ports, preferably without morphing object types.
The text was updated successfully, but these errors were encountered: