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
Add support for DartNativeExternalTypedData to enable zero-copy buffers #3
Conversation
For reference, we've been doing some experiments with Rust -> Flutter on embedded systems. Avoiding a buffer copy is more performant in some cases, and also enables zero-copy serialization approaches such as flat_buffers, capnproto, etc. At a large enough buffer size, doing a full copy using We're still doing some experiments to see just how effective this approach is, but the idea is to enable transmitting data from Rust -> Dart in a way that makes frame drops unlikely. |
That's really really interesting for me too, I guess we can swap the current Another approach is to create a wrapper type over anything that can be converted In my opinion the second approach is better and maybe in the future like when releasing a v0.2 we can make it the default. My current use case for this that I'm building something that uses ProtocolBuffers as Request Response approach, instead of calling a lot of FFI functions in both languages. I would like to see if we could work on the |
I would think that a I can add that to this PR if you like. |
Yes, That would be great. Thank you. |
FYI -- I haven't been able to reproduce the results from frame drops on native free so far, but with the When running the same test using |
That looks good so far, thank you @jerel. Could you please fix the lints too? |
Ok it sounds that it is something with CI uses nightly, which uses different rules for formatting. Let me know if it is now ready, I will merge and fix the lints issue after that. And then we would release a new version to crates.io |
@shekohex ah, ok. I was surprised that CI failed this last time because I ran clippy and fmt locally. Yes, it's ready as far as I'm concerned. |
Thanks, Merged 🎉 |
Available now since Thanks, everyone! |
Edit: this has now been updated to add a
ZeroCopyBuffer
type. Usage is now simply:Since there is already an impl for `Vec` I'm not sure of a good way to add this zero-copy approach to `into_dart` without it conflicting with the existing copy approach so I've just included a working example below. For now we're doing the work shown in application code and passing the resulting `DartCObject` to `isolate.post()` and it works well. This PR adds the necessary types to enable that.
The Dart VM exposes both the
DartNativeTypedData
and theDartNativeExternalTypedData
as aUint8List
but in theDartNativeExternalTypedData
case the finalizer calls the providedcallback
to deallocate.The implementation that sends a zero copy mutable buffer to Dart looks like this: