From bba8b54005c9793750667ffe552f725fc460bba3 Mon Sep 17 00:00:00 2001 From: Chris Elrod Date: Thu, 13 Jan 2022 14:28:20 -0500 Subject: [PATCH 1/2] Avoid recursion heuristic? --- Project.toml | 2 +- src/dual.jl | 6 +++--- 2 files changed, 4 insertions(+), 4 deletions(-) diff --git a/Project.toml b/Project.toml index ece577e5..14233f29 100644 --- a/Project.toml +++ b/Project.toml @@ -1,6 +1,6 @@ name = "ForwardDiff" uuid = "f6369f11-7733-5829-9624-2563aa707210" -version = "0.10.24" +version = "0.10.25" [deps] CommonSubexpressions = "bbf7d656-a473-5ed7-a52c-81e309532950" diff --git a/src/dual.jl b/src/dual.jl index 03330c00..31a7ce99 100644 --- a/src/dual.jl +++ b/src/dual.jl @@ -380,9 +380,9 @@ for R in (Irrational, Real, BigFloat, Bool) end end -Base.convert(::Type{Dual{T,V,N}}, d::Dual{T}) where {T,V,N} = Dual{T}(convert(V, value(d)), convert(Partials{N,V}, partials(d))) -Base.convert(::Type{Dual{T,V,N}}, x) where {T,V,N} = Dual{T}(convert(V, x), zero(Partials{N,V})) -Base.convert(::Type{Dual{T,V,N}}, x::Number) where {T,V,N} = Dual{T}(convert(V, x), zero(Partials{N,V})) +@inline Base.convert(::Type{Dual{T,V,N}}, d::Dual{T}) where {T,V,N} = Dual{T}(V(value(d)), convert(Partials{N,V}, partials(d))) +@inline Base.convert(::Type{Dual{T,V,N}}, x) where {T,V,N} = Dual{T}(V(x), zero(Partials{N,V})) +@inline Base.convert(::Type{Dual{T,V,N}}, x::Number) where {T,V,N} = Dual{T}(V(x), zero(Partials{N,V})) Base.convert(::Type{D}, d::D) where {D<:Dual} = d Base.float(::Type{Dual{T,V,N}}) where {T,V,N} = Dual{T,float(V),N} From 8089636f5dbf134a4687f44d483588727dc50140 Mon Sep 17 00:00:00 2001 From: Chris Elrod Date: Thu, 13 Jan 2022 15:47:28 -0500 Subject: [PATCH 2/2] Add allocation test to check for this regression. --- test/AllocationsTest.jl | 9 ++++++++- 1 file changed, 8 insertions(+), 1 deletion(-) diff --git a/test/AllocationsTest.jl b/test/AllocationsTest.jl index a16a0103..2a0075a7 100644 --- a/test/AllocationsTest.jl +++ b/test/AllocationsTest.jl @@ -4,6 +4,8 @@ using ForwardDiff include(joinpath(dirname(@__FILE__), "utils.jl")) +convert_test_574() = convert(ForwardDiff.Dual{Nothing,ForwardDiff.Dual{Nothing,ForwardDiff.Dual{Nothing,Float64,8},4},2}, 1.3) + @testset "Test seed! allocations" begin x = rand(1000) cfg = ForwardDiff.GradientConfig(nothing, x) @@ -28,6 +30,11 @@ include(joinpath(dirname(@__FILE__), "utils.jl")) alloc = @allocated ForwardDiff.seed!(duals, x, index, seed) alloc = @allocated ForwardDiff.seed!(duals, x, index, seed) @test alloc == 0 + + alloc = @allocated convert_test_574() + alloc = @allocated convert_test_574() + @test alloc == 0 + end -end \ No newline at end of file +end