Skip to content

Does MutableDiffResult have to exist at all? #26

@gdalle

Description

@gdalle

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)

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions