Skip to content

Avoid quadratic allocation when collecting reactions and equations from subsystems - #1557

Merged
isaacsas merged 1 commit into
SciML:masterfrom
nifets:perf-reduce-init
Sep 12, 2026
Merged

isaacsas merged 1 commit into
SciML:masterfrom
nifets:perf-reduce-init

Conversation

@nifets

@nifets nifets commented Sep 12, 2026

Copy link
Copy Markdown
Contributor

Passing init to reduce(vcat, xs) in reactions(network) and MT.equations(::ReactionSystem)opts out of the optimised reduce dispatch, resulting in quadratic allocation when dealing with hiearchical models. It is also unnecessary, as both functions return early when systems is empty.

Example allocations (MiB) for a system with N subsystems of one reaction pair each:

N reactions master reactions this PR equations master equations this PR
1024 19.4 10.8 18.9 10.2
4096 189.4 43.3 187.7 40.9
8192 648.2 89.6 644.7 81.7

Checklist

  • Appropriate tests were added
  • Any code changes were done in a way that does not break public API
  • All documentation related to code changes were updated
  • The new code follows the
    contributor guidelines, in particular the SciML Style Guide and
    COLPRAC.
  • Any new documentation only uses public API

@isaacsas

Copy link
Copy Markdown
Member

Thanks! I assume the QA test failures are unrelated, so we can merge once the doc build passes.

@ChrisRackauckas

Copy link
Copy Markdown
Member

Yeah that's a blanket reexport thing. Let me change that up so we're not just reexporting everything. I think the tests look fine from this.

@isaacsas
isaacsas merged commit d96e7f5 into SciML:master Sep 12, 2026
23 of 24 checks passed
@nifets
nifets deleted the perf-reduce-init branch September 12, 2026 15:11
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