Skip to content

Commit 5a3dda9

Browse files
Fold AbstractMatrix optimization into general method as one-line change
Address review: instead of a separate dispatch method for AbstractMatrix, use a conditional in the existing method to skip reshape when result is already a matrix. This minimizes the diff while preserving the allocation fix under --check-bounds=yes. Co-Authored-By: Chris Rackauckas <accounts@chrisrackauckas.com> Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
1 parent 0964e46 commit 5a3dda9

1 file changed

Lines changed: 1 addition & 12 deletions

File tree

src/jacobian.jl

Lines changed: 1 addition & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -92,19 +92,8 @@ jacobian(f, x::Real) = throw(DimensionMismatch("jacobian(f, x) expects that x is
9292
# result extraction #
9393
#####################
9494

95-
# Specialized method for AbstractMatrix: no reshape needed, avoids ReshapedArray allocation
96-
# that cannot be elided under --check-bounds=yes.
97-
function extract_jacobian!(::Type{T}, result::AbstractMatrix, ydual::AbstractArray, n) where {T}
98-
ydual_reshaped = vec(ydual)
99-
# Use closure to avoid GPU broadcasting with Type
100-
partials_wrap(ydual, nrange) = partials(T, ydual, nrange)
101-
result .= partials_wrap.(ydual_reshaped, transpose(1:n))
102-
return result
103-
end
104-
105-
# General method for non-matrix arrays: reshape unconditionally (type-stable).
10695
function extract_jacobian!(::Type{T}, result::AbstractArray, ydual::AbstractArray, n) where {T}
107-
out_reshaped = reshape(result, length(ydual), n)
96+
out_reshaped = result isa AbstractMatrix ? result : reshape(result, length(ydual), n)
10897
ydual_reshaped = vec(ydual)
10998
# Use closure to avoid GPU broadcasting with Type
11099
partials_wrap(ydual, nrange) = partials(T, ydual, nrange)

0 commit comments

Comments
 (0)