From ec1e7a58de7e11407821b1f20e06d4cedeeb2c57 Mon Sep 17 00:00:00 2001 From: daviehh <25255906+daviehh@users.noreply.github.com> Date: Sun, 17 Aug 2025 21:44:40 -0400 Subject: [PATCH 1/3] remove duplicate code --- src/PreallocationTools.jl | 13 ++++--------- 1 file changed, 4 insertions(+), 9 deletions(-) diff --git a/src/PreallocationTools.jl b/src/PreallocationTools.jl index e46b416..58623d3 100644 --- a/src/PreallocationTools.jl +++ b/src/PreallocationTools.jl @@ -57,14 +57,7 @@ This function enables seamless switching between regular and automatic different computations without manual cache management. """ function get_tmp(dc::FixedSizeDiffCache, u::Union{Number, AbstractArray}) - if promote_type(eltype(dc.du), eltype(u)) <: eltype(dc.du) - dc.du - else - if length(dc.du) > length(dc.any_du) - resize!(dc.any_du, length(dc.du)) - end - _restructure(dc.du, dc.any_du) - end + get_tmp(dc, eltype(u)) end function get_tmp(dc::FixedSizeDiffCache, ::Type{T}) where {T <: Number} @@ -171,10 +164,12 @@ elements and issues a one-time warning suggesting an appropriate chunk size for optimal performance. ## Arguments + - `dc`: The `DiffCache` object to enlarge - `nelem`: The new required number of elements ## Notes + The warning is shown only once per `DiffCache` instance to avoid spam. For optimal performance in production code, pre-allocate with the suggested chunk size to avoid runtime allocations. @@ -273,7 +268,7 @@ function Base.resize!(dc::DiffCache, n::Integer) else throw(ArgumentError("resize! is only supported for DiffCache with vector arrays, got $(typeof(dc.du))")) end - # dual_du is often pre-allocated for ForwardDiff dual numbers, + # dual_du is often pre-allocated for ForwardDiff dual numbers, # and may need special handling based on chunk size # Only resize if it's a vector if dc.dual_du isa AbstractVector From e10c6ce5c0d32d950a4abfd22758adb8c734f360 Mon Sep 17 00:00:00 2001 From: daviehh <25255906+daviehh@users.noreply.github.com> Date: Mon, 18 Aug 2025 15:39:17 -0400 Subject: [PATCH 2/3] format --- ext/PreallocationToolsForwardDiffExt.jl | 20 +++++++++++++------- 1 file changed, 13 insertions(+), 7 deletions(-) diff --git a/ext/PreallocationToolsForwardDiffExt.jl b/ext/PreallocationToolsForwardDiffExt.jl index 91e307f..bee0032 100644 --- a/ext/PreallocationToolsForwardDiffExt.jl +++ b/ext/PreallocationToolsForwardDiffExt.jl @@ -6,7 +6,7 @@ using ArrayInterface using Adapt function PreallocationTools.dualarraycreator(u::AbstractArray{T}, siz, - ::Type{Val{chunk_size}}) where {T, chunk_size} + ::Type{Val{chunk_size}}) where {T, chunk_size} ArrayInterface.restructure(u, zeros(ForwardDiff.Dual{Nothing, T, chunk_size}, siz...)) @@ -18,7 +18,8 @@ PreallocationTools.forwarddiff_compat_chunk_size(x::Int) = ForwardDiff.pickchunk PreallocationTools.chunksize(::Type{ForwardDiff.Dual{T, V, N}}) where {T, V, N} = N # Define get_tmp methods for ForwardDiff.Dual types -function PreallocationTools.get_tmp(dc::PreallocationTools.FixedSizeDiffCache, u::T) where {T <: ForwardDiff.Dual} +function PreallocationTools.get_tmp( + dc::PreallocationTools.FixedSizeDiffCache, u::T) where {T <: ForwardDiff.Dual} x = reinterpret(T, dc.dual_du) if PreallocationTools.chunksize(T) === PreallocationTools.chunksize(eltype(dc.dual_du)) x @@ -27,7 +28,8 @@ function PreallocationTools.get_tmp(dc::PreallocationTools.FixedSizeDiffCache, u end end -function PreallocationTools.get_tmp(dc::PreallocationTools.FixedSizeDiffCache, u::Type{T}) where {T <: ForwardDiff.Dual} +function PreallocationTools.get_tmp( + dc::PreallocationTools.FixedSizeDiffCache, u::Type{T}) where {T <: ForwardDiff.Dual} x = reinterpret(T, dc.dual_du) if PreallocationTools.chunksize(T) === PreallocationTools.chunksize(eltype(dc.dual_du)) x @@ -36,7 +38,8 @@ function PreallocationTools.get_tmp(dc::PreallocationTools.FixedSizeDiffCache, u end end -function PreallocationTools.get_tmp(dc::PreallocationTools.FixedSizeDiffCache, u::AbstractArray{T}) where {T <: ForwardDiff.Dual} +function PreallocationTools.get_tmp(dc::PreallocationTools.FixedSizeDiffCache, + u::AbstractArray{T}) where {T <: ForwardDiff.Dual} x = reinterpret(T, dc.dual_du) if PreallocationTools.chunksize(T) === PreallocationTools.chunksize(eltype(dc.dual_du)) x @@ -45,7 +48,8 @@ function PreallocationTools.get_tmp(dc::PreallocationTools.FixedSizeDiffCache, u end end -function PreallocationTools.get_tmp(dc::PreallocationTools.DiffCache, u::T) where {T <: ForwardDiff.Dual} +function PreallocationTools.get_tmp(dc::PreallocationTools.DiffCache, u::T) where {T <: + ForwardDiff.Dual} if isbitstype(T) nelem = div(sizeof(T), sizeof(eltype(dc.dual_du))) * length(dc.du) if nelem > length(dc.dual_du) @@ -57,7 +61,8 @@ function PreallocationTools.get_tmp(dc::PreallocationTools.DiffCache, u::T) wher end end -function PreallocationTools.get_tmp(dc::PreallocationTools.DiffCache, ::Type{T}) where {T <: ForwardDiff.Dual} +function PreallocationTools.get_tmp(dc::PreallocationTools.DiffCache, ::Type{T}) where {T <: + ForwardDiff.Dual} if isbitstype(T) nelem = div(sizeof(T), sizeof(eltype(dc.dual_du))) * length(dc.du) if nelem > length(dc.dual_du) @@ -69,7 +74,8 @@ function PreallocationTools.get_tmp(dc::PreallocationTools.DiffCache, ::Type{T}) end end -function PreallocationTools.get_tmp(dc::PreallocationTools.DiffCache, u::AbstractArray{T}) where {T <: ForwardDiff.Dual} +function PreallocationTools.get_tmp( + dc::PreallocationTools.DiffCache, u::AbstractArray{T}) where {T <: ForwardDiff.Dual} if isbitstype(T) nelem = div(sizeof(T), sizeof(eltype(dc.dual_du))) * length(dc.du) if nelem > length(dc.dual_du) From b233dbdf095fcd80390c64600fa33a2a3b1b15de Mon Sep 17 00:00:00 2001 From: daviehh <25255906+daviehh@users.noreply.github.com> Date: Fri, 22 Aug 2025 12:28:07 -0400 Subject: [PATCH 3/3] Revert "format" This reverts commit e10c6ce5c0d32d950a4abfd22758adb8c734f360. --- ext/PreallocationToolsForwardDiffExt.jl | 20 +++++++------------- 1 file changed, 7 insertions(+), 13 deletions(-) diff --git a/ext/PreallocationToolsForwardDiffExt.jl b/ext/PreallocationToolsForwardDiffExt.jl index bee0032..91e307f 100644 --- a/ext/PreallocationToolsForwardDiffExt.jl +++ b/ext/PreallocationToolsForwardDiffExt.jl @@ -6,7 +6,7 @@ using ArrayInterface using Adapt function PreallocationTools.dualarraycreator(u::AbstractArray{T}, siz, - ::Type{Val{chunk_size}}) where {T, chunk_size} + ::Type{Val{chunk_size}}) where {T, chunk_size} ArrayInterface.restructure(u, zeros(ForwardDiff.Dual{Nothing, T, chunk_size}, siz...)) @@ -18,8 +18,7 @@ PreallocationTools.forwarddiff_compat_chunk_size(x::Int) = ForwardDiff.pickchunk PreallocationTools.chunksize(::Type{ForwardDiff.Dual{T, V, N}}) where {T, V, N} = N # Define get_tmp methods for ForwardDiff.Dual types -function PreallocationTools.get_tmp( - dc::PreallocationTools.FixedSizeDiffCache, u::T) where {T <: ForwardDiff.Dual} +function PreallocationTools.get_tmp(dc::PreallocationTools.FixedSizeDiffCache, u::T) where {T <: ForwardDiff.Dual} x = reinterpret(T, dc.dual_du) if PreallocationTools.chunksize(T) === PreallocationTools.chunksize(eltype(dc.dual_du)) x @@ -28,8 +27,7 @@ function PreallocationTools.get_tmp( end end -function PreallocationTools.get_tmp( - dc::PreallocationTools.FixedSizeDiffCache, u::Type{T}) where {T <: ForwardDiff.Dual} +function PreallocationTools.get_tmp(dc::PreallocationTools.FixedSizeDiffCache, u::Type{T}) where {T <: ForwardDiff.Dual} x = reinterpret(T, dc.dual_du) if PreallocationTools.chunksize(T) === PreallocationTools.chunksize(eltype(dc.dual_du)) x @@ -38,8 +36,7 @@ function PreallocationTools.get_tmp( end end -function PreallocationTools.get_tmp(dc::PreallocationTools.FixedSizeDiffCache, - u::AbstractArray{T}) where {T <: ForwardDiff.Dual} +function PreallocationTools.get_tmp(dc::PreallocationTools.FixedSizeDiffCache, u::AbstractArray{T}) where {T <: ForwardDiff.Dual} x = reinterpret(T, dc.dual_du) if PreallocationTools.chunksize(T) === PreallocationTools.chunksize(eltype(dc.dual_du)) x @@ -48,8 +45,7 @@ function PreallocationTools.get_tmp(dc::PreallocationTools.FixedSizeDiffCache, end end -function PreallocationTools.get_tmp(dc::PreallocationTools.DiffCache, u::T) where {T <: - ForwardDiff.Dual} +function PreallocationTools.get_tmp(dc::PreallocationTools.DiffCache, u::T) where {T <: ForwardDiff.Dual} if isbitstype(T) nelem = div(sizeof(T), sizeof(eltype(dc.dual_du))) * length(dc.du) if nelem > length(dc.dual_du) @@ -61,8 +57,7 @@ function PreallocationTools.get_tmp(dc::PreallocationTools.DiffCache, u::T) wher end end -function PreallocationTools.get_tmp(dc::PreallocationTools.DiffCache, ::Type{T}) where {T <: - ForwardDiff.Dual} +function PreallocationTools.get_tmp(dc::PreallocationTools.DiffCache, ::Type{T}) where {T <: ForwardDiff.Dual} if isbitstype(T) nelem = div(sizeof(T), sizeof(eltype(dc.dual_du))) * length(dc.du) if nelem > length(dc.dual_du) @@ -74,8 +69,7 @@ function PreallocationTools.get_tmp(dc::PreallocationTools.DiffCache, ::Type{T}) end end -function PreallocationTools.get_tmp( - dc::PreallocationTools.DiffCache, u::AbstractArray{T}) where {T <: ForwardDiff.Dual} +function PreallocationTools.get_tmp(dc::PreallocationTools.DiffCache, u::AbstractArray{T}) where {T <: ForwardDiff.Dual} if isbitstype(T) nelem = div(sizeof(T), sizeof(eltype(dc.dual_du))) * length(dc.du) if nelem > length(dc.dual_du)