Rust performance impact when using FFI to C

Hi everyone,

I wanted to know, what performance impact could we have in rust if we are making frequent FFI calls to a C API from our rust code.

Also, are there any benchmarks available for the same.

Thanks for your responses in advance.

At a basic level, there isn't any. As far as the CPU is concerned, it doesn't care what a function was written in.

What might cause performance loss (relative to if you were calling a Rust function) is the loss of inlining, and the potential loss of link-time optimisations (depending on the exact toolchain and setup). So frequently calling very short C functions will probably be slower than frequently calling very short Rust functions (assuming they're available for inlining and/or you have LTO turned on).

1 Like

It depends. Can be significantly faster or slower than a "normal" Rust version.
It gets slower the more you have to convert your data before passing or when receiving "from" C.

In Rust, FFI calls are just regular function calls, just as if C called a function in a C library. There isn't any special work required to call them.