EXTERNAL vs NONLINEAR_LS with EXACT hessian

Hi acados developers :wave:
thank you for creating and mainting this really useful tool :slight_smile:

I’m using the Matlab/Simulink Interface and then generating code (aswell as the s-function) but i have a more general question regarding EXTERNAL vs NONLINEAR_LS cost type.

I have a cost function that can in general be formulated as NONLINEAR_LS.
However, I started with the EXTERNAL cost type and exact hessian. The solution time initally wasn’t fast enough for my application and i switched to NONLINEAR_LS with GAUSS_NEWTON hessian, which improved the solve time.

I believe GAUSS_NEWTON hessian is only “well defined” with the NONLINEAR_LS cost type as there information regarding the residuals can be derived?
When using GAUSS_NEWTON together with EXTERNAL cost type, the exact hessian for the cost term will be computed and the constraint and dynamics contribution will be ignored?
Are both points correct?

Nevertheless, due to formulation improvements and model reduction i’m now at a point where the solve time using EXACT hessian and NONLINEAR_LS cost type is fast enough for my apllication.
Since i find the interface using EXTERNAL cost type more straighforward than NONLINEAR_LS for my use case, i’m now considering switching back to EXTERNAL and EXACT hessian.

Now to my main question:
When using EXACT hessian, is there performance/code size advantage or disadvantage using between NONLINEAR_LS and EXTERNAL? Is there any exploitation happening?

I took a quick look at the source code and noticed that NONLINEAR_LS calls * fun_jac* and hess, in this case e.g. two sperate function calls and “information regarding the CasADi graph” can not be shared/used?.
Whereas EXTERNAL calls 'fun_jac_hess*, e.g. one function call for the jacobian and hessian. Could this one call be exploited in the Casadi Graph, e.g. could CasADi exploit shared subexpressions across value, gradient and hessian in the generated C code*?
Might there thus even be a benefit of using EXTERNAL in this case or am i overthing it? :smiley:

Best regards