extendr / rextendr

An R package that helps scaffolding extendr-enabled packages or compiling Rust code dynamically
https://extendr.github.io/rextendr/
Other
181 stars 27 forks source link

6x slowdown when calling Rust using rust_source() #243

Closed Jaage closed 1 year ago

Jaage commented 1 year ago

I have a lengthy function which takes about 1s to run in Rust, but 6s when I call that function from R. The function takes some vectors of data, such as means and standard deviations, does some sampling, and then does some dataframe stuff in Polars. It returns a 2D ndarray. The only thing different between the two is changing the return value from df.to_ndarray::<Float64Type>().unwrap() to df.to_ndarray::<Float64Type>().unwrap().try_into().unwrap().

I time the body of the Rust code and print it, which is nearly equal to the total time the function runs, so I know converting the arguments/return value between Rust and R objects is not causing this.

They have all the same dependency versions and are both running on nightly Rust. What might be causing such a slowdown? I ported this code to Rust to make it a lost faster and this has unfortunately caused a major performance regression.

I can provide the function if needed, it is just large and requires a lot of data to run.

yutannihilation commented 1 year ago

I think you can use rust_source(..., profile = "release").