Skip to content

Commit acfeb8a

Browse files
asinghvi17claude
andcommitted
fix: DefaultCache trim compatibility
- Eagerly initialize all 6 sub-caches in the constructor instead of lazily via init_ith_default_cache, so the trimmer can statically resolve all alg_cache calls. - Add @generated _reset_alg_opts_default! that enumerates (old, new) algorithm index pairs at compile time, replacing dynamic tuple indexing that the trimmer cannot resolve. Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
1 parent 1238519 commit acfeb8a

2 files changed

Lines changed: 37 additions & 45 deletions

File tree

lib/OrdinaryDiffEqCore/src/caches/basic_caches.jl

Lines changed: 8 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -99,25 +99,14 @@ function alg_cache(
9999
args, alg.choice_function, 1, u
100100
)
101101
algs = alg.algs
102-
# If the type is a bitstype we need to initialize it correctly here since isdefined will always return true.
103-
if isbitstype(T1)
104-
cache.cache1 = alg_cache(algs[1], args...)
105-
end
106-
if isbitstype(T2)
107-
cache.cache2 = alg_cache(algs[2], args...)
108-
end
109-
if isbitstype(T3)
110-
cache.cache3 = alg_cache(algs[3], args...)
111-
end
112-
if isbitstype(T4)
113-
cache.cache4 = alg_cache(algs[4], args...)
114-
end
115-
if isbitstype(T5)
116-
cache.cache5 = alg_cache(algs[5], args...)
117-
end
118-
if isbitstype(T6)
119-
cache.cache6 = alg_cache(algs[6], args...)
120-
end
102+
# Eagerly initialize all caches for trim compatibility (the trimmer cannot
103+
# resolve lazy alg_cache calls through splatted args).
104+
cache.cache1 = alg_cache(algs[1], args...)
105+
cache.cache2 = alg_cache(algs[2], args...)
106+
cache.cache3 = alg_cache(algs[3], args...)
107+
cache.cache4 = alg_cache(algs[4], args...)
108+
cache.cache5 = alg_cache(algs[5], args...)
109+
cache.cache6 = alg_cache(algs[6], args...)
121110
return cache
122111
end
123112

lib/OrdinaryDiffEqCore/src/perform_step/composite_perform_step.jl

Lines changed: 29 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -1,29 +1,8 @@
1+
# All caches are now eagerly initialized in alg_cache for
2+
# CompositeAlgorithm{CS, Tuple{A1..A6}}, so this is a no-op.
3+
# Kept for API compatibility with callers.
14
function init_ith_default_cache(cache::DefaultCache, algs, i)
2-
return if i == 1
3-
if !isdefined(cache, :cache1)
4-
cache.cache1 = alg_cache(algs[1], cache.args...)
5-
end
6-
elseif i == 2
7-
if !isdefined(cache, :cache2)
8-
cache.cache2 = alg_cache(algs[2], cache.args...)
9-
end
10-
elseif i == 3
11-
if !isdefined(cache, :cache3)
12-
cache.cache3 = alg_cache(algs[3], cache.args...)
13-
end
14-
elseif i == 4
15-
if !isdefined(cache, :cache4)
16-
cache.cache4 = alg_cache(algs[4], cache.args...)
17-
end
18-
elseif i == 5
19-
if !isdefined(cache, :cache5)
20-
cache.cache5 = alg_cache(algs[5], cache.args...)
21-
end
22-
elseif i == 6
23-
if !isdefined(cache, :cache6)
24-
cache.cache6 = alg_cache(algs[6], cache.args...)
25-
end
26-
end
5+
return nothing
276
end
287

298
function initialize!(integrator, cache::DefaultCache)
@@ -274,11 +253,35 @@ function choose_algorithm!(integrator, cache::DefaultCache)
274253
old_cache = cache.cache6
275254
end
276255

277-
reset_alg_dependent_opts!(integrator, algs[old_current], algs[new_current])
256+
# Use static tuple indexing for trim compatibility (the trimmer
257+
# cannot resolve dynamic algs[runtime_int] on a heterogeneous tuple).
258+
_reset_alg_opts_default!(integrator, algs, old_current, new_current)
278259
transfer_cache!(integrator, old_cache, new_cache)
279260
end
280261
end
281262

263+
# Trim-safe version of reset_alg_dependent_opts! for DefaultCache:
264+
# enumerates all (old, new) index pairs at compile time so the trimmer
265+
# sees only statically-resolved tuple indexing.
266+
@generated function _reset_alg_opts_default!(
267+
integrator, algs::T, old_current, new_current
268+
) where {T <: Tuple}
269+
n = length(T.types)
270+
expr = Expr(:block)
271+
for i in 1:n
272+
for j in 1:n
273+
i == j && continue
274+
push!(expr.args, quote
275+
if old_current == $i && new_current == $j
276+
return reset_alg_dependent_opts!(integrator, algs[$i], algs[$j])
277+
end
278+
end)
279+
end
280+
end
281+
push!(expr.args, :(return nothing))
282+
return expr
283+
end
284+
282285
"""
283286
Generic fallback for composite caches that are not CompositeCache or DefaultCache
284287
(e.g. StochasticCompositeCache). Uses the `is_composite_cache` trait.

0 commit comments

Comments
 (0)