From cb98574ad9486c8e4a2445b5e1545d5260c7b046 Mon Sep 17 00:00:00 2001 From: franckgaga Date: Tue, 10 Nov 2026 12:04:34 -0500 Subject: [PATCH 1/2] added: reduce TTFX of `NonLinMPC` and `MovingHorizonEstimator` constructors 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. --- src/controller/nonlinmpc.jl | 16 +++++++++++++--- src/estimator/mhe/construct.jl | 16 +++++++++++++--- src/general.jl | 15 +++++++++++++++ 3 files changed, 41 insertions(+), 6 deletions(-) diff --git a/src/controller/nonlinmpc.jl b/src/controller/nonlinmpc.jl index 102ce1765..d53a0dc08 100644 --- a/src/controller/nonlinmpc.jl +++ b/src/controller/nonlinmpc.jl @@ -943,10 +943,20 @@ function get_nonlinobj_op(mpc::NonLinMPC, optim::JuMP.GenericModel{JNT}) where J return fill_diffstructure!(∇²J_arg, ∇²J, ∇²J_structure) end end - if !isnothing(hess) - @operator(optim, J_op, nZ̃, J_func, ∇J_func!, ∇²J_func!) + J_op = if !isnothing(hess) + JuMP.add_nonlinear_operator( + optim, nZ̃, + NoTypeFunction(J_func), + NoTypeFunction(∇J_func!), + NoTypeFunction(∇²J_func!); + name = :J_op) else - @operator(optim, J_op, nZ̃, J_func, ∇J_func!) + JuMP.add_nonlinear_operator( + optim, + nZ̃, + NoTypeFunction(J_func), + NoTypeFunction(∇J_func!); + name = :J_op) end return J_op end diff --git a/src/estimator/mhe/construct.jl b/src/estimator/mhe/construct.jl index 1d6bb4f18..1b6fe0ef6 100644 --- a/src/estimator/mhe/construct.jl +++ b/src/estimator/mhe/construct.jl @@ -1551,10 +1551,20 @@ function get_nonlinobj_op( update_objective!(J, ∇J, ∇²J, Z̃_J, Z̃_arg) return fill_diffstructure!(∇²J_arg, ∇²J, ∇²J_structure) end - if !isnothing(hess) - @operator(optim, J_op, nZ̃, J_func, ∇J_func!, ∇²J_func!) + J_op = if !isnothing(hess) + JuMP.add_nonlinear_operator( + optim, nZ̃, + NoTypeFunction(J_func), + NoTypeFunction(∇J_func!), + NoTypeFunction(∇²J_func!); + name = :J_op) else - @operator(optim, J_op, nZ̃, J_func, ∇J_func!) + JuMP.add_nonlinear_operator( + optim, + nZ̃, + NoTypeFunction(J_func), + NoTypeFunction(∇J_func!); + name = :J_op) end return J_op end diff --git a/src/general.jl b/src/general.jl index fe2829a9d..3fe648e02 100644 --- a/src/general.jl +++ b/src/general.jl @@ -49,6 +49,21 @@ const ERROR_STATUSES = ( JuMP.INVALID_OPTION, JuMP.INTERRUPTED, JuMP.OTHER_ERROR ) +""" +A callable wrapper that erases the concrete type of its function. + +The `f` field is deliberately an abstract type (`Function`) to hide its concrete type. It +means that calling this wrapper produces a dynamic dispatch, which is acceptable since MOI +already does a dynamic dispatch when calling registered nonlinear operators. The goal is +to hide the concrete type of `J_func`, `∇J_func` and `∇²J_func` closures at construction, +since they are huge because of all the captured variables. Methods are slow to compile when +their signature contains giant types. The constructor TTFX is thus reduced with this trick. +""" +struct NoTypeFunction <: Function + f::Function +end +(obj::NoTypeFunction)(args...) = obj.f(args...) + "Verify that `optim` termination status is `OPTIMAL` or `LOCALLY_SOLVED`." function issolved(optim::JuMP.GenericModel) status = JuMP.termination_status(optim) From 35f882805eb666f69565fe6d8ab740d29ab2ce1e Mon Sep 17 00:00:00 2001 From: franckgaga Date: Tue, 10 Nov 2026 12:29:14 -0500 Subject: [PATCH 2/2] bump --- Project.toml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Project.toml b/Project.toml index ea7e74a04..1544b4abe 100644 --- a/Project.toml +++ b/Project.toml @@ -1,6 +1,6 @@ name = "ModelPredictiveControl" uuid = "61f9bdb8-6ae4-484a-811f-bbf86720c31c" -version = "2.16.1" +version = "2.16.2" authors = ["Francis Gagnon"] [deps]