jump-dev / Ipopt.jl

A Julia interface to the Ipopt nonlinear solver
https://github.com/coin-or/ipopt
Other
149 stars 58 forks source link

Some performance and allocation optimizations #391

Closed yuyichao closed 8 months ago

yuyichao commented 8 months ago

https://github.com/jump-dev/Ipopt.jl/pull/357 appears to increase the memory allocation for a problem I have by ~4x in number and ~1.7x in size. (It doesn't affect the constructed problem but it was affecting the overall performance on a somewhat memory constraint system (relative to the problem size))

Most of the allocations seems to be caused by unnecessary allocation of the array which is fixed in the second commit. The first and the third commits are some minor and easy optimizations I noticed while going through the code. These changes got the final allocation to be ~2x in number and ~1.1x in size.

The remaining additional allocation comes from the use of quadratic function to uniformly store all the constraints and objective information. (The extra allocation has two parts, one from the creation of the empty quadratic term vector when converting to a quadratic function and the other one from the index mapping for those terms at https://github.com/jump-dev/MathOptInterface.jl/blob/36ed9d8c8561d2fdc0cb7c1fe773566cbc0c7e29/src/Utilities/functions.jl#L315 ). This is fixed in the last commit by making the functions unions and rely on the union splitting from the compiler to deal with the resulting type instability. With this last commit, I got an allocation count/size that's basically the same as before.

odow commented 8 months ago

Nice. I'll fix the formatting. I went for simplicity over performance with #357, and I figured it didn't matter because no one complained (until now :smile:).

codecov[bot] commented 8 months ago

Codecov Report

All modified and coverable lines are covered by tests :white_check_mark:

Comparison is base (0eaf817) 94.06% compared to head (0582b64) 94.22%.

Additional details and impacted files ```diff @@ Coverage Diff @@ ## master #391 +/- ## ========================================== + Coverage 94.06% 94.22% +0.16% ========================================== Files 4 4 Lines 927 953 +26 ========================================== + Hits 872 898 +26 Misses 55 55 ``` | [Files](https://app.codecov.io/gh/jump-dev/Ipopt.jl/pull/391?src=pr&el=tree&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=jump-dev) | Coverage Δ | | |---|---|---| | [src/utils.jl](https://app.codecov.io/gh/jump-dev/Ipopt.jl/pull/391?src=pr&el=tree&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=jump-dev#diff-c3JjL3V0aWxzLmps) | `99.58% <100.00%> (+0.05%)` | :arrow_up: |

:umbrella: View full report in Codecov by Sentry.
:loudspeaker: Have feedback on the report? Share it here.

odow commented 8 months ago

Still happy?

yuyichao commented 8 months ago

Yeah looks good to me.