Description
Native methods in Dart are currently using our runtime call mechanism to call from Dart into C code. There is a significant performance overhead due to this: The native dart function cannot be inlined, we go indirectly through stubs, we box all arguments and make an array on the stack (in order to provide the C function a pointer to an array of arguments) ...
To avoid some of this overhead, we would like to enable calling natives using the existing FFI calling mechanism. That would allow us to pass unboxed primitives (e.g. integers/doubles) directly using C calling convention as well as dart objects via auto-wrapping in handles.
We want to prototype this on natives in Dart's core libraries and later make Flutter use it in dart:ui
for faster calls into C (e.g. skia calls).
This will require adding a symbol-lookup mechanism (either declaratively as we do with natives right now, or imperatively) to avoid depending on VM/Embedder symbols to be available at runtime.
Some tasks related to the Ffi Native support:
- Perf: Avoid expensive handle allocation for ffi natives that pass objects
- Size+Perf: Avoid using static fields in ffi native
- Size+Perf: Allow inlining ffi trampolines
- Size: Possibly always outline safepoint transitions (measure how much slower it would get)
- Size: Possibly avoid
StackOverflow
checks in ffi trampoline (we don't do it for natives) - Maintenance: Replace InvokeMathCFunction with FfiNative
- Feature: Limited simarm/simarm64 support for ffi - sufficient to migrate dart:io
/cc @mraleph