Will rust optimize c funtion for opt3 like this?

assume that i have c callback function like this, it receive a tickdata and process it.

 pub unsafe extern "C" fn recv(tick: *mut instrument) {
                // any rust code here. 
                  MD_SENDER.assume_init_mut().send(*tick);
              }

and this API will be called by c function like this.

  unsafe {  set_update_md_callback(Some(recv)) ; } 

if I wrote some rust code in recv function, will rust compiler optimize o3 for recv function ?
Cargo.toml

[profile.release]
lto = true
opt-level = 3
codegen-units = 1

Being in an extern "C" doesn't affect whether the compiler optimises code or not. It just doesn't care.

LTO is a different kettle of fish that depends on what compilers and linkers are involved, and exactly what you're doing. Given that you're describing a callback-based system, that implies dynamic dispatch, which means LTO probably won't happen. But that's not a limitation of Rust, LTO just wouldn't apply on those sorts of calls.

1 Like

if i wrote some rust code in recv function, execution speed will decrease dramatically. is there any suggestions to avoid this?

Are you compiling the Rust code using cargo build --release?

of course i do.

i need some time to write an example. and i will show you this issue. I feel this is weird and beyond my common sense

If you can make a standalone example, https://rust.godbolt.org will show you what it compiles to (don't forget to add -O to flags there!)