Skip to content

Add ability to trasnsfer TypedData to ffi #60170

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

Closed
MohiuddinM opened this issue Feb 19, 2025 · 6 comments
Closed

Add ability to trasnsfer TypedData to ffi #60170

MohiuddinM opened this issue Feb 19, 2025 · 6 comments
Labels
area-vm Use area-vm for VM related issues, including code coverage, and the AOT and JIT backends. library-ffi triaged Issue has been triaged by sub team type-enhancement A request for a change that isn't a bug

Comments

@MohiuddinM
Copy link

Right now, we have to allocate a buffer using malloc, and then copy TypedData to the buffer element by element. This adds a lot of overhead, and makes the ffi calls slow for larger buffers. So it will be great if we can get a pointer to TypedData, and use that in ffi calls.

We can have a version of malloc that takes a TypedData, instead of size, and then allocates the TypedData buffer in native memory. Then we can add a method to TypedData called .toNative() on top of that.

@lrhn lrhn added area-vm Use area-vm for VM related issues, including code coverage, and the AOT and JIT backends. library-ffi labels Feb 19, 2025
@dcharkes
Copy link
Contributor

For short running FFI calls (isLeaf: true) you can pass the pointer to C directly into the typed data on the Dart heap.

https://api.dart.dev/stable/latest/dart-ffi/Int32ListAddress/address.html

(Leaf calls prevent the Dart GC from running, so the typed data will not move during a leaf call. It blocks the GC, and you cannot call back into Dart in leaf calls.)

If you cannot make it a leaf call and you need to copy you can take advantage of memcopy by using asTypedList on your target pointer and using setRange.

https://api.dart.dev/stable/latest/dart-ffi/Uint32Pointer/asTypedList.html

https://api.dart.dev/stable/latest/dart-core/List/setRange.html

@a-siva a-siva added type-enhancement A request for a change that isn't a bug triaged Issue has been triaged by sub team labels Feb 19, 2025
@mraleph
Copy link
Member

mraleph commented Feb 20, 2025

@MohiuddinM could you give a bit more details on what you are trying to achieve? Where does TypedData you are trying to transfer to FFI originate from and what kind of operation are you trying to feed it into?

@dcharkes we have a bunch of open issues around the same topic (efficient TypedData transfer) - we should probably merge them all together into one.

Tangentially related:

@MohiuddinM
Copy link
Author

@mraleph I am trying to process video feed using AI, I get video feed using OpenCV (ffi), process it in Dart, and then pass it to tensorflow (ffi), and then get the result out back to Dart. The overhead for the transfer is 100ms more as compared to keeping the whole thing in C.

@dcharkes
Copy link
Contributor

process it in Dart

The overhead for the transfer is 100ms more as compared to keeping the whole thing in C.

Would it be possible to do this with only Pointer and asTypedList? Then all the data would stay in native memory instead of copying it to Dart memory and copying it back to native memory afterwards. Is the processing in Dart creating new typed datas that are in Dart memory at will or can they populate typed datas being passed in. In the latter case you can pre-alloc the memory on the native side and fill it with the Dart processing.

@MohiuddinM
Copy link
Author

Thanks @dcharkes I will try your solution.

@MohiuddinM
Copy link
Author

@dcharkes Thanks for the help, processing everything in the native memory solves the problem.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
area-vm Use area-vm for VM related issues, including code coverage, and the AOT and JIT backends. library-ffi triaged Issue has been triaged by sub team type-enhancement A request for a change that isn't a bug
Projects
None yet
Development

No branches or pull requests

5 participants