Skip to content

Unroll CompositeControllerCache dispatch (non-stiff Default step! allocation-free) - #4647

Merged
ChrisRackauckas merged 5 commits into
SciML:masterfrom
ChrisRackauckas-Claude:composite-controller-dispatch
Sep 27, 2026
Merged

ChrisRackauckas merged 5 commits into
SciML:masterfrom
ChrisRackauckas-Claude:composite-controller-dispatch

Conversation

@ChrisRackauckas-Claude

@ChrisRackauckas-Claude ChrisRackauckas-Claude commented Sep 26, 2026 •

Copy link
Copy Markdown
Member

What changed and why

CompositeControllerCache methods indexed the heterogeneous caches tuple (and alg.algs) with a runtime current index. That is type-unstable, so every Default / Composite step paid dynamic-dispatch allocations in accept_step_controller, stepsize_controller!, and step_accept_controller!. This PR replaces those wrappers with @generated unrolled if current == i dispatch (factored through one @eval loop over (fname, extra_args)), matching the existing pattern in generic_dense.jl / DefaultCache perform_step!.

Separately, on Julia LTS (1.10), alg_extrapolates(::CompositeAlgorithm) used any(alg_extrapolates.(alg.algs)), which materializes a Bool NTuple every step (~3184 B). This is now any(alg_extrapolates, alg.algs) (same form as alg_order), so LTS non-stiff Default step! is also 0 B.

A regression test asserts non-stiff DefaultODEAlgorithm step! allocates 0 bytes after warm-up. Stiff Default steps still allocate ~3.9–4.9 KB via unwrap_alg (alg_utils.jl:958); that remaining allocation is out of scope for this PR.

Diagnosis (what the profile showed)

Julia 1.12 — Profile.Allocs on step!(DefaultODEAlgorithm()) after warm-up (sample_rate=1.0, 20 steps):

Total alloc samples: 140
Allocs with controllers.jl in stack: 120
  40  stepsize_controller! @ controllers.jl:1304
  40  accept_step_controller @ controllers.jl:1295
  40  step_accept_controller! @ controllers.jl:1313

step! bytes: Tsit5=0, Default=1536. After controller unroll: Default=0 (non-stiff).

Julia LTS 1.10.12 — after the controller fix, CI still failed with 3184 == 0. Profile.Allocs attributed the remaining bytes to broadcast in alg_extrapolates:

alg_extrapolates @ alg_utils.jl:652
  → update_uprev! @ integrator_utils.jl:1162
  → apply_step! / loopheader! / _step!
alg_extrapolates alone bytes=3184
any(f, algs) bytes=0
broadcast bytes=3184

LTS step! bytes: master=4592, PR (controllers only)=3200, PR (+ alg_extrapolates fix)=0.

Verification

  • Runic on changed files (exit 0)
  • typos on changed files (exit 0)
  • OrdinaryDiffEqDefault GROUP=Core on Julia 1.12: 57/57 passed (includes allocation test)
  • OrdinaryDiffEqDefault default_solver_tests on Julia LTS: allocation-free Pass; interpolation testsets Pass
  • Behaviour equivalence: 1.12 golden fingerprints 12/12; LTS master vs PR fingerprints identical (Lorenz/VdP)

Failing-before / passing-after

Julia 1.12 — before (master controllers.jl):

default_step_bytes() = 1536
DefaultODEAlgorithm step! allocation-free: Test Failed
  Expression: bytes == 0
   Evaluated: 1536 == 0

Julia 1.12 — after (this PR):

default_step_bytes() = 0
Test Summary:                             | Pass  Total  Time
DefaultODEAlgorithm step! allocation-free |    1      1  0.0s

Julia LTS — before (broadcast alg_extrapolates, matches CI):

LTS FAIL-BEFORE (broadcast) default_step_bytes() = 3184
  Expression: b == 0
   Evaluated: 3184 == 0

Julia LTS — after (any(f, itr)):

default_step_bytes() = 0
Test Summary:                             | Pass  Total  Time
DefaultODEAlgorithm step! allocation-free |    1      1  1.3s

Before/after numbers

Pinned and interleaved review measurements (JULIA_NUM_THREADS=1, pinned core):

Case Before → After
Default VdP min (μs) 3742–5789 → 2979–3748
Default VdP allocs 49569 → 31703
Default Lorenz allocs 8268 → 263
ROBER allocs 1678 → 1059

step! after warm-up (non-stiff Default Lorenz): 1.12: 1536 → 0 B; LTS: 4592 → 0 B.

Behaviour-equivalence check

Fingerprints identical before/after for Default on Lorenz and Van der Pol (μ=1e6) on each Julia version:

  • 1.12: Lorenz naccept=1298, nreject=34, nf=7997, alg_choice=[1,3], switch_idxs=[1,524,527]; VdP naccept=1488, nreject=564, nf=10479, same switches — 12/12 match

  • LTS: master and PR fingerprints bitwise-identical (Lorenz naccept=1308 / alg_choice=[1] differs from 1.12 FP path, not from this PR)

  • Independent review: Claude Code (Opus 5.5), final pass at 8347567: MERGE, low risk, high confidence. 22 ODE composite cases plus a DDE are bitwise identical to master; code_typed shows the unrolled dispatch is statically resolved; Default VdP is faster in every pinned round. On Julia 1.10.12 the test fails on master (4576 == 0) and passes at head, and Default Lorenz step! drops from 8689 B/step (master) to 0. The LTS-only alg_extrapolates broadcast fix is verified semantically identical.

What was not verified

  • GPU / ModelingToolkit groups
  • Full monorepo OrdinaryDiffEq umbrella test suite
  • Fixing stiff-path unwrap_alg allocations (explicitly out of scope)
  • Timing comparisons can be noisy under heavy machine load; allocation counts are the reliable signal

Please ignore until reviewed by @ChrisRackauckas.

Risk assessment

  • Risk: Low — changes hot-path dispatch for all CompositeControllerCache users; alg_extrapolates form is semantically identical; behaviour verified bitwise-identical
  • Blast radius: every CompositeAlgorithm and default-solver user
  • Evidence: Profile.Allocs on 1.12 (controllers) and LTS (alg_extrapolates broadcast); before/after step! 1536→0 (1.12) and 4592→0 (LTS); behaviour fingerprints match; Core+Default green on both versions; independent review confirmed
  • Independent review: Claude Code (Opus 5.5): MERGE on Julia 1.12, low risk, high confidence; bitwise-identical on 23 composite cases + DDE, not slower anywhere; asked for dedup / narrower claim / comment removal (done in 3bb5932). LTS CI red was the remaining alg_extrapolates broadcast (fixed in 8347567).
  • Merge: low-risk candidate. OrdinaryDiffEqDefault Core passes on Julia 1, LTS and pre, as do QA and downgrade; the red jobs are the known master-red set.

🤖 Generated with Cursor Agent (model: auto), orchestrated by Claude Code (claude-opus-5-5[1m]) session https://claude.ai/code/session_01LPHREnnonfLg1VcE1EJovv

Made with Cursor

Risk assessment

  • Risk: low
  • Blast radius: Default and composite solver controller dispatch, including rejection, Newton handling, and controller accessors. No public signatures changed or tests weakened. T.types accesses Julia’s internal type representation. Undisclosed behavioral details: any(f, tuple) short-circuits trait evaluation, and invalid cache indices now explicitly throw strings; neither establishes a supported-public-API break requiring a major release.
  • Evidence: Head 83475670ae4701b2c712d39e4b0b4c14d8d3c8d3: 256 successful, seven failed, three skipped checks. All seven failures are pre-existing; no test regression identified. SciMLSensitivity.jl/Core1/1 / Downstream Tests - Core1 has the same Mooncake Any constructor error on head and master. downgrade-sublibraries / test (lib/DiffEqBase) and downgrade-sublibraries / test (lib/OrdinaryDiffEqDifferentiation) have matching callback and diag(::ScalarOperator) errors on head and master. Sublibrary Julia 1 Core checks for GlobalDiffEq, OrdinaryDiffEqDifferentiation, and OrdinaryDiffEqNonlinearSolve have matching unsupported-callback, ScalarOperator, and Trapezoid mass-matrix errors on head and master. tests / GPU (julia 1, self-hosted Linux X64 gpu) / Tests - GPU (Julia 1) has the same QNDF gpu_mismatch on head and master. Core and Default checks pass across LTS, release, and prerelease, including QA. No GitHub reviews or review comments exist.
  • Independent review: Codex / gpt-6-astra rated it low, high confidence: the complete diff preserves valid controller routing, adds an allocation assertion, and every failing check matches a master failure; the requested CLI 0.156.1 (Mac) attribution was not verified.
  • Merge: auto-merge candidate (no observed test regressions or supported API break; minor internal-representation and description caveats above).

🤖 Risk assessment posted by an AI agent (fleet master) — harness: Codex CLI 0.156.1 (Mac) / gpt-6-astra; dispatched by Claude Code head, model claude-opus-5-5[1m]
Conversation: local Claude Code session 3cd6500a-1f81-46b5-ac0b-c466e15b6a53 on Chris's Mac (session ID, no URL)

Runtime indexing of the heterogeneous controller-cache tuple forced
dynamic dispatch on every Default/Composite step. Match the existing
@generated pattern used for dense interpolants and DefaultCache.

Co-Authored-By: Chris Rackauckas <[email protected]>
Co-Authored-By: Cursor Agent <[email protected]>
Agent-Harness: Cursor Agent 2026.09.26-dd393fe
Agent-Model: unknown (Cursor auto)
Agent-Session: local cursor-agent session in /home/crackauc/sandbox/goals/performance/jobs/pr-default-alloc/repo (log /home/crackauc/sandbox/goals/performance/jobs/pr-default-alloc/log.txt)
ChrisRackauckas and others added 2 commits September 26, 2026 20:46
Replace twelve copies of the unrolled cache-index body with one
@eval loop over (fname, extra_args), and match generic_dense.jl's
throw("Cache $current…") error style.

Co-Authored-By: Chris Rackauckas <[email protected]>
Co-Authored-By: Cursor Agent <[email protected]>
Agent-Harness: Cursor Agent 2026.09.26-dd393fe
Agent-Model: unknown (Cursor auto)
Agent-Session: local cursor-agent session in /home/crackauc/sandbox/goals/performance/jobs/pr-default-alloc/repo (log /home/crackauc/sandbox/goals/performance/jobs/pr-default-alloc/log.txt)
Co-Authored-By: Chris Rackauckas <[email protected]>
Co-Authored-By: Cursor Agent <[email protected]>
Agent-Harness: Cursor Agent 2026.09.26-dd393fe
Agent-Model: unknown (Cursor auto)
Agent-Session: local cursor-agent session in /home/crackauc/sandbox/goals/performance/jobs/pr-default-alloc/repo (log /home/crackauc/sandbox/goals/performance/jobs/pr-default-alloc/log.txt)
@ChrisRackauckas-Claude ChrisRackauckas-Claude changed the title Unroll CompositeControllerCache dispatch to eliminate Default step! allocs Unroll CompositeControllerCache dispatch (non-stiff Default step! allocation-free) Sep 27, 2026
ChrisRackauckas and others added 2 commits September 26, 2026 21:15
Nesting $(extra_args...) inside the @generated quote left the name
unbound at generate time. Splice QuoteNode(fname/extra_args) at @eval
and build the call Expr in the generator instead.

Co-Authored-By: Chris Rackauckas <[email protected]>
Co-Authored-By: Cursor Agent <[email protected]>
Agent-Harness: Cursor Agent 2026.09.26-dd393fe
Agent-Model: unknown (Cursor auto)
Agent-Session: local cursor-agent session in /home/crackauc/sandbox/goals/performance/jobs/pr-default-alloc/repo (log /home/crackauc/sandbox/goals/performance/jobs/pr-default-alloc/log.txt)
any(f.(tuple)) materializes a Bool NTuple each Default/composite
step on Julia LTS (3184 B); any(f, itr) matches alg_order and is 0 B.

Co-Authored-By: Chris Rackauckas <[email protected]>
Co-Authored-By: Cursor Agent <[email protected]>
Agent-Harness: Cursor Agent 2026.09.26-dd393fe
Agent-Model: unknown (Cursor auto)
Agent-Session: local cursor-agent session in /home/crackauc/sandbox/goals/performance/jobs/pr-default-alloc/repo (log /home/crackauc/sandbox/goals/performance/jobs/pr-default-alloc/log.txt)
@ChrisRackauckas
ChrisRackauckas marked this pull request as ready for review September 27, 2026 14:43
@ChrisRackauckas
ChrisRackauckas merged commit fc98802 into SciML:master Sep 27, 2026
259 of 266 checks passed
ChrisRackauckas referenced this pull request Sep 29, 2026
* Bump DiffEqBase to 7.21.3

Co-Authored-By: Chris Rackauckas <[email protected]>
Co-Authored-By: Devin <158243242+devin-ai-integration[bot]@users.noreply.github.com>
Agent-Harness: Devin CLI 3000.11.3 (Mac)
Agent-Model: Fusion (claude-opus-5-5 medium + swe-2 medium)
Agent-Session: local transcript ~/.local/share/devin/cli/transcripts/helpful-exception.json

* Bump OrdinaryDiffEqBDF to 2.4.12

Co-Authored-By: Chris Rackauckas <[email protected]>
Co-Authored-By: Devin <158243242+devin-ai-integration[bot]@users.noreply.github.com>
Agent-Harness: Devin CLI 3000.11.3 (Mac)
Agent-Model: Fusion (claude-opus-5-5 medium + swe-2 medium)
Agent-Session: local transcript ~/.local/share/devin/cli/transcripts/helpful-exception.json

* Bump OrdinaryDiffEqSDIRK to 2.9.7

Co-Authored-By: Chris Rackauckas <[email protected]>
Co-Authored-By: Devin <158243242+devin-ai-integration[bot]@users.noreply.github.com>
Agent-Harness: Devin CLI 3000.11.3 (Mac)
Agent-Model: Fusion (claude-opus-5-5 medium + swe-2 medium)
Agent-Session: local transcript ~/.local/share/devin/cli/transcripts/helpful-exception.json

* Bump OrdinaryDiffEqRosenbrock to 2.7.5

Co-Authored-By: Chris Rackauckas <[email protected]>
Co-Authored-By: Devin <158243242+devin-ai-integration[bot]@users.noreply.github.com>
Agent-Harness: Devin CLI 3000.11.3 (Mac)
Agent-Model: Fusion (claude-opus-5-5 medium + swe-2 medium)
Agent-Session: local transcript ~/.local/share/devin/cli/transcripts/helpful-exception.json

* Bump OrdinaryDiffEqLowOrderRK to 2.2.6

Co-Authored-By: Chris Rackauckas <[email protected]>
Co-Authored-By: Devin <158243242+devin-ai-integration[bot]@users.noreply.github.com>
Agent-Harness: Devin CLI 3000.11.3 (Mac)
Agent-Model: Fusion (claude-opus-5-5 medium + swe-2 medium)
Agent-Session: local transcript ~/.local/share/devin/cli/transcripts/helpful-exception.json

* Bump OrdinaryDiffEqTsit5 to 2.1.5

Co-Authored-By: Chris Rackauckas <[email protected]>
Co-Authored-By: Devin <158243242+devin-ai-integration[bot]@users.noreply.github.com>
Agent-Harness: Devin CLI 3000.11.3 (Mac)
Agent-Model: Fusion (claude-opus-5-5 medium + swe-2 medium)
Agent-Session: local transcript ~/.local/share/devin/cli/transcripts/helpful-exception.json

* Bump OrdinaryDiffEqVerner to 2.4.2

Co-Authored-By: Chris Rackauckas <[email protected]>
Co-Authored-By: Devin <158243242+devin-ai-integration[bot]@users.noreply.github.com>
Agent-Harness: Devin CLI 3000.11.3 (Mac)
Agent-Model: Fusion (claude-opus-5-5 medium + swe-2 medium)
Agent-Session: local transcript ~/.local/share/devin/cli/transcripts/helpful-exception.json

* Bump OrdinaryDiffEqSymplecticRK to 2.2.3

Co-Authored-By: Chris Rackauckas <[email protected]>
Co-Authored-By: Devin <158243242+devin-ai-integration[bot]@users.noreply.github.com>
Agent-Harness: Devin CLI 3000.11.3 (Mac)
Agent-Model: Fusion (claude-opus-5-5 medium + swe-2 medium)
Agent-Session: local transcript ~/.local/share/devin/cli/transcripts/helpful-exception.json

---------

Co-authored-by: Devin <158243242+devin-ai-integration[bot]@users.noreply.github.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants