The comments say it should compile to a no-op and pass things through. But I don’t think that’s true? It throws away the const qualifier so unless the compiler does some backbends to prove to itself it doesn’t need to, it’s going to go ahead and construct another Eigen::Matrix<double, R, C>.
Anyway I think this should return const reference so that we’re not depending on compiler optimizations to make this avoid unnecessary copies.
That’s exactly my experience! This is why my templated GLM code was not running as fast as expected. I only figured out this behaviour after a lot of benchmarking.
I’m really not much of a C++ wizard. Are you suggesting that a fix would be to add a const to the return type?
Niiiiice, glad to have a verify on it. I’ll make an issue and a pull.
I was just trying to make sure the stuff in adj_jac_vari could be as efficient as hand-written varis. I just ran the unit tests and they passed with the signature:
Good find. I am curious what speedup we are going to get from this. This little change could make a decent speedup (if we pour into the code base sufficiently many const declarations wherever we can/should have done it).
Oh that would really be great. It would make the GLMs run a lot faster I imagine without having to write more long-winded code. Let me know if you’d like me to review something.
There’s signatures for (var and double being placeholder for matrices of var and double):
f(var, var)
f(double, var)
f(var, double)
Because of this there’s tons of code duplication etc. It’d be nice to avoid this.
edit: And I’m hoping I can make the adj_jac_vari fast enough so even in the most critical cases we can use it instead of writing manual varis. value_of being efficient is important for this.
Should we just make the change to the signature of value_of to return a const ref? Or is there a reason to wait on this/be careful? Are you taking care of it or shall I do it?