Skip to content

Commit b7a38f9

Browse files
Stop precomputing chunk sizes into a nonconcrete array (#708)
This prevents constant propagation of the length of a static array. Alternative to #707 Co-authored-by: David Widmann <[email protected]>
1 parent d66a379 commit b7a38f9

File tree

3 files changed

+8
-5
lines changed

3 files changed

+8
-5
lines changed

src/prelude.jl

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -14,12 +14,9 @@ const BINARY_PREDICATES = Symbol[:isequal, :isless, :<, :>, :(==), :(!=), :(<=),
1414

1515
struct Chunk{N} end
1616

17-
const CHUNKS = [Chunk{i}() for i in 1:DEFAULT_CHUNK_THRESHOLD]
18-
1917
function Chunk(input_length::Integer, threshold::Integer = DEFAULT_CHUNK_THRESHOLD)
2018
N = pickchunksize(input_length, threshold)
21-
0 < N <= DEFAULT_CHUNK_THRESHOLD && return CHUNKS[N]
22-
return Chunk{N}()
19+
Base.@nif 12 d->(N == d) d->(Chunk{d}()) d->(Chunk{N}())
2320
end
2421

2522
function Chunk(x::AbstractArray, threshold::Integer = DEFAULT_CHUNK_THRESHOLD)

test/GradientTest.jl

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -135,6 +135,9 @@ end
135135
@test DiffResults.gradient(sresult1) == DiffResults.gradient(result)
136136
@test DiffResults.gradient(sresult2) == DiffResults.gradient(result)
137137
@test DiffResults.gradient(sresult3) == DiffResults.gradient(result)
138+
139+
# make sure this is not a source of type instability
140+
@inferred ForwardDiff.GradientConfig(f, sx)
138141
end
139142

140143
@testset "exponential function at base zero" begin

test/JacobianTest.jl

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -222,6 +222,9 @@ for T in (StaticArrays.SArray, StaticArrays.MArray)
222222
@test DiffResults.jacobian(sresult1) == DiffResults.jacobian(result)
223223
@test DiffResults.jacobian(sresult2) == DiffResults.jacobian(result)
224224
@test DiffResults.jacobian(sresult3) == DiffResults.jacobian(result)
225+
226+
# make sure this is not a source of type instability
227+
@inferred ForwardDiff.JacobianConfig(f, sx)
225228
end
226229

227230
@testset "dimension errors for jacobian" begin
@@ -233,7 +236,7 @@ end
233236
@testset "eigen" begin
234237
@test ForwardDiff.jacobian(x -> eigvals(SymTridiagonal(x, x[1:end-1])), [1.,2.]) [(1 - 3/sqrt(5))/2 (1 - 1/sqrt(5))/2 ; (1 + 3/sqrt(5))/2 (1 + 1/sqrt(5))/2]
235238
@test ForwardDiff.jacobian(x -> eigvals(Symmetric(x*x')), [1.,2.]) [0 0; 2 4]
236-
239+
237240
x0 = [1.0, 2.0];
238241
ev1(x) = eigen(Symmetric(x*x')).vectors[:,1]
239242
@test ForwardDiff.jacobian(ev1, x0) Calculus.finite_difference_jacobian(ev1, x0)

0 commit comments

Comments
 (0)