Preliminary: DiffResult objects must always be realiased when updated, as mentioned in the docs
https://juliadiff.org/DiffResults.jl/stable/#DiffResults.value!
In most cases it is unnecessary, because the result object itself gets mutated. However, it causes a heap allocation to create the mutable struct defined here:
|
mutable struct MutableDiffResult{O,V,D<:Tuple} <: DiffResult{O,V,D} |
|
value::V |
|
derivs::D # ith element = ith-order derivative |
|
function MutableDiffResult(value::V, derivs::NTuple{O,Any}) where {O,V} |
|
return new{O,V,typeof(derivs)}(value, derivs) |
|
end |
|
end |
Here's a proof of concept: instead of the current code
|
value!(r::MutableDiffResult, x::Number) = (r.value = x; return r) |
|
value!(r::MutableDiffResult, x::AbstractArray) = (copyto!(value(r), x); return r) |
we could instead return a new object every time
value!(r::MutableDiffResult, x::Number) = MutableDiffResult(x, r.derivs)
value!(r::MutableDiffResult, x::AbstractArray) = (copyto!(value(r), x); return r)
- Would it decrease or increase the performance of the update? Don't know
- Would it be considered a breaking change? Perhaps (see below)
- Would it break stuff in practice? Definitely (see below)
Preliminary: DiffResult objects must always be realiased when updated, as mentioned in the docs
https://juliadiff.org/DiffResults.jl/stable/#DiffResults.value!
In most cases it is unnecessary, because the result object itself gets mutated. However, it causes a heap allocation to create the mutable struct defined here:
DiffResults.jl/src/DiffResults.jl
Lines 19 to 25 in 724a23d
Here's a proof of concept: instead of the current code
DiffResults.jl/src/DiffResults.jl
Lines 156 to 157 in 724a23d
we could instead return a new object every time