Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is there something like expression templates in Rust?

Tags:

c++

rust

In C++, expression templates is a technique that relies on the compiler's knowledge about expressions in C++ code to simplify them and optimize them beyond what would be possible in a procedural program. It's a powerful technique used by e.g. the Eigen and Armadillo matrix libraries to speed up certain compound operations on matrices. An incomplete wiki page on the Eigen web page almost starts explaining it.

I wonder if a similar technique exists in Rust, i.e. is there a way to make the Rust compiler optimize certain expressions at compile time so that the least amount of temporaries is created.

like image 299
rubenvb Avatar asked May 08 '15 07:05

rubenvb


1 Answers

If I read Expression Templates right, then you can see them in action with Rust Iterators: methods such as filter, take, etc etc return an expression template, a struct which represents the computation but doesn't perform it until requested. This gives the optimization you require right away, no temporaries are created.

Using the where clause I imagine one can write specializations to further optimize certain combinations of computations.

like image 152
ArtemGr Avatar answered Nov 15 '22 13:11

ArtemGr