Skip to content

Skip O(length) sentinel scans over constant numeric array values - #5312

Merged
AayushSabharwal merged 2 commits into
SciML:masterfrom
xtalax-droid:forge/opaque-param-scan
Oct 28, 2026
Merged

AayushSabharwal merged 2 commits into
SciML:masterfrom
xtalax-droid:forge/opaque-param-scan

Conversation

@xtalax-droid

Copy link
Copy Markdown
Contributor

Large constant numeric array values, for example a SparseMatrixCSC parameter with tunable = false, made problem construction quadratic in the matrix dimension. Three places walked every element of such values: build_operating_point and AtomicArrayDictSubstitutionWrapper lookups scanned them through symbolic getindex, and narrow_buffer_type for a vector of arrays iterated them densely, so an N x N sparse matrix cost N^2 steps.

  • A constant array of plain numbers cannot contain COMMON_SENTINEL or COMMON_NOTHING, so a new helper is_numeric_const_array lets both sentinel scans skip it.
  • narrow_buffer_type uses eltype(arr) directly when the array is non-empty and the eltype is concrete. This gives the same promoted type, since typeof(x) == eltype(arr) for every element of a concretely typed array. Empty arrays still take the original loop so they contribute nothing to promotion.
  • Arrays that can hold sentinels (arrays of symbolics, Any arrays) take the unchanged path.

Benchmark (DAEProblem construction with a tridiagonal SparseMatrixCSC{Float64,Int} non-tunable parameter, a dense Fp[1:N] parameter and Fp ~ D(u) + Kp*u; median of 3 after warm-up):

N master this PR
500 0.358 s 0.0074 s
1000 1.437 s 0.0177 s
2000 5.585 s 0.048 s
4000 23.08 s 0.182 s

This is the "operating-point sentinel scan" item of the FiniteElementMethod.jl upstream-blocker list (quadratic scan of opaque/sparse parameters). It is independent of #5121 (array D(u) binding in implicit DAE codegen) and #5190 (evaluate_varmap!).

Tests: new testsets in lib/ModelingToolkitBase/test/initial_values.jl check that constant sparse and dense values pass through build_operating_point unchanged, that sentinels inside symbolic array values are still replaced with COMMON_NOTHING, and that narrow_buffer_type ignores empty arrays when promoting. Initialization group passes; InterfaceI has one failure, components.jl:446 "Causal connections generate causal equations", which also fails on master.

Codex review (two rounds) approved. One minor remaining note: the AADSubWrapper shortcut does not consult dd.default, so a wrapper deliberately constructed with a numeric default such as Const(0.0) would no longer treat an array containing that value as "contains the default". No in-repo caller constructs one that way (all use the default sentinel).


🤖 Posted by an AI agent · harness: Claude Code 2.1.310 · model: claude-sonnet-5-5
Review: unreviewed
Session: de59b94f-0227-49ea-adb5-b158639dad0b

xtalax and others added 2 commits October 24, 2026 20:57
build_operating_point, AtomicArrayDictSubstitutionWrapper lookups and narrow_buffer_type walked every element of constant numeric arrays (e.g. a SparseMatrixCSC parameter) through symbolic getindex or dense iteration, which is O(length) and quadratic for an NxN matrix. Constant arrays of plain numbers cannot hold the sentinel, so they are now skipped; arrays that can hold it behave as before.

Agent-Harness: Claude Code 2.1.310
Agent-Model: claude-sonnet-5-5
Agent-Session: de59b94f-0227-49ea-adb5-b158639dad0b
Agent-Review: unreviewed
Co-authored-by: Claude Code <agent@noreply.juliahub.com>
An empty array contributes no elements to promotion in the original loop, so the concrete-eltype shortcut must not let its eltype widen the result.

Agent-Harness: Claude Code 2.1.310
Agent-Model: claude-sonnet-5-5
Agent-Session: de59b94f-0227-49ea-adb5-b158639dad0b
Agent-Review: unreviewed
Co-authored-by: Claude Code <agent@noreply.juliahub.com>
@AayushSabharwal
AayushSabharwal merged commit 3dbca4b into SciML:master Oct 28, 2026
90 of 111 checks passed
ChrisRackauckas referenced this pull request Oct 30, 2026
Hold signals disconnected by loop openings at their loop-closed values
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.

3 participants