Posts

Showing posts with the label compiler-construction

Why don't C++ compilers do better constant folding?

I'm investigating ways to speed up a large section of C++ code, which has automatic derivatives for computing jacobians. This involves doing some amount of work in the actual residuals, but the majority of the work (based on profiled execution time) is in calculating the jacobians. This surprised me, since most of the jacobians are propagated forward from 0s and 1s, so the amount of work should be 2-4x the function, not 10-12x. In order to model what a large amount of the jacobian work is like, I made a super minimal example with just a dot product (instead of sin, cos, sqrt and more that would be in a real situation) that the compiler should be able to optimize to a single return value: #include <Eigen/Core> #include <Eigen/Geometry> using Array12d = Eigen::Matrix<double,12,1>; double testReturnFirstDot(const Array12d& b) { Array12d a; a.array() = 0.; a(0) = 1.; return a.dot(b); } Which should be the same as double testReturnFirst(const A...