symforce-org / symforce

Fast symbolic computation, code generation, and nonlinear optimization for robotics
https://symforce.org
Apache License 2.0
1.44k stars 147 forks source link

Idea: `PerformanceWarning` #378

Open aaron-skydio opened 10 months ago

aaron-skydio commented 10 months ago

Some thoughts from discussing this:

jpreiss commented 6 months ago

I noticed my generated code has a lot of std::pow calls where y = 2 is known at codegen time. Is there anything I can do to make symforce emit something like _tmp2 = _tmp1 * _tmp1 instead?

aaron-skydio commented 6 months ago

For a constant exponent of 2 known at codegen time, your compiler should optimize the std::pow to a multiply, so we don't bother special casing that to generate a multiply - the logic for which powers we special-case is here: https://github.com/symforce-org/symforce/blob/main/symforce/codegen/backends/cpp/cpp_code_printer.py#L68

If you have a compiler where this doesn't get optimized and actually results in a function call or something else that's not just a multiplication instruction that would be good to know though and we can also special-case that to generate a multiplication