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

1

5

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.

Marcmarcano answered 4/6, 2020 at 17:18 Comment(1)
Presumably 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?Tribute
J
8

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/

Johnettajohnette answered 16/4, 2021 at 5:36 Comment(1)
Does this mean that Method Channels are generally slower? What if I am trying to build an Engine that utilizes the device sensors, the GPU for rendering and inference, and also send requests to the cloud all in real time? Would a Method Channel be too much and ffi be faster? (with memory considerations)Ballyhoo

© 2022 - 2024 — McMap. All rights reserved.