Whats the difference between writing MethodChannel bridge versus dart:ffi bridge to run c/c++ code to get the response?
Here is a tour repo for dart:ffi: https://github.com/Sunbreak/native_interop.tour
- Async/Sync
- For MethodChannel, both
Dart -> Native
andNative -> Dart
are asynchronous - For dart:ffi,
Dart -> Native
andNative -> Dart
could be synchronous (except native call from non-mutator thread of Isolate)
- Memory
- For MethodChannel, each interop requires serilization/deseriliazation
- For dart:ffi, you'd easily write C-like memory-efficient operation
- Performance
dart:ffi synchronous interop has a good advantage on non-frequent small data
https://www.xdea.xyz/2020/11/flutter-platform-channel-%e6%80%a7%e8%83%bd%e6%b5%8b%e8%af%95/
Aawaz Gyawali
Android developer with knowledge of MySQL and php.
Updated on December 21, 2022Comments
-
Aawaz Gyawali over 1 year
Before I start my question I want to point out that it's not similar this question. Difference between writing platform specific code vs dart:ffi code. Here the questioner is asking the actual difference whereas I am trying to know the difference for the same task that can be achieved using both methods.
What's the difference in running native c/c++ code on the platform, getting the outcome on platform side(let's say Kotlin for instance) and sending it to dart via method channel versus writing the
dart:ffi
interface and directly calling the native c++ code. They basically will provide the same code execution. The only difference I see is that the MethodChannel call would be an async task vsdart:ffi
which will be synchronous. Apart from the async behavior, will there be any difference(performance primarily) in getting the response from either of the technique.-
Richard Heap almost 4 yearsPresumably it would be easier to code one ffi interface than all of Dart to swift and kotlin and thence to C through uni or whatever. Callbacks are a little easier with method channel over ffi. Presumably the ffi interface is more performant than serialising over a channel, but will it be significant?
-