Efficiency: use multiplication over std::pow for squaring (#3373) - #3375
Conversation
It's a bit hard to spot with grep, but there is one more file that has this pattern: While these are in our autodiff systems, they're both only concerned with double values, so the same kind of patch can be applied |
|
And thank you for your patch! |
|
Thank you for pointing that out. It looks like my initial search missed it. I will take another look through the codebase for any remaining files with this pattern and push the updates shortly. |
Jenkins Console Log Machine informationNo LSB modules are available. Distributor ID: Ubuntu Description: Ubuntu 20.04.3 LTS Release: 20.04 Codename: focalCPU: G++: Clang: |
|
Looking at the generated assembly, |
Ahh looking at the linked issue I see the motivation, ignore me - sorry for the noise! |
WardBrian
left a comment
There was a problem hiding this comment.
One minor thing but then I think we can merge this. Thank you!
| check_finite("squared_distance", "a", a); | ||
| check_finite("squared_distance", "b", b); | ||
| return make_callback_vari(std::pow(a.val() - b, 2), | ||
| return make_callback_vari((a.val() - b) * (a.val() - b), |
There was a problem hiding this comment.
| return make_callback_vari((a.val() - b) * (a.val() - b), | |
| double difference = a.val() - b; | |
| return make_callback_vari(difference * difference, |
And similarly above, to avoid the repetition
There was a problem hiding this comment.
I updated both functions to use a difference variable to avoid the repetition.
d312e24 to
c98e30e
Compare
|
The downstream tests are failing, even though they passed successfully on the previous, identical change. My guess is this is a CI flake or timeout issue .could you help confirm whether something else is going on? |
|
It’s an unrelated issue that can be ignored |
Jenkins Console Log Machine informationNo LSB modules are available. Distributor ID: Ubuntu Description: Ubuntu 20.04.3 LTS Release: 20.04 Codename: focalCPU: G++: Clang: |
WardBrian
left a comment
There was a problem hiding this comment.
Thank you @Purna-Chandra-4706!
Replaced std::pow(x, 2) with x * x after casting to double, preserving the prior rounding and overflow behavior. (Verified via grep that there are no other instances in the source code). Fixes #3373.