GeoStat-Framework / GSTools-Core

A Rust implementation of the core algorithms of GSTools.
GNU Lesser General Public License v3.0
10 stars 0 forks source link

Avoid dynamic dispatch in inner loop using trait-based dispatch. #2

Closed ghost closed 2 years ago

ghost commented 2 years ago

As the impl Fn(...) -> ... opaque type will resolve to fn(...) -> ..., the estimator and distance functions will be called via function pointers, i.e. via dynamic dispatch.

To avoid this, two traits - Estimator and Distance - are defined and the various algorithms are parametrized on those traits using type-level parameters. This way, the actual implementations will be monomorphized for the relevant combinations of estimator and distance function.

The static dispatch over the trait types is factored out using two rule-based helper macros.

Finally, the Estimator is converted to use array views instead of references to arrays so that the implementation of normalize_vec becomes independent of the estimator and can be parallelized using Zip::par_for_each.

ghost commented 2 years ago

The diff looks a bit wild, but I would say that this is mainly due to indentation changes when the unmodified implementation of each function is moved into the inner function imaginatively named inner.

LSchueler commented 2 years ago

Wow, that is so awesome! I see that I still got a lot to learn. Also, your commits are very well defined, which helps keeping track of your changes. I didn't even get to look at Rust macros yet. And I definitely have to get used to the differences in ArrayX and ArrayViewX :-) The implementation of the estimators is really elegant, I would have never thought of something like that in Cython.