Home > database >  How is Dart FFI implemented indeed? Are they as cheap as a normal function call, or do they do heavy
How is Dart FFI implemented indeed? Are they as cheap as a normal function call, or do they do heavy

Time:10-05

I am quite interested in how Dart FFI is implemented. Are they as cheap as a normal function call, or do they do heavy lifting under the hood?

I have searched through the Internet but cannot find much information. I only find this article talking a bit about the insights of argument passing and ABIs. In addition, I guess it should have some protections because Dart has things like GC while C does not.

Thanks for any hints!

CodePudding user response:

Dart FFI uses C's dlopen() for platforms other than Windows (non-POSIX). It's a very lightweight interface to shared objects. Compiled shared objects contain a table with symbol names and their memory offset. The shared object is opened with dlopen() then specific contained items are found in memory using dlsym() with their symbol name. This provides a memory address for, say, a function with the name 'testDartFFI()'. The dart runtime can then call this function with the memory address, using the Dart prototype to correctly pass and return values based on C standards. Overhead-wise it's not much different than calling other dynamically linked system libraries.

  • Related