From cd2a0a8579d748a687136c19ba4c244dfba69e34 Mon Sep 17 00:00:00 2001 From: Aayush Sabharwal Date: Wed, 29 Nov 2023 17:35:26 +0530 Subject: [PATCH] refactor: fix unnecessary allocations in SciMLFunction constructors --- src/scimlfunctions.jl | 48 +++++++++++++++++-------------------------- 1 file changed, 19 insertions(+), 29 deletions(-) diff --git a/src/scimlfunctions.jl b/src/scimlfunctions.jl index 52df42e0fe..4503b6d4fa 100644 --- a/src/scimlfunctions.jl +++ b/src/scimlfunctions.jl @@ -2464,7 +2464,7 @@ function ODEFunction{iip, specialize}(f; _f = prepare_function(f) - sys = something(sys, SymbolCache(something(syms, []), something(paramsyms, []), indepsym === nothing ? [] : [indepsym])) + sys = something(sys, SymbolCache(syms, paramsyms, indepsym)) if specialize === NoSpecialize ODEFunction{iip, specialize, @@ -2682,7 +2682,7 @@ function SplitFunction{iip, specialize}(f1, f2; sys = __has_sys(f1) ? f1.sys : nothing) where {iip, specialize, } - sys = something(sys, SymbolCache(something(syms, []), something(paramsyms, []), something([indepsym], []))) + sys = something(sys, SymbolCache(syms, paramsyms, indepsym)) if specialize === NoSpecialize SplitFunction{iip, specialize, Any, Any, Any, Any, Any, Any, Any, Any, Any, Any, Any, Any, Any, Any, @@ -2763,7 +2763,7 @@ function DynamicalODEFunction{iip, specialize}(f1, f2; iip, specialize, } - sys = something(sys, SymbolCache(something(syms, []), something(paramsyms, []), something([indepsym], []))) + sys = something(sys, SymbolCache(syms, paramsyms, indepsym)) if specialize === NoSpecialize DynamicalODEFunction{iip, specialize, Any, Any, Any, Any, Any, Any, Any, @@ -2811,7 +2811,7 @@ function DiscreteFunction{iip, specialize}(f; specialize, } _f = prepare_function(f) - sys = something(sys, SymbolCache(something(syms, []), something(paramsyms, []), something([indepsym], []))) + sys = something(sys, SymbolCache(syms, paramsyms, indepsym)) if specialize === NoSpecialize DiscreteFunction{iip, specialize, Any, Any, Any, Any}(_f, analytic, @@ -2859,7 +2859,7 @@ function ImplicitDiscreteFunction{iip, specialize}(f; specialize, } _f = prepare_function(f) - sys = something(sys, SymbolCache(something(syms, []), something(paramsyms, []), something([indepsym], []))) + sys = something(sys, SymbolCache(syms, paramsyms, indepsym)) if specialize === NoSpecialize ImplicitDiscreteFunction{iip, specialize, Any, Any, Any, Any}(_f, @@ -2955,7 +2955,7 @@ function SDEFunction{iip, specialize}(f, g; _f = prepare_function(f) _g = prepare_function(g) - sys = something(sys, SymbolCache(something(syms, []), something(paramsyms, []), something([indepsym], []))) + sys = something(sys, SymbolCache(syms, paramsyms, indepsym)) if specialize === NoSpecialize SDEFunction{iip, specialize, Any, Any, Any, Any, Any, Any, Any, Any, Any, Any, @@ -2985,12 +2985,10 @@ end function unwrapped_f(f::SDEFunction, newf = unwrapped_f(f.f), newg = unwrapped_f(f.g)) specialize = specialization(f) - sys = something(sys, SymbolCache(something(syms, []), something(paramsyms, []), something([indepsym], []))) - if specialize === NoSpecialize SDEFunction{isinplace(f), specialize, Any, Any, - typeoff(f.mass_matrix), Any, Any, + typeof(f.mass_matrix), Any, Any, Any, Any, Any, typeof(f.jac_prototype), typeof(f.sparsity), Any, Any, Any, Any, @@ -3087,7 +3085,7 @@ function SplitSDEFunction{iip, specialize}(f1, f2, g; iip, specialize, } - sys = something(sys, SymbolCache(something(syms, []), something(paramsyms, []), something([indepsym], []))) + sys = something(sys, SymbolCache(syms, paramsyms, indepsym)) if specialize === NoSpecialize SplitSDEFunction{iip, specialize, Any, Any, Any, Any, Any, Any, @@ -3170,7 +3168,7 @@ function DynamicalSDEFunction{iip, specialize}(f1, f2, g; iip, specialize, } - sys = something(sys, SymbolCache(something(syms, []), something(paramsyms, []), something([indepsym], []))) + sys = something(sys, SymbolCache(syms, paramsyms, indepsym)) if specialize === NoSpecialize DynamicalSDEFunction{iip, specialize, Any, Any, Any, Any, Any, Any, @@ -3266,7 +3264,7 @@ function RODEFunction{iip, specialize}(f; =# _f = prepare_function(f) - sys = something(sys, SymbolCache(something(syms, []), something(paramsyms, []), something([indepsym], []))) + sys = something(sys, SymbolCache(syms, paramsyms, indepsym)) if specialize === NoSpecialize @@ -3354,7 +3352,7 @@ function DAEFunction{iip, specialize}(f; end _f = prepare_function(f) - sys = something(sys, SymbolCache(something(syms, []), something(paramsyms, []), something([indepsym], []))) + sys = something(sys, SymbolCache(syms, paramsyms, indepsym)) if specialize === NoSpecialize @@ -3442,7 +3440,7 @@ function DDEFunction{iip, specialize}(f; end _f = prepare_function(f) - sys = something(sys, SymbolCache(something(syms, []), something(paramsyms, []), something([indepsym], []))) + sys = something(sys, SymbolCache(syms, paramsyms, indepsym)) if specialize === NoSpecialize @@ -3532,7 +3530,7 @@ function DynamicalDDEFunction{iip, specialize}(f1, f2; iip, specialize, } - sys = something(sys, SymbolCache(something(syms, []), something(paramsyms, []), something([indepsym], []))) + sys = something(sys, SymbolCache(syms, paramsyms, indepsym)) if specialize === NoSpecialize DynamicalDDEFunction{iip, specialize, Any, Any, Any, Any, Any, Any, Any, Any, Any, @@ -3612,7 +3610,7 @@ function SDDEFunction{iip, specialize}(f, g; _f = prepare_function(f) _g = prepare_function(g) - sys = something(sys, SymbolCache(something(syms, []), something(paramsyms, []), something([indepsym], []))) + sys = something(sys, SymbolCache(syms, paramsyms, indepsym)) if specialize === NoSpecialize @@ -3716,7 +3714,7 @@ function NonlinearFunction{iip, specialize}(f; end _f = prepare_function(f) - sys = something(sys, SymbolCache(something(syms, []), something(paramsyms, []))) + sys = something(sys, SymbolCache(syms, paramsyms)) if specialize === NoSpecialize @@ -3771,20 +3769,12 @@ function IntervalNonlinearFunction{iip, specialize}(f; specialize, } _f = prepare_function(f) - sys = something(sys, SymbolCache(something(syms, []), something(paramsyms, []))) + sys = something(sys, SymbolCache(syms, paramsyms)) if specialize === NoSpecialize IntervalNonlinearFunction{iip, specialize, - Any, Any, Any, - typeof(_colorvec), Any}(_f, mass_matrix, - analytic, tgrad, jac, - jvp, vjp, - jac_prototype, - sparsity, Wfact, - Wfact_t, paramjac, - observed, - _colorvec, sys) + Any, Any, Any, Any}(_f, analytic, observed, sys) else IntervalNonlinearFunction{iip, specialize, typeof(_f), typeof(analytic), @@ -3830,7 +3820,7 @@ function OptimizationFunction{iip}(f, adtype::AbstractADType = NoAD(); expr = nothing, cons_expr = nothing, sys = __has_sys(f) ? f.sys : nothing) where {iip} _f = prepare_function(f) - sys = something(sys, SymbolCache(something(syms, []), something(paramsyms, []))) + sys = something(sys, SymbolCache(syms, paramsyms)) isinplace(_f, 2; has_two_dispatches = false, isoptimization = true) OptimizationFunction{iip, typeof(adtype), typeof(_f), typeof(grad), typeof(hess), @@ -3978,7 +3968,7 @@ function BVPFunction{iip, specialize, twopoint}(f, bc; _f = prepare_function(f) - sys = something(sys, SymbolCache(something(syms, []), something(paramsyms, []), something([indepsym], []))) + sys = something(sys, SymbolCache(syms, paramsyms, indepsym)) if specialize === NoSpecialize