-
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
Support Dart_TypedDataAcquireData of Unmodifiable TypedData classes #42785
Comments
Since the embedder is creating External TypedData objects it should have direct access to the memory region, why does it need to call Dart_TypedDataAcquireData to access underlying buffer. |
With the existing API, it is possible to extract the underlying buffer with Dart_GetField and pass that to Dart_TypedDataAcquireData. Though it does seem reasonable to expect Dart_TypedDataAcquireData to handle all the UnmodifiableX that are defined in dart:typed_data. |
The embedder can generate an external typed data object, and return it to Dart code, then that object can come back into the embedder through a Dart language API. A slightly contrived example would be the return from
Yeah, it looks like a |
@mkustermann What do you think about giving the unmodifiable views fixed CIDs like the regular typed data views? |
Right now I can discourage the usage of the unmodifiable views for the following reason: The canonical representation of bytes in Dart code is Optimized access: Our AOT compiler does perform those optimizations - accessing 3rd party implementations: The Short term option: We can turn those unmodifiable view classes to be VM-supported and use our unified typed data layout. That would allow us to have fast read access. BUT: The class would no longer be tree shakable and the Longer term option: I have talked to members of the language/library team about this over the last few months and suggested we could solve it via: a) Introduce a new => If we do the optimizations mentioned in the short term option above and take the performance hit for write-access we could go ahead. @sstrickl or @askeksa-google could take a look at this. (**) Our internal implementations are |
We did just introduce an ImmutableBuffer class for dart:ui. Perhaps that would help here? |
Thanks for looking into this @mkustermann! @dnfield Currently, the |
It would probably help to understand the use cases we'd want to support with this. If the goal is to avoid unnecessary copies, we'd want to know what we're eventually copying to and from - for example, if it's a file or a socket, users may not ever need to get access to the bytes directly in Dart, right? If they need to do comparisons, we could probably have interfaces that boil down to doing a But if Dart could handle |
Another use case is in flutter/engine#21153: we currently have to copy the memory mapped bytes of all assets (potentially quite large!) in order to pass them back through the engine. If we could directly convert them to read only buffers, we'd be able to avoid tons of work |
To speed up the discussion on this, I've written a proposal (see go/performant-typeddata-in-dart). If we follow this path we would ensure to have always fast access to bytes while safely allowing immutable bytes as well as sharing of such across isolates. |
These types now work with Dart_TypedDataAcquireData. The presence of these types no longer degrades the performance of typed data indexed loads. The presence of these types degrades the performance of typed data indexed stores much less. The performance of indexed stores is somewhat regressed if these types were not used. TEST=ci Bug: #32028 Bug: #40924 Bug: #42785 Change-Id: I05ac5c9543f6f61ac37533b9efe511254778caed Reviewed-on: https://dart-review.googlesource.com/c/sdk/+/253700 Reviewed-by: Aske Simon Christensen <askesc@google.com> Reviewed-by: Martin Kustermann <kustermann@google.com> Commit-Queue: Ryan Macnak <rmacnak@google.com>
This reverts commit d1112d3. Reason for revert: b/242043014 Original change's description: > [vm] Recognize unmodifiabled typed data views. > > These types now work with Dart_TypedDataAcquireData. > > The presence of these types no longer degrades the performance of typed data indexed loads. > > The presence of these types degrades the performance of typed data indexed stores much less. The performance of indexed stores is somewhat regressed if these types were not used. > > TEST=ci > Bug: #32028 > Bug: #40924 > Bug: #42785 > Change-Id: I05ac5c9543f6f61ac37533b9efe511254778caed > Reviewed-on: https://dart-review.googlesource.com/c/sdk/+/253700 > Reviewed-by: Aske Simon Christensen <askesc@google.com> > Reviewed-by: Martin Kustermann <kustermann@google.com> > Commit-Queue: Ryan Macnak <rmacnak@google.com> TBR=kustermann@google.com,rmacnak@google.com,askesc@google.com TEST=ci Change-Id: I32c1c460fc30c51bc0d42e7cfaafe72bf5630069 No-Presubmit: true No-Tree-Checks: true No-Try: true Bug: #32028 Bug: #40924 Bug: #42785 Reviewed-on: https://dart-review.googlesource.com/c/sdk/+/254560 Reviewed-by: Siva Annamalai <asiva@google.com> Reviewed-by: Ryan Macnak <rmacnak@google.com> Commit-Queue: Ryan Macnak <rmacnak@google.com>
These types now work with Dart_TypedDataAcquireData. The presence of these types no longer degrades the performance of typed data indexed loads. The presence of these types degrades the performance of typed data indexed stores much less. The performance of indexed stores is somewhat regressed if these types were not used. TEST=ci Bug: #32028 Bug: #40924 Bug: #42785 Change-Id: Iffad865708501acf96db418985cd5a69bd9afa55 Reviewed-on: https://dart-review.googlesource.com/c/sdk/+/254501 Reviewed-by: Martin Kustermann <kustermann@google.com> Commit-Queue: Ryan Macnak <rmacnak@google.com>
It is useful for embedders to be able to expose read-only memory regions to Dart code as external type data. This will let Flutter avoid expensive copies on the Dart thread of large amounts of image data, for example see here:
https://github.com/flutter/engine/blob/18fee597ab6ec957e6311ba2bc74d4732ad55bd7/lib/ui/painting/image_encoding.cc#L48
for
Image.toByteData
:https://github.com/flutter/engine/blob/18fee597ab6ec957e6311ba2bc74d4732ad55bd7/lib/ui/painting.dart#L1610
(The caller of that API could then do a copy to a writable buffer if they need one.)
The best match for this in the Dart SDK appears to be the unmodifiable typed data view classes like UnmodifiableByteDataView. Unfortunately, when these buffers come back into the embedder for various reasons, a call to
Dart_TypedDataAcquireData()
will fail by returning an error because the Unmodifiable typed data classes don't pass the tests here:sdk/runtime/vm/dart_api_impl.cc
Line 4065 in f66eb72
This issue is a feature request to integrate the Unmodifiable typed data classes more seamlessly with the VM so that they're a good match for the read-only buffers we can expose from Flutter as an optimization. If there's another approach that would solve the problem, then I'm interested in that, too.
/cc @a-siva @rmacnak-google @dnfield
The text was updated successfully, but these errors were encountered: