# DifferentialEquations Package Kills Performance Everywhere when TruncatedStacktraces is used

**URL:** <https://discourse.julialang.org/t/differentialequations-package-kills-performance-everywhere-when-truncatedstacktraces-is-used/96417>\
**Category:** Performance\
**Tags:** package, differentialequation\
**Created:** [March 21, 2023, 7:01pm UTC](https://discourse.julialang.org/t/differentialequations-package-kills-performance-everywhere-when-truncatedstacktraces-is-used/96417 "2023-03-21T19:01:34Z")\
**Posts on this page:** 20\
**Page:** 2

<div class="post-metadata">

**Author:** ![Oscar\_Smith](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/oscar_smith/32/25343_2.png) [@Oscar\_Smith](https://discourse.julialang.org/u/Oscar_Smith)\
**Post date:** [March 23, 2023, 3:22am UTC](https://discourse.julialang.org/t/differentialequations-package-kills-performance-everywhere-when-truncatedstacktraces-is-used/96417/21 "2023-03-23T03:22:55Z")

</div>

The `Nothing` parameters are used when no value was specified. For example one of those `Nothing`s exists because there are no `tstops` (times where you want to force the solver to stop). Sure you could express this as an empty array, but if you did so, you still wouldn’t know the type of the array it should be so you would still need a type parameter.

---

<div class="post-metadata">

**Author:** ![CameronBieganek](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/cameronbieganek/32/6915_2.png) [@CameronBieganek](https://discourse.julialang.org/u/CameronBieganek)\
**Post date:** [March 23, 2023, 3:29am UTC](https://discourse.julialang.org/t/differentialequations-package-kills-performance-everywhere-when-truncatedstacktraces-is-used/96417/22 "2023-03-23T03:29:09Z")

</div>

> [@Oscar\_Smith](#):
>
> The `Nothing` parameters are used when no value was specified.

I understand that. This reminds me of the [admonition](https://guide.elm-lang.org/error_handling/maybe.html) in the Elm Lang book to not overuse the `Maybe` type:

> This `Maybe` type is quite useful, but there are limits. Beginners are particularly prone to getting excited about `Maybe` and using it everywhere, even though a custom type would be more appropriate.

In other words, types that are 90% empty are not doing a very good job of encoding the domain logic into the type system. Julia has an expressive type system. Filling 90% of fields with `nothing` is not really taking advantage of the expressiveness of the type system.

---

<div class="post-metadata">

**Author:** ![Elrod](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/elrod/32/22461_2.png) [@Elrod](https://discourse.julialang.org/u/Elrod)\
**Post date:** [March 23, 2023, 4:22am UTC](https://discourse.julialang.org/t/differentialequations-package-kills-performance-everywhere-when-truncatedstacktraces-is-used/96417/23 "2023-03-23T04:22:12Z")

</div>

How so?  
You don’t want to define structs representing the exponential explosion of possibilities. Instead, use a large product type parameterized to express whatever particular combinations of behavior or functionality the user wants.

---

<div class="post-metadata">

**Author:** ![jishnub](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/jishnub/32/33620_2.png) [@jishnub](https://discourse.julialang.org/u/jishnub)\
**Post date:** [March 23, 2023, 9:18am UTC](https://discourse.julialang.org/t/differentialequations-package-kills-performance-everywhere-when-truncatedstacktraces-is-used/96417/24 "2023-03-23T09:18:35Z")

</div>

Perhaps a less disruptive solution to address the stacktrace readability might be exported type-aliases for common cases? E.g.

```julia
julia> module temp
           struct A{T1,T2,T3}
               a::T1
               b::T2
               c::T3
           end
           AN{T1} = A{T1,Nothing,Nothing}
           export AN
       end
Main.temp

julia> f(x) = temp.A(x,nothing,nothing) + 1
f (generic function with 2 methods)

julia> f(2)
ERROR: MethodError: no method matching +(::Main.temp.AN{Int64}, ::Int64)

Closest candidates are:
  +(::Any, ::Any, ::Any, ::Any...)
   @ Base operators.jl:578
  +(::T, ::T) where T<:Union{Int128, Int16, Int32, Int64, Int8, UInt128, UInt16, UInt32, UInt64, UInt8}
   @ Base int.jl:87
  +(::Rational, ::Integer)
   @ Base rational.jl:327
  ...

Stacktrace:
 [1] f(x::Int64)
   @ Main ./REPL[17]:1
 [2] top-level scope
   @ REPL[18]:1

```

In this case, the alias is used in the stacktrace, and it’s much easier to read.

---

<div class="post-metadata">

**Author:** ![ChrisRackauckas](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/chrisrackauckas/32/77_2.png) [@ChrisRackauckas](https://discourse.julialang.org/u/ChrisRackauckas)\
**Post date:** [March 23, 2023, 9:28am UTC](https://discourse.julialang.org/t/differentialequations-package-kills-performance-everywhere-when-truncatedstacktraces-is-used/96417/25 "2023-03-23T09:28:55Z")

</div>

We’re discussing two missing features in the language. The problem is that type parameters for specialization are not necessarily type parameters that are intended to be used for dispatch.

> <https://github.com/JuliaLang/julia/issues/45687>
>
> I was looking again at https://github.com/JuliaLang/julia/issues/40138 and my mo…re evolved understanding of this issue is the fact that fully parametric fields are treated in the same manner as other type parameters, and that just seems wrong. For example, if I have a type
> 
> \`\`\`julia
> struct MyType{T}
> x::T
> end
> \`\`\`
> 
> What our current stacktrace assumes is that knowing \`T\` is "fundamental" to understanding \`MyType\`. In reality, I only did \`::T\` because I wanted the Julia compiler to optimize it. If it wasn't a programming optimization, then I would've done:
> 
> \`\`\`julia
> struct MyType
> x
> end
> \`\`\`
> 
> There's nothing fundamental to understanding the type of \`x\` that is required for understanding how \`MyType\` works: nothing ever dispatches on it, it's not part of the public API (so I don't intend anyone to ever dispatch on it), yet it's treated as first-class information. How can I allow program specialization here without allowing ease of dispatch? I can't. This seems to be a fundamentally missing feature in the type system.
> 
> For an example of this in practice, look at none other than the infamous \`ODEIntegrator\` type.
> 
> \`\`\`julia
> mutable struct ODEIntegrator{algType\<:Union{OrdinaryDiffEqAlgorithm,DAEAlgorithm},IIP,uType,duType,tType,pType,eigenType,EEstT,QT,tdirType,ksEltype,SolType,F,CacheType,O,FSALType,EventErrorType,CallbackCacheType,IA} \<: DiffEqBase.AbstractODEIntegrator{algType,IIP,uType,tType}
> sol::SolType
> u::uType
> du::duType
> k::ksEltype
> t::tType
> dt::tType
> f::F
> p::pType
> uprev::uType
> uprev2::uType
> duprev::duType
> tprev::tType
> alg::algType
> dtcache::tType
> dtchangeable::Bool
> dtpropose::tType
> tdir::tdirType
> eigen\_est::eigenType
> EEst::EEstT
> qold::QT
> q11::QT
> erracc::QT
> dtacc::tType
> success\_iter::Int
> iter::Int
> saveiter::Int
> saveiter\_dense::Int
> cache::CacheType
> callback\_cache::CallbackCacheType
> kshortsize::Int
> force\_stepfail::Bool
> last\_stepfail::Bool
> just\_hit\_tstop::Bool
> do\_error\_check::Bool
> event\_last\_time::Int
> vector\_event\_last\_time::Int
> last\_event\_error::EventErrorType
> accept\_step::Bool
> isout::Bool
> reeval\_fsal::Bool
> u\_modified::Bool
> reinitialize::Bool
> isdae::Bool
> opts::O
> destats::DiffEqBase.DEStats
> initializealg::IA
> fsalfirst::FSALType
> fsallast::FSALType
> end
> \`\`\`
> 
> where one of its fields are:
> 
> \`\`\`julia
> mutable struct DEOptions{absType,relType,QT,tType,Controller,F1,F2,F3,F4,F5,F6,F7,tstopsType,discType,ECType,SType,MI,tcache,savecache,disccache}
> maxiters::MI
> save\_everystep::Bool
> adaptive::Bool
> abstol::absType
> reltol::relType
> gamma::QT
> qmax::QT
> qmin::QT
> qsteady\_max::QT
> qsteady\_min::QT
> qoldinit::QT
> failfactor::QT
> dtmax::tType
> dtmin::tType
> controller::Controller
> internalnorm::F1
> internalopnorm::F2
> save\_idxs::SType
> tstops::tstopsType
> saveat::tstopsType
> d\_discontinuities::discType
> tstops\_cache::tcache
> saveat\_cache::savecache
> d\_discontinuities\_cache::disccache
> userdata::ECType
> progress::Bool
> progress\_steps::Int
> progress\_name::String
> progress\_message::F6
> timeseries\_errors::Bool
> dense\_errors::Bool
> dense::Bool
> save\_on::Bool
> save\_start::Bool
> save\_end::Bool
> save\_end\_user::F3
> callback::F4
> isoutofdomain::F5
> unstable\_check::F7
> verbose::Bool
> calck::Bool
> force\_dtmin::Bool
> advance\_to\_tstop::Bool
> stop\_at\_next\_tstop::Bool
> end
> \`\`\`
> 
> Beautiful. This is the thing that generated a literally 10,000 character stack trace when Unitful numbers were put into it. But why? Let's understand this in the context of the above. 
> 
> \`\`\`julia
> abstol::absType
> reltol::relType
> \`\`\`
> 
> \`abstol\` and \`reltol\` are parametrically typed because it's possible for a user to pass different types in there. Yes it's normal to \`solve(prob,Tsit5(),abstol=1e-6,reltol=1e-6)\`, but you can also do \`solve(prob,Tsit5(),abstol=\[1e-6,1e-2\],reltol=1e-6)\` to have per-element tolerances. Great feature right? Well by allowing this feature, I now have to make the internal struct parametric, and so now everybody who uses the ODE solver has to see \`absType\` and \`relType\` in every stack trace. Mind you, not a single function in the entire library dispatches on this type parameter, nor is anything ever intended to, so this type parameter is a waste of time in any stacktrace: I've just learned to ignore it.
> 
> The integrator type has \>100 type parameters at any given type, and the first thing we have to train a user to do is ignore all of them. What the hell? Don't we think something has gone very wrong here?
> 
> I think the issue is that we have in the type syntax conflated the different ideas of "I want this program to specialize on this field" and "I want to be able to dispatch on this field". When we write functions, we do \`f(x)\` and we know that \`f\` will (most likely, some heuristics) specialize on the type of \`x\`. We do not require that people write \`f(x::T) where T\` everywhere. If we did, our function stack traces would be ginormous because every call would describe methods like \`f(x::T,y::T2,z::T3) where {T1.T2.T3}\` and people would go "what does this mean?". But we do not treat types the same way: we force basically "where T" on every field that we want to specialize, and we print this information to the user that basically just say "hey, DifferentialEquations.jl likes to specialize, you wanted to know that right?"
> 
> No, nobody cares. So what can we do? What about a new language feature for non-dispatchable field specialization syntax?
> 
> \`\`\`julia
> mutable struct DEOptions{tType}
> maxiters::Specialize
> save\_everystep::Bool
> adaptive::Bool
> abstol::Specialize
> reltol::Specialize
> gamma::Specialize
> qmax::Specialize
> qmin::Specialize
> qsteady\_max::Specialize
> qsteady\_min::Specialize
> qoldinit::Specialize
> failfactor::Specialize
> dtmax::tType
> dtmin::tType
> controller::Specialize
> internalnorm::Specialize
> internalopnorm::Specialize
> save\_idxs::Specialize
> tstops::Specialize
> saveat::Specialize
> d\_discontinuities::Specialize
> tstops\_cache::Specialize
> saveat\_cache::Specialize
> d\_discontinuities\_cache::Specialize
> userdata::Specialize
> progress::Bool
> progress\_steps::Int
> progress\_name::String
> progress\_message::Specialize
> timeseries\_errors::Bool
> dense\_errors::Bool
> dense::Bool
> save\_on::Bool
> save\_start::Bool
> save\_end::Bool
> save\_end\_user::Specialize
> callback::Specialize
> isoutofdomain::Specialize
> unstable\_check::Specialize
> verbose::Bool
> calck::Bool
> force\_dtmin::Bool
> advance\_to\_tstop::Bool
> stop\_at\_next\_tstop::Bool
> end
> \`\`\`
> 
> And it would then be really nice to have a macro in Base where this is equivalent to:
> 
> \`\`\`julia
> @specialize mutable struct DEOptions{tType}
> maxiters
> save\_everystep::Bool
> adaptive::Bool
> abstol
> reltol
> gamma
> qmax
> qmin
> qsteady\_max
> qsteady\_min
> qoldinit
> failfactor
> dtmax::tType
> dtmin::tType
> controller
> internalnorm
> internalopnorm
> save\_idxs
> tstops
> saveat
> d\_discontinuities
> tstops\_cache
> saveat\_cache
> d\_discontinuities\_cache
> userdata
> progress::Bool
> progress\_steps::Int
> progress\_name::String
> progress\_message
> timeseries\_errors::Bool
> dense\_errors::Bool
> dense::Bool
> save\_on::Bool
> save\_start::Bool
> save\_end::Bool
> save\_end\_user
> callback
> isoutofdomain
> unstable\_check
> verbose::Bool
> calck::Bool
> force\_dtmin::Bool
> advance\_to\_tstop::Bool
> stop\_at\_next\_tstop::Bool
> end
> \`\`\`
> 
> This better captures the essence of what the type is, makes it easier to write code without performance bugs, and will go a very long way to making the stacktraces more legible.

So yes it’s weird that these are shown to the user at all, because there’s no intention for anyone to be dispatching on them except for the compiler as a code generation tool. However, Julia does not make a distinction between type parameters for specialization and dispatch, and so since we have not made the distinction the only way to make this optimize is to expose type parameters that have no intended use.

So then the next missing feature is control of stacktrace printing. The expected controls were for some reason not merged even as experimental:

> <https://github.com/JuliaLang/julia/pull/48444>
>
> It is fairly common to define types with parameters whose only use is specializa…tion (of code and memory layout), and printing those parameters everywhere can cause excessive noise. One motivating example is https://github.com/SciML/OrdinaryDiffEq.jl/blob/2eb73bed76c31248113fa8f3f05ff2f84448651a/src/integrators/type.jl#L80. The proposed feature here is to put \`Experimental.@quietparams\` in a struct declaration, and then printing instances and types within stacktraces will omit parameters, e.g.
> \`\`\`
> julia\> struct Foo{A,B,C}
> a::A
> b::B
> c::C
> Base.Experimental.@quietparams
> end
> 
> julia\> map(error, \[Foo((1,2),4,5)\])
> ERROR: Foo((1, 2), 4, 5)
> Stacktrace:
> \[1\] error(s::Foo{...})
> @ Base ./error.jl:44
> \`\`\`
> 
> In the case of \`ODEIntegrator\`, each type can be multiple screens full of text and it is causing quite a problem.

And so we both have a bunch of type parameters that we don’t want, and we cannot control the printing to remove these pseudo type parameters.

Note that TruncatedStacktraces is now not on by default and requires a preference to be set.

```julia
## Enabling TruncatedStacktraces.jl

TruncatedStacktraces.jl is currently disabled by default, as it causes invalidations which will slow down package loading.

It can be enabled using Preferences.jl. To enable it, create a `LocalPreferences.toml` with the following entry:

[TruncatedStacktraces] disable = false

Alternatively, you can generate the `LocalPreferences.toml` using:

using Preferences, UUIDs using TruncatedStacktraces Preferences.set_preferences!(TruncatedStacktraces, "disable" => false) # OR if you don't want to load TruncatedStacktraces.jl Preferences.set_preferences!(UUID("781d530d-4396-4725-bb49-402e4bee1e77"), "disable" => false)

In either case, you need to reload your packages (depending on TruncatedStacktraces) for the change to take effect.

**TruncatedStacktraces is known to create invalidations, to remove these simply set the preference to disable it!**

```

It’s fine to use with system images (which everyone should be using anyways) but no longer the default. There just is no answer to better stacktraces until Julia Base merges something.

As it’s no longer the default, the loading performance issue is gone unless someone opts-in via a preference, so we can call this closed.

> [@CameronBieganek](#):
>
> In other words, types that are 90% empty are not doing a very good job of encoding the domain logic into the type system. Julia has an expressive type system. Filling 90% of fields with `nothing` is not really taking advantage of the expressiveness of the type system.

You do realize that in just that case you’re telling me that creating `1.3076744e+12` types on just the `ODEFunction` is a better way of using Julia’s expressive type system? To me, 10^12 types sounds bonkers 😅. And note that will overflow the limit in the compiler so it’s not feasible without a custom Julia build that bumps the type limit. I would hardly say that’s a good ergonomic solution for what’s already one parameterized type. This has to be the oddest suggestion I’ve seen on the forum.

The real thing is that a form of OpaqueClosures with standard optimizations is required in Base to reduce function specializations, but that of course is a topic we all have beaten to death elsewhere.

---

<div class="post-metadata">

**Author:** ![DanielVandH](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/danielvandh/32/31134_2.png) [@DanielVandH](https://discourse.julialang.org/u/DanielVandH)\
**Post date:** [March 23, 2023, 9:34am UTC](https://discourse.julialang.org/t/differentialequations-package-kills-performance-everywhere-when-truncatedstacktraces-is-used/96417/26 "2023-03-23T09:34:08Z")

</div>

Hopefully we can eventually get [RFC: "quiet" types for easier-to-read stack traces by JeffBezanson · Pull Request #48444 · JuliaLang/julia · GitHub](https://github.com/JuliaLang/julia/pull/48444). Just looking through the PR it’s not clear to me what the drawback from including it would be, and it’s really helpful - would be nice to just add it and then think about more sophisticated ways about handling stacktrace issues further along (which I don’t imagine will be solved quite as easily or as soon especially). Am I missing something with this PR?

The daunting stacktraces are too problematic for newcomers I reckon, as I’ve seen when teaching with Julia, it’s a shame to not have #48444.

---

<div class="post-metadata">

**Author:** ![uniment](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/uniment/32/24532_2.png) [@uniment](https://discourse.julialang.org/u/uniment)\
**Post date:** [March 23, 2023, 11:54am UTC](https://discourse.julialang.org/t/differentialequations-package-kills-performance-everywhere-when-truncatedstacktraces-is-used/96417/27 "2023-03-23T11:54:26Z")

</div>

> [@DanielVandH](#):
>
> The daunting stacktraces are too problematic for newcomers I reckon

This is a good observation. Not only are long stacktraces visually noisy; to the uninitiated, they’re downright **scary**. _Oh no! Did I break Julia!?_ When you don’t know what a stacktrace is good for, it’s just the computer screaming at you.

This might be heresy, but considering that 90% of the time in interactive mode I don’t care to see the stacktrace anyway because I have some silly typo that can be fixed in half a second, I’d be happy if the stacktrace didn’t even show by default. Having to type `err` to see the stacktrace isn’t so bad—if I’m looking at the stacktrace, I’m signing up for serious debug effort anyway.

---

<div class="post-metadata">

**Author:** ![CameronBieganek](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/cameronbieganek/32/6915_2.png) [@CameronBieganek](https://discourse.julialang.org/u/CameronBieganek)\
**Post date:** [March 23, 2023, 4:10pm UTC](https://discourse.julialang.org/t/differentialequations-package-kills-performance-everywhere-when-truncatedstacktraces-is-used/96417/28 "2023-03-23T16:10:47Z")

</div>

> [@ChrisRackauckas](#):
>
> You do realize that in just that case you’re telling me that creating `1.3076744e+12` types on just the `ODEFunction` is a better way of using Julia’s expressive type system?

Creating a new type for every possible combination of input types was not what I had in mind. What I had in mind was more composition of smaller types, most of which are concrete.

Are there fields in `DEOptions` or `ODEIntegrator` that could be a `Union` type instead of a type parameter? E.g., `Union{Int, Nothing}` or `Union{Vector{Float64}, Nothing}`?

I tried out the function barrier approach that I mentioned above, and it almost works, but not quite. As expected, all of the types inside the function barrier are fully inferred. But it doesn’t help with stacktraces. It just shifts all the types from one place to another.

The toy example I created is in the spirit of `ODEFunction`. I tried two different versions, `foo` and `bar`.

```julia
struct Funcs{F}
    f::F
    more_funcs
end

Funcs(f; more_funcs...) = Funcs(f, more_funcs)

foo(fs::Funcs, x) = _foo(fs.f, x; fs.more_funcs...)

function _foo(f, x; more_funcs...)
    g = get(more_funcs, :g, identity)
    h = get(more_funcs, :h, identity)
    k = get(more_funcs, :k, identity)
    f(g(h(k(x))))
end

function bar(fs::Funcs, x)
    g = get(fs.more_funcs, :g, identity)
    h = get(fs.more_funcs, :h, identity)
    k = get(fs.more_funcs, :k, identity)
    _bar(fs.f, g, h, k, x)
end

_bar(f, g, h, k, x) = f(g(h(k(x))))

funcs = Funcs(x -> x + 1,
    g = x -> x + 2,
    h = x -> x + 3,
    k = x -> x + 4,
)

```

Type inference works inside `_foo` and `_bar`:

> **Output of \`@code\_warntype\`**
>
> ```julia
> julia> @code_warntype _foo(funcs.f, 10; funcs.more_funcs...)
> MethodInstance for (::var"#_foo##kw")(::NamedTuple{(:g, :h, :k), Tuple{var"#8#12", var"#9#13", var"#10#14"}}, ::typeof(_foo), ::var"#7#11", ::Int64)
> from (::var"#_foo##kw")(::Any, ::typeof(_foo), f, x) in Main at /Users/cameron/projects/julia_misc/function_barrier.jl:11
> Arguments
> _::Core.Const(var"#_foo##kw"())
> @_2::Core.Const((g = var"#8#12"(), h = var"#9#13"(), k = var"#10#14"()))
> @_3::Core.Const(_foo)
> f::Core.Const(var"#7#11"())
> x::Int64
> Locals
> more_funcs...::Base.Pairs{Symbol, Function, Tuple{Symbol, Symbol, Symbol}, NamedTuple{(:g, :h, :k), Tuple{var"#8#12", var"#9#13", var"#10#14"}}}
> Body::Int64
> 1 ─ (more_funcs... = Base.pairs(@_2))
> │ %2 = Main.:(var"#_foo#6")(more_funcs...::Core.Const(Base.Pairs{Symbol, Function, Tuple{Symbol, Symbol, Symbol}, NamedTuple{(:g, :h, :k), Tuple{var"#8#12", var"#9#13", var"#10#14"}}}(:g => var"#8#12"(), :h => var"#9#13"(), :k => var"#10#14"())), @_3, f, x)::Int64
> └── return %2
> 
> julia> @code_warntype _bar(funcs.f, funcs.more_funcs[:g], funcs.more_funcs[:h], funcs.more_funcs[:k], 10)
> MethodInstance for _bar(::var"#7#11", ::var"#8#12", ::var"#9#13", ::var"#10#14", ::Int64)
> from _bar(f, g, h, k, x) in Main at /Users/cameron/projects/julia_misc/function_barrier.jl:25
> Arguments
> #self#::Core.Const(_bar)
> f::Core.Const(var"#7#11"())
> g::Core.Const(var"#8#12"())
> h::Core.Const(var"#9#13"())
> k::Core.Const(var"#10#14"())
> x::Int64
> Body::Int64
> 1 ─ %1 = (k)(x)::Int64
> │ %2 = (h)(%1)::Int64
> │ %3 = (g)(%2)::Int64
> │ %4 = (f)(%3)::Int64
> └── return %4
> 
> ```

But unfortunately we still see all the types that we don’t want to see in the stacktrace:

### `foo` stacktrace:

```julia
julia> foo(funcs, "a")
ERROR: MethodError: no method matching +(::String, ::Int64)
Stacktrace:
 [1] (::var"#10#14")(x::String)
   @ Main ~/projects/julia_misc/function_barrier.jl:30
 [2] _foo(f::var"#7#11", x::String; more_funcs::Base.Pairs{Symbol, Function, Tuple{Symbol, Symbol, Symbol}, NamedTuple{(:g, :h, :k), Tuple{var"#8#12", var"#9#13", var"#10#14"}}})
   @ Main ~/projects/julia_misc/function_barrier.jl:15
 [3] foo(fs::Funcs{var"#7#11"}, x::String)
   @ Main ~/projects/julia_misc/function_barrier.jl:9
 [4] top-level scope
   @ REPL[8]:1

```

### `bar` stacktrace:

```julia
julia> bar(funcs, "a")
ERROR: MethodError: no method matching +(::String, ::Int64)
Stacktrace:
 [1] (::var"#10#14")(x::String)
   @ Main ~/projects/julia_misc/function_barrier.jl:30
 [2] _bar(f::var"#7#11", g::var"#8#12", h::var"#9#13", k::var"#10#14", x::String)
   @ Main ~/projects/julia_misc/function_barrier.jl:25
 [3] bar(fs::Funcs{var"#7#11"}, x::String)
   @ Main ~/projects/julia_misc/function_barrier.jl:22
 [4] top-level scope
   @ REPL[9]:1

```

In fact, `foo` is actually worse because it adds all the extra type parameters of `Base.Pairs`.

---

<div class="post-metadata">

**Author:** ![CameronBieganek](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/cameronbieganek/32/6915_2.png) [@CameronBieganek](https://discourse.julialang.org/u/CameronBieganek)\
**Post date:** [March 23, 2023, 9:13pm UTC](https://discourse.julialang.org/t/differentialequations-package-kills-performance-everywhere-when-truncatedstacktraces-is-used/96417/29 "2023-03-23T21:13:35Z")

</div>

I think I figured out how to improve the printing of `ODEFunction` in stacktraces. The fact that the number of non-`nothing` fields in `ODEFunction` is highly variable indicates that what you really need is a collection, not a struct. So I’ve created an example type called `MyFunction` that wraps a named tuple. The named tuple contains only the options that are actually passed to the `MyFunction` constructor. For comparison, I also define a `SciMLFunction` type that is essentially the same as `ODEFunction`.

Here is the implementation for `SciMLFunction` and `MyFunction`:

```julia
struct SciMLFunction{iip, specialize, F, TMM, Ta, Tt,
                   TJ, JVP, VJP, JP, SP, TW, TWt,
                   TPJ, S, S2, S3, O, TCV, SYS}
    f::F
    mass_matrix::TMM
    analytic::Ta
    tgrad::Tt
    jac::TJ
    jvp::JVP
    vjp::VJP
    jac_prototype::JP
    sparsity::SP
    Wfact::TW
    Wfact_t::TWt
    paramjac::TPJ
    syms::S
    indepsym::S2
    paramsyms::S3
    observed::O
    colorvec::TCV
    sys::SYS
end

function SciMLFunction(f; mass_matrix=nothing, analytic=nothing, tgrad=nothing,
                       jac=nothing, jvp=nothing, vjp=nothing, jac_prototype=nothing,
                       sparsity=nothing, Wfact=nothing, Wfact_t=nothing,
                       paramjac=nothing, syms=nothing, indepsym=nothing,
                       paramsyms=nothing, observed=nothing, colorvec=nothing,
                       sys=nothing)
    SciMLFunction{1, 2, typeof(f), typeof(mass_matrix), typeof(analytic), typeof(tgrad), typeof(jac), typeof(jvp), typeof(vjp), typeof(jac_prototype),
    typeof(sparsity), typeof(Wfact), typeof(Wfact_t), typeof(paramjac), typeof(syms), typeof(indepsym), typeof(paramsyms),
    typeof(observed), typeof(colorvec), typeof(sys)}(f, mass_matrix, analytic, tgrad, jac, jvp, vjp, jac_prototype,
        sparsity, Wfact, Wfact_t, paramjac, syms, indepsym, paramsyms,
        observed, colorvec, sys)
end

function foo(funcs::SciMLFunction, x)
    g = isnothing(funcs.jac) ? identity : funcs.jac
    h = isnothing(funcs.jvp) ? identity : funcs.jvp
    k = isnothing(funcs.vjp) ? identity : funcs.vjp
    funcs.f(g(h(k(x))))
end

struct MyFunction{F,N,T}
    f::F
    more_funcs::NamedTuple{N,T}
end

function MyFunction(f; kwargs...)
    # Extract the named tuple from the Base.Pairs to reduce
    # the type complexity.
    MyFunction(f, values(kwargs))
end

function foo(funcs::MyFunction, x)
    g = get(funcs.more_funcs, :jac, identity)
    h = get(funcs.more_funcs, :jvp, identity)
    k = get(funcs.more_funcs, :vjp, identity)
    funcs.f(g(h(k(x))))
end

```

Now let’s define an instance of each of the two types where none of the optional arguments are passed in:

```julia
sciml_funcs = SciMLFunction(x -> x + 1)
my_funcs = MyFunction(x -> x + 1)

```

Here is a screenshot of what the `SciMLFunction` stacktrace looks like:

 ![image](https://global.discourse-cdn.com/julialang/original/3X/1/b/1bd35cfaa35291d1c7289affebd572904c81968c.png)

Here is a screenshot of what the `MyFunction` stacktrace looks like:

 ![image](https://global.discourse-cdn.com/julialang/original/3X/9/7/97f5d3bd368451d40b2cca30d2daf7b87497c641.png)

If you compare frame 2 of the stacktraces, you can see that the printing of the type `MyFunction` is much shorter than the printing of the type `SciMLFunction`. I think that’s a considerable improvement.

The same technique could be applied to other types which contain a bunch of optional fields, like `DEOptions`.

---

<div class="post-metadata">

**Author:** ![ChrisRackauckas](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/chrisrackauckas/32/77_2.png) [@ChrisRackauckas](https://discourse.julialang.org/u/ChrisRackauckas)\
**Post date:** [March 24, 2023, 10:47am UTC](https://discourse.julialang.org/t/differentialequations-package-kills-performance-everywhere-when-truncatedstacktraces-is-used/96417/30 "2023-03-24T10:47:10Z")

</div>

> [@CameronBieganek](#):
>
> Are there fields in `DEOptions` or `ODEIntegrator` that could be a `Union` type instead of a type parameter? E.g., `Union{Int, Nothing}` or `Union{Vector{Float64}, Nothing}`?
> 
> I tried out the function barrier approach that I mentioned above, and it almost works, but not quite. As expected, all of the types inside the function barrier are fully inferred. But it doesn’t help with stacktraces. It just shifts all the types from one place to another.

Union splitting blows up compile times so we have to avoid that (and in fact, internally we turn off union splitting so this would be slow).

> [@CameronBieganek](#):
>
> I think I figured out how to improve the printing of `ODEFunction` in stacktraces. The fact that the number of non-`nothing` fields in `ODEFunction` is highly variable indicates that what you really need is a collection, not a struct. So I’ve created an example type called `MyFunction` that wraps a named tuple.

This doesn’t have error checking for typos. Incorrectly labelled functions like `t_grad` instead of `tgrad` are then just not found with `has_property(x,:tgrad)`, so you get silent incorrect failures to use the function.

---

<div class="post-metadata">

**Author:** ![CameronBieganek](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/cameronbieganek/32/6915_2.png) [@CameronBieganek](https://discourse.julialang.org/u/CameronBieganek)\
**Post date:** [March 24, 2023, 12:19pm UTC](https://discourse.julialang.org/t/differentialequations-package-kills-performance-everywhere-when-truncatedstacktraces-is-used/96417/31 "2023-03-24T12:19:30Z")

</div>

> [@ChrisRackauckas](#):
>
> This doesn’t have error checking for typos. Incorrectly labelled functions like `t_grad` instead of `tgrad` are then just not found with `has_property(x,:tgrad)`, so you get silent incorrect failures to use the function.

That was just a toy example. It’s pretty easy to address your concern.

```julia
struct MyFunction{F,N,T}
    f::F
    more_funcs::NamedTuple{N,T}
end

function MyFunction(
        f;
        mass_matrix=nothing, analytic=nothing, tgrad=nothing,
        jac=nothing, jvp=nothing, vjp=nothing,
        jac_prototype=nothing, sparsity=nothing, Wfact=nothing,
        Wfact_t=nothing, paramjac=nothing, syms=nothing,
        indepsym=nothing, paramsyms=nothing, observed=nothing, 
        colorvec=nothing, sys=nothing
    )
    # This particular syntax requires Julia 1.7.
    kwargs = (;
        mass_matrix, analytic, tgrad, jac, jvp, vjp,
        jac_prototype, sparsity, Wfact, Wfact_t, paramjac,
        syms, indepsym, paramsyms, observed, colorvec, sys
    )

    more_funcs = NamedTuple(
        k => v for (k, v) in pairs(kwargs) if v !== nothing
    )
    MyFunction(f, more_funcs)
end

```

Now we get the usual `MethodError` for incorrectly typed keyword argument names:

```julia
julia> MyFunction(x -> x + 1; t_grad = x -> x + 2)
ERROR: MethodError: no method matching MyFunction(::var"#57#59"; t_grad=var"#58#60"())
Closest candidates are:
  MyFunction(::Any; mass_matrix, analytic, tgrad, jac, jvp, vjp, jac_prototype, sparsity, Wfact, Wfact_t, paramjac, syms, indepsym, paramsyms, observed, colorvec, sys) at ~/projects/julia_misc/ode_function.jl:57 got unsupported keyword argument "t_grad"
  MyFunction(::F, ::NamedTuple{N, T}) where {F, N, T} at ~/projects/julia_misc/ode_function.jl:47 got unsupported keyword argument "t_grad"
Stacktrace:
 [1] kwerr(::NamedTuple{(:t_grad,), Tuple{var"#58#60"}}, ::Type, ::Function)
   @ Base ./error.jl:165
 [2] top-level scope
   @ REPL[24]:1

```

---

<div class="post-metadata">

**Author:** ![ChrisRackauckas](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/chrisrackauckas/32/77_2.png) [@ChrisRackauckas](https://discourse.julialang.org/u/ChrisRackauckas)\
**Post date:** [March 24, 2023, 7:07pm UTC](https://discourse.julialang.org/t/differentialequations-package-kills-performance-everywhere-when-truncatedstacktraces-is-used/96417/32 "2023-03-24T19:07:35Z")

</div>

That is an interesting idea, and if a real solution wasn’t around the corner that would be okay. But it’s a very heavy way to simulate a parameterized type just because there’s no way to control stacktraces.

---

<div class="post-metadata">

**Author:** ![CameronBieganek](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/cameronbieganek/32/6915_2.png) [@CameronBieganek](https://discourse.julialang.org/u/CameronBieganek)\
**Post date:** [March 24, 2023, 7:49pm UTC](https://discourse.julialang.org/t/differentialequations-package-kills-performance-everywhere-when-truncatedstacktraces-is-used/96417/33 "2023-03-24T19:49:44Z")

</div>

I take the reverse view. I would say a type with every field parameterized is a heavy way to represent a collection of optional configuration variables. A minimally parameterized struct makes sense for holding configuration variables if all the variables have sensible and usable default values. For a case like `ODEFunction`, where most of the configuration variables do not have usable default values, and most of the variables are left unspecified, it makes more sense to store the specified variables in a collection. The number of configuration variables that are set in the `ODEFunction` constructor can vary anywhere between 0 and 18. That to me sounds like a job for a collection, not a type with 18 parameters. (Actually 20 parameters since there are also the `iip` and `specialize` parameters.)

Actually, I think the most natural way to handle this sort of scenario is to just slurp and splat `kwargs` among the various functions involved. However, I’m not sure if that could be made to work without breaking the current API.

---

<div class="post-metadata">

**Author:** ![ChrisRackauckas](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/chrisrackauckas/32/77_2.png) [@ChrisRackauckas](https://discourse.julialang.org/u/ChrisRackauckas)\
**Post date:** [March 24, 2023, 7:52pm UTC](https://discourse.julialang.org/t/differentialequations-package-kills-performance-everywhere-when-truncatedstacktraces-is-used/96417/34 "2023-03-24T19:52:26Z")

</div>

> [@CameronBieganek](#):
>
> Actually, I think the most natural way to handle this sort of scenario is to just slurp and splat `kwargs` among the various functions involved. However, I’m not sure if that could be made to work without breaking the current API.

It could via getproperty overloading.

---

<div class="post-metadata">

**Author:** ![uniment](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/uniment/32/24532_2.png) [@uniment](https://discourse.julialang.org/u/uniment)\
**Post date:** [March 25, 2023, 2:58am UTC](https://discourse.julialang.org/t/differentialequations-package-kills-performance-everywhere-when-truncatedstacktraces-is-used/96417/35 "2023-03-25T02:58:14Z")

</div>

> [@CameronBieganek](#):
>
> ```julia
> more_funcs = NamedTuple(
> k => v for (k, v) in pairs(kwargs) if v !== nothing
> )
> 
> ```

Note that this is type-unstable. Example:

```julia
julia> nt = (a=1, b=2, c=nothing);

julia> @btime NamedTuple(k=>v for (k,v) ∈ pairs($nt) if !isnothing(v))
  844.615 ns (15 allocations: 912 bytes)
(a = 1, b = 2)

```

Generally, using `k=>v` pairs to represent `NamedTuple`s loses type information.

This, however, is type-stable:

```julia
julia> Base.filter(f, nt::NamedTuple{ks}) where ks = let ks=filter(f ∘ Base.Fix1(getindex, nt), ks)
           NamedTuple{ks}(map(Base.Fix1(getindex, nt), ks))
       end
       @btime filter(!isnothing, $nt)
  1.800 ns (0 allocations: 0 bytes)
(a = 1, b = 2)

```

---

<div class="post-metadata">

**Author:** ![CameronBieganek](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/cameronbieganek/32/6915_2.png) [@CameronBieganek](https://discourse.julialang.org/u/CameronBieganek)\
**Post date:** [March 25, 2023, 3:55am UTC](https://discourse.julialang.org/t/differentialequations-package-kills-performance-everywhere-when-truncatedstacktraces-is-used/96417/36 "2023-03-25T03:55:59Z")

</div>

Nice. Though if you’re only constructing a few `ODEFunction`s, 800 nanoseconds is still pretty fast.

By the way, the less obscure version of your function also has zero allocations:

```julia
function Base.filter(f, nt::NamedTuple{ks}) where ks
    new_ks = filter(k -> f(nt[k]), ks)
    NamedTuple{new_ks}(map(k -> nt[k], new_ks))
end

```

```julia
julia> nt = (a=1, b=2, c=nothing);

julia> @btime filter(!isnothing, $nt);
  2.552 ns (0 allocations: 0 bytes)

```

Don’t make your code obscure if the readable version runs just as fast. 😉

(Of course we wouldn’t want to engage in type piracy on `filter(f, ::NamedTuple)`, but that’s easily fixed.)

---

<div class="post-metadata">

**Author:** ![uniment](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/uniment/32/24532_2.png) [@uniment](https://discourse.julialang.org/u/uniment)\
**Post date:** [March 26, 2023, 5:24am UTC](https://discourse.julialang.org/t/differentialequations-package-kills-performance-everywhere-when-truncatedstacktraces-is-used/96417/37 "2023-03-26T05:24:53Z")

</div>

> [@CameronBieganek](#):
>
> we wouldn’t want to engage in type piracy on `filter(f, ::NamedTuple)`, but that’s easily fixed.

Would you like to open the PR?

---

<div class="post-metadata">

**Author:** ![CameronBieganek](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/cameronbieganek/32/6915_2.png) [@CameronBieganek](https://discourse.julialang.org/u/CameronBieganek)\
**Post date:** [March 26, 2023, 1:28pm UTC](https://discourse.julialang.org/t/differentialequations-package-kills-performance-everywhere-when-truncatedstacktraces-is-used/96417/38 "2023-03-26T13:28:01Z")

</div>

Oh, I meant in this case we would just define a new function, like

```julia
trim(nt::NamedTuple) = nt[filter(k -> !isnothing(nt[k]), keys(nt))]

```

```julia
julia> nt = (a=1, b=2, c=nothing);

julia> trim(nt)
(a = 1, b = 2)

julia> @btime trim($nt);
  2.552 ns (0 allocations: 0 bytes)

```

But I suppose it probably would be nice to have `filter` on named tuples in Base. Feel free to open a PR if you like. 🙂

---

<div class="post-metadata">

**Author:** ![ParadaCarleton](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/paradacarleton/32/20005_2.png) [@ParadaCarleton](https://discourse.julialang.org/u/ParadaCarleton)\
**Post date:** [March 27, 2023, 3:53pm UTC](https://discourse.julialang.org/t/differentialequations-package-kills-performance-everywhere-when-truncatedstacktraces-is-used/96417/39 "2023-03-27T15:53:08Z")

</div>

@ChrisRackauckas try ConcreteStructs.jl with `terse`. It automatically avoids printing types in the stacktraces (it adds “quiet” types).

---

<div class="post-metadata">

**Author:** ![ChrisRackauckas](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/chrisrackauckas/32/77_2.png) [@ChrisRackauckas](https://discourse.julialang.org/u/ChrisRackauckas)\
**Post date:** [March 27, 2023, 4:20pm UTC](https://discourse.julialang.org/t/differentialequations-package-kills-performance-everywhere-when-truncatedstacktraces-is-used/96417/40 "2023-03-27T16:20:23Z")

</div>

That has exactly the same issue.

[Previous page](https://discourse.julialang.org/t/differentialequations-package-kills-performance-everywhere-when-truncatedstacktraces-is-used/96417.md?page=1)

[Next page](https://discourse.julialang.org/t/differentialequations-package-kills-performance-everywhere-when-truncatedstacktraces-is-used/96417.md?page=3)
