Skip to content

🥳 added: halve TTFX of NonLinMPC and MovingHorizonEstimator constructors - #511

Merged
franckgaga merged 2 commits into
mainfrom
TTFX_reduce_NLP
Nov 10, 2026
Merged

franckgaga merged 2 commits into
mainfrom
TTFX_reduce_NLP

Conversation

@franckgaga

@franckgaga franckgaga commented Nov 10, 2026 •

Copy link
Copy Markdown
Member

I introduced a new function-like wrapper, NoTypeFunction, that deliberately hide the very complex concrete type of the closures created for the evaluation of the objective function and its derivatives, in both NonLinMPC and MovingHorizonEstimator. It produces a dynamic dispatch when this wrapper is called, but this is acceptable since MOI.jl already does a dynamic dispatch internally when calling registered nonlinear operators.

This addition approximately halves the time-for-first-execution (TTFX) of NonLinMPC and MovingHorizonEstimator construction and also reduces the allocations on this MRE:

using ModelPredictiveControl
using ControlSystemsBase, LinearAlgebra

function myfunc()
    Ts = 400.0
    sys = [ tf(1.90,[1800.0,1])   tf(1.90,[1800.0,1])   tf(1.90,[1800.0,1]);
            tf(-0.74,[800.0,1])   tf(0.74,[800.0,1])    tf(-0.74,[800.0,1])   ] 

    linmodel = LinModel(sys,Ts,i_u=[1,2], i_d=[3])
    linmodel = setop!(linmodel, uop=[10,50], yop=[50,30], dop=[5])
    f(x,u,d,model) = model.A*x + model.Bu*u + model.Bd*d
    h(x,d,model)   = model.C*x + model.Dd*d
    nonlinmodel = NonLinModel(f, h, Ts, 2, 4, 2, 1, solver=nothing, p=linmodel)
    nonlinmodel = setop!(nonlinmodel, uop=[10,50], yop=[50,30], dop=[5])

    nmpc = NonLinMPC(nonlinmodel, Hp=3, hessian=true)
    mhe = MovingHorizonEstimator(nonlinmodel, He=3)
end

@time myfunc()

before:

 82.678084 seconds (31.71 M allocations: 1.663 GiB, 0.29% gc time, 99.98% compilation time)

after:

 34.710251 seconds (25.00 M allocations: 1.309 GiB, 0.82% gc time, 99.96% compilation time)

🍾🥳🍾🥳🍾🥳🍾🥳🍾🥳🍾🥳

Now let's see if it impacts the benchmarks...

…uctors

I introduced a new function like wrapper, `NoTypeFunction`, to hide the concrete types of closures created for the evaluation of the objective function and its derivatives.
@franckgaga franckgaga changed the title added: halve TTFX of NonLinMPC and MovingHorizonEstimator constructors 🥳 added: halve TTFX of NonLinMPC and MovingHorizonEstimator constructors Nov 10, 2026
@codecov-commenter

codecov-commenter commented Nov 10, 2026 •

Copy link
Copy Markdown

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 97.26%. Comparing base (e7420db) to head (35f8828).

Additional details and impacted files
@@            Coverage Diff             @@
##             main     #511      +/-   ##
==========================================
+ Coverage   97.23%   97.26%   +0.02%     
==========================================
  Files          28       28              
  Lines        7099     7101       +2     
==========================================
+ Hits         6903     6907       +4     
+ Misses        196      194       -2     
Flag Coverage Δ
unittests 97.26% <100.00%> (+0.02%) ⬆️

Flags with carried forward coverage won't be shown. Click here to find out more.

☔ View full report in Codecov by Harness.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

@franckgaga

franckgaga commented Nov 10, 2026 •

Copy link
Copy Markdown
Member Author

Woaaah the precompilation time is also drastically reduced. 🥳🥳🥳

before:

(TestMPC) pkg> precompile ModelPredictiveControl
Precompiling ModelPredictiveControl finished.
  1 dependency successfully precompiled in 494 seconds. 113 already precompiled.

after:

Precompiling ModelPredictiveControl finished.
  1 dependency successfully precompiled in 236 seconds. 113 already precompiled.

Moreover, executing my test suite is also drastically faster. The worst test, MHE estim. & getinfo (NonLinModel, SS), took before 2578.91 s, and now takes 223.91 s ! That's 10 times faster!

Let's hope it does not impact the benchmarks 🤞🤞🤞

@franckgaga

franckgaga commented Nov 10, 2026 •

Copy link
Copy Markdown
Member Author

Woahhhh the CI run on GitHub is twice faster on all the legs, that's insane haha.

I will try to re-enable Linux 32 bits and MacOS CI jobs in a separate PR, since the memory consumption to run the complete CI suite is significantly reduced.

@franckgaga

franckgaga commented Nov 10, 2026 •

Copy link
Copy Markdown
Member Author

Alright no differences on the MHE benchmarks for runtimes, but the memory allocations at each time steps are more than twice smaller! Let's see for MPC...

@franckgaga

franckgaga commented Nov 10, 2026 •

Copy link
Copy Markdown
Member Author

ALLLRIGHT similar improvement on the memory for the MPCs. The runtimes are even slighlty betters at each time steps, presumably because the number of allocations reduced.

This is the perfect PR, let's merge 🍾🥳

@franckgaga
franckgaga merged commit f7fe31d into main Nov 10, 2026
19 checks passed
@franckgaga
franckgaga deleted the TTFX_reduce_NLP branch November 10, 2026 21:10
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants