Whats the difference between writing MethodChannel bridge versus dart:ffi bridge to run c/c++ code to get the response?

Viewed 826

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 vs dart: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.

1 Answers

Here is a tour repo for dart:ffi: https://github.com/Sunbreak/native_interop.tour

  1. Async/Sync
  • For MethodChannel, both Dart -> Native and Native -> Dart are asynchronous
  • For dart:ffi, Dart -> Native and Native -> Dart could be synchronous (except native call from non-mutator thread of Isolate)
  1. Memory
  • For MethodChannel, each interop requires serilization/deseriliazation
  • For dart:ffi, you'd easily write C-like memory-efficient operation
  1. 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/

Related