inexplicaply slow function

43 points Maarico

Hello everyone!

I am trying to rewrite a program, that I wrote for my bachelors thesis in rust (it was in C) to learn a bit of the language. I am done implementing the first part, however the performance is subpar to say it lightly. (In C I can do 10,000 iterations in 0.2s, where Rust takes about 2 and a half minutes in the release version.

I did some profiling and a big part of what is slowing my program down are two functions that are hardly 20 lines combined. This is the code https://pastebin.com/YaAs3h2M. The slow functions are meson_worm_step2_W_D and I think it is slowed by the other called meson_worm_step2_weight. The flamegraph performance report can be found here: https://ibb.co/YdkJY8F (sorry for the bad quality).

I would be delighted if anyone could look at my code and tell my what is slowing it down so much (maybe bounds checks?) And please go easy on me, this is my first semi-big project in rust :D

Thanks in advance!

EDIT: turns out my bottleneck was memory, because my structs were so large. Refactoring to reduce the variables that have to be cached improved my code to the point, that it is now faster than the original C version. Here is the diff if anyone is interested (note that the tests do not work yet) https://editor.mergely.com/FscH8nie/