# Memory usage while solving \`JumpProblem\` increases with run time, even with \`save\_positions=(false,false)\`

**URL:** <https://discourse.julialang.org/t/memory-usage-while-solving-jumpproblem-increases-with-run-time-even-with-save-positions-false-false/96656>\
**Category:** New to Julia\
**Tags:** differentialequation, catalyst\
**Created:** [March 27, 2023, 11:47am UTC](https://discourse.julialang.org/t/memory-usage-while-solving-jumpproblem-increases-with-run-time-even-with-save-positions-false-false/96656 "2023-03-27T11:47:31Z")\
**Posts on this page:** 18\
**Page:** 2

<div class="post-metadata">

**Author:** ![isaacsas](https://avatars.discourse-cdn.com/v4/letter/i/f6c823/32.png) [@isaacsas](https://discourse.julialang.org/u/isaacsas)\
**Post date:** [March 27, 2023, 4:39pm UTC](https://discourse.julialang.org/t/memory-usage-while-solving-jumpproblem-increases-with-run-time-even-with-save-positions-false-false/96656/21 "2023-03-27T16:39:11Z")

</div>

```julia
aff1 = jprob.discrete_jump_aggregation.affects![1]
f = eval(aff1)

```

then gives

```julia
julia> @btime aff1($int)
  14.194 ns (1 allocation: 48 bytes)

julia> @btime aff1($int)
  14.194 ns (1 allocation: 48 bytes)

julia> @btime f($int)
  15.573 ns (1 allocation: 48 bytes)

julia> @btime f($int)
  14.194 ns (1 allocation: 48 bytes)

```

Is that what you meant?

---

<div class="post-metadata">

**Author:** ![ChrisRackauckas](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/chrisrackauckas/32/77_2.png) [@ChrisRackauckas](https://discourse.julialang.org/u/ChrisRackauckas)\
**Post date:** [March 27, 2023, 4:44pm UTC](https://discourse.julialang.org/t/memory-usage-while-solving-jumpproblem-increases-with-run-time-even-with-save-positions-false-false/96656/22 "2023-03-27T16:44:42Z")

</div>

Yeah interesting. Is it returning something? Add a `nothing` on the bottom to force no return.

---

<div class="post-metadata">

**Author:** ![isaacsas](https://avatars.discourse-cdn.com/v4/letter/i/f6c823/32.png) [@isaacsas](https://discourse.julialang.org/u/isaacsas)\
**Post date:** [March 27, 2023, 4:47pm UTC](https://discourse.julialang.org/t/memory-usage-while-solving-jumpproblem-increases-with-run-time-even-with-save-positions-false-false/96656/23 "2023-03-27T16:47:26Z")

</div>

```julia
julia> f = eval(:((var"##MTKIntegrator#1026",)->begin
                        #= /Users/isaacsas/.julia/packages/SymbolicUtils/1JRDc/src/code.jl:350 =#
                        #= /Users/isaacsas/.julia/packages/SymbolicUtils/1JRDc/src/code.jl:351 =#
                        #= /Users/isaacsas/.julia/packages/SymbolicUtils/1JRDc/src/code.jl:352 =#
                        begin
                            ˍ₋out = (var"##MTKIntegrator#1026").u
                            ˍ₋arg1 = (var"##MTKIntegrator#1026").u
                            ˍ₋arg2 = (var"##MTKIntegrator#1026").p
                            t = (var"##MTKIntegrator#1026").t
                            begin
                                begin
                                    #= /Users/isaacsas/.julia/packages/Symbolics/VIBnK/src/build_function.jl:520 =#
                                    #= /Users/isaacsas/.julia/packages/SymbolicUtils/1JRDc/src/code.jl:399 =# @inbounds begin
                                            #= /Users/isaacsas/.julia/packages/SymbolicUtils/1JRDc/src/code.jl:395 =#
                                            ˍ₋out[1] = (+)(1, ˍ₋arg1[1])
                                            #= /Users/isaacsas/.julia/packages/SymbolicUtils/1JRDc/src/code.jl:397 =#
                                            nothing
                                        end
                                end
                            end
                        end; nothing
                    end))
#47 (generic function with 1 method)

julia> @btime f($int)
  14.194 ns (1 allocation: 48 bytes)

julia> @btime f($int)
  14.153 ns (1 allocation: 48 bytes)

julia> function f2(var"##MTKIntegrator#1026")
                              begin
                                   ˍ₋out = (var"##MTKIntegrator#1026").u
                                   ˍ₋arg1 = (var"##MTKIntegrator#1026").u
                                   ˍ₋arg2 = (var"##MTKIntegrator#1026").p
                                   t = (var"##MTKIntegrator#1026").t
                                   begin
                                       begin
                                           #= /Users/isaacsas/.julia/packages/Symbolics/VIBnK/src/build_function.jl:520 =#
                                           #= /Users/isaacsas/.julia/packages/SymbolicUtils/1JRDc/src/code.jl:399 =# @inbounds begin
                                                   #= /Users/isaacsas/.julia/packages/SymbolicUtils/1JRDc/src/code.jl:395 =#
                                                   ˍ₋out[1] = (+)(1, ˍ₋arg1[1])
                                                   #= /Users/isaacsas/.julia/packages/SymbolicUtils/1JRDc/src/code.jl:397 =#
                                                   nothing
                                               end
                                       end
                                   end
                               end
                           end
f2 (generic function with 1 method)

julia> @btime f2($int)
  3.458 ns (0 allocations: 0 bytes)

julia> @btime f2($int)
  3.458 ns (0 allocations: 0 bytes)

```

Explicitly creating a `function` with the code does not allocate. Using `eval` gives allocations with the anonymous function notation it appears.

Note I added a `nothing` in the anonymous function at the end.

---

<div class="post-metadata">

**Author:** ![isaacsas](https://avatars.discourse-cdn.com/v4/letter/i/f6c823/32.png) [@isaacsas](https://discourse.julialang.org/u/isaacsas)\
**Post date:** [March 27, 2023, 4:55pm UTC](https://discourse.julialang.org/t/memory-usage-while-solving-jumpproblem-increases-with-run-time-even-with-save-positions-false-false/96656/24 "2023-03-27T16:55:56Z")

</div>

Another strange thing is the number of `end`s that is generated seems unbalanced:

```julia
julia> aff1
RuntimeGeneratedFunction(#=in ModelingToolkit=#, #=using ModelingToolkit=#, :((var"##MTKIntegrator#1026",)->begin
          #= /Users/isaacsas/.julia/packages/SymbolicUtils/1JRDc/src/code.jl:350 =#
          #= /Users/isaacsas/.julia/packages/SymbolicUtils/1JRDc/src/code.jl:351 =#
          #= /Users/isaacsas/.julia/packages/SymbolicUtils/1JRDc/src/code.jl:352 =#
          begin
              ˍ₋out = (var"##MTKIntegrator#1026").u
              ˍ₋arg1 = (var"##MTKIntegrator#1026").u
              ˍ₋arg2 = (var"##MTKIntegrator#1026").p
              t = (var"##MTKIntegrator#1026").t
              begin
                  begin
                      #= /Users/isaacsas/.julia/packages/Symbolics/VIBnK/src/build_function.jl:520 =#
                      #= /Users/isaacsas/.julia/packages/SymbolicUtils/1JRDc/src/code.jl:399 =# @inbounds begin
                              #= /Users/isaacsas/.julia/packages/SymbolicUtils/1JRDc/src/code.jl:395 =#
                              ˍ₋out[1] = (+)(1, ˍ₋arg1[1])
                              #= /Users/isaacsas/.julia/packages/SymbolicUtils/1JRDc/src/code.jl:397 =#
                              nothing
                          end
                  end
              end
          end
      end))

```

Notice there are 5 ends but only 4 begins. But dropping one end doesn’t seem to eliminate the issue.

---

<div class="post-metadata">

**Author:** ![johannesnauta](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/johannesnauta/32/47434_2.png) [@johannesnauta](https://discourse.julialang.org/u/johannesnauta)\
**Post date:** [March 27, 2023, 4:58pm UTC](https://discourse.julialang.org/t/memory-usage-while-solving-jumpproblem-increases-with-run-time-even-with-save-positions-false-false/96656/25 "2023-03-27T16:58:19Z")

</div>

I rewrote my code to not use a `Symbolics` `@reaction_network`, similar to your implementation, but I do see the increase in memory allocations still (albeit a bit less)…

# Code

(I didn’t want to hide your discussion in my long functions, so here’s a fold:

> **See code details here**
>
> ```julia
> function catalystplasmiddirect(;T = 100.0)    
> function rate(η,X,Y,K)
> return η*(1-(X+Y)/K)
> end
> 
> r1(u,p,t) = rate(p[1],u[1],u[2],p[2])*u[1]
> r2(u,p,t) = rate(p[1],u[2],u[1],p[2])*u[2]
> r3(u,p,t) = p[3]*u[1]
> r4(u,p,t) = p[3]*u[2]
> r5(u,p,t) = p[4]*u[1]*u[2]
> r6(u,p,t) = p[5]*u[2]
> aff1!(integrator) = integrator.u[1] += 1
> aff2!(integrator) = integrator.u[2] += 1
> aff3!(integrator) = integrator.u[1] -= 1
> aff4!(integrator) = integrator.u[2] -= 1
> function aff5!(integrator)
> integrator.u[1] -= 1
> integrator.u[2] += 1
> end
> function aff6!(integrator)
> integrator.u[1] += 1
> integrator.u[2] -= 1
> end
> # η K μ γ ρ
> p = (1.0, 1e4, 0.1, 1e-4, 0.01)
> u0 = [1000,10]
> tspan = (0.0, T)
> 
> dprob = DiscreteProblem(u0, tspan, p)
> jprob = JumpProblem(
> dprob, Direct(),
> ConstantRateJump(r1,aff1!), ConstantRateJump(r2,aff2!), ConstantRateJump(r3,aff3!),
> ConstantRateJump(r4,aff4!), ConstantRateJump(r5,aff5!), ConstantRateJump(r6,aff6!);
> save_positions=(false,false)
> )
> return jprob
> end
> 
> ```
> 
> And the benchmarks:
> 
> ```julia
> julia> long_prob = catalystplasmiddirect(T=1e5)
> JumpProblem with problem DiscreteProblem with aggregator Direct
> Number of jumps with discrete aggregation: 6
> Number of jumps with continuous aggregation: 0
> Number of mass action jumps: 0
> 
> julia> long_sol = solve(long_prob, stepper)
> retcode: Success
> Interpolation: Piecewise constant interpolation
> t: 2-element Vector{Float64}:
> 0.0
> 100000.0
> u: 2-element Vector{Vector{Int64}}:
> [1000, 10]
> [98, 8850]
> 
> julia> @btime solve($long_prob, $stepper)
> 11.082 s (195816952 allocations: 2.92 GiB)
> retcode: Success
> Interpolation: Piecewise constant interpolation
> t: 2-element Vector{Float64}:
> 0.0
> 100000.0
> u: 2-element Vector{Vector{Int64}}:
> [1000, 10]
> [96, 8951]
> 
> julia> short_prob = catalystplasmiddirect(T=1e2)
> JumpProblem with problem DiscreteProblem with aggregator Direct
> Number of jumps with discrete aggregation: 6
> Number of jumps with continuous aggregation: 0
> Number of mass action jumps: 0
> 
> julia> short_sol = solve(short_prob, stepper)
> retcode: Success
> Interpolation: Piecewise constant interpolation
> t: 2-element Vector{Float64}:
> 0.0
> 100.0
> u: 2-element Vector{Vector{Int64}}:
> [1000, 10]
> [92, 8892]
> 
> julia> @btime solve($short_prob, $stepper)
> 10.677 ms (204394 allocations: 3.12 MiB)
> retcode: Success
> Interpolation: Piecewise constant interpolation
> t: 2-element Vector{Float64}:
> 0.0
> 100.0
> u: 2-element Vector{Vector{Int64}}:
> [1000, 10]
> [118, 8933]
> 
> ```

---

<div class="post-metadata">

**Author:** ![isaacsas](https://avatars.discourse-cdn.com/v4/letter/i/f6c823/32.png) [@isaacsas](https://discourse.julialang.org/u/isaacsas)\
**Post date:** [March 27, 2023, 5:09pm UTC](https://discourse.julialang.org/t/memory-usage-while-solving-jumpproblem-increases-with-run-time-even-with-save-positions-false-false/96656/26 "2023-03-27T17:09:25Z")

</div>

> [@johannesnauta](#):
>
> `short_prob = TestJumpProcesses.catalystplasmiddirect(T=1e2)`

OK, but if you switch to `DirectFW`, which uses `FunctionWrapper`s, the memory allocations stay the same for me (make sure you call the same function to generate your problem for short and long times).

So the issue in this specific case is from what I said earlier with `Direct` calling the affects array and creating a union type I think. However, there is still an issue with Symbolics generated code, which seems to still exhibit allocations even when using `DirectFW`.

---

<div class="post-metadata">

**Author:** ![johannesnauta](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/johannesnauta/32/47434_2.png) [@johannesnauta](https://discourse.julialang.org/u/johannesnauta)\
**Post date:** [March 27, 2023, 5:27pm UTC](https://discourse.julialang.org/t/memory-usage-while-solving-jumpproblem-increases-with-run-time-even-with-save-positions-false-false/96656/27 "2023-03-27T17:27:46Z")

</div>

Indeed, when I change `Direct()` to `DirectFW()` the large memory allocations disappear. Is this documented somewhere?

And there is indeed still the issue with `Symbolics` generated code. I can confirm the increased memory usage even when using `DirectFW()`. Thanks anyways for highlighting the other approach(es) to implementing reactions.

---

<div class="post-metadata">

**Author:** ![isaacsas](https://avatars.discourse-cdn.com/v4/letter/i/f6c823/32.png) [@isaacsas](https://discourse.julialang.org/u/isaacsas)\
**Post date:** [March 27, 2023, 5:29pm UTC](https://discourse.julialang.org/t/memory-usage-while-solving-jumpproblem-increases-with-run-time-even-with-save-positions-false-false/96656/28 "2023-03-27T17:29:56Z")

</div>

Any SSA method but `Direct` will use `FunctionWrappers` internally and so shouldn’t have this issue (when directly constructing the jumps – the issue with codegen from Symbolics is different).

For your problem, if you build a dependency graph as explained in the JumpProcesses docs I’d guess one of `SortingDirect`, `RSSA` or `RSSACR` is going to be fastest (depending on how big your full model is).

---

<div class="post-metadata">

**Author:** ![isaacsas](https://avatars.discourse-cdn.com/v4/letter/i/f6c823/32.png) [@isaacsas](https://discourse.julialang.org/u/isaacsas)\
**Post date:** [March 27, 2023, 5:35pm UTC](https://discourse.julialang.org/t/memory-usage-while-solving-jumpproblem-increases-with-run-time-even-with-save-positions-false-false/96656/29 "2023-03-27T17:35:38Z")

</div>

I should add, if you find allocation growth on these examples with other methods (when building the problem directly in JumpProcesses) please do let me know.

---

<div class="post-metadata">

**Author:** ![johannesnauta](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/johannesnauta/32/47434_2.png) [@johannesnauta](https://discourse.julialang.org/u/johannesnauta)\
**Post date:** [March 27, 2023, 7:52pm UTC](https://discourse.julialang.org/t/memory-usage-while-solving-jumpproblem-increases-with-run-time-even-with-save-positions-false-false/96656/30 "2023-03-27T19:52:59Z")

</div>

I originally used the `RSSA()` method (but with the `Symbolics` codegen) and noticed the growth. I will try to figure out how to define a [`MassActionJump`](https://docs.sciml.ai/JumpProcesses/stable/api/#JumpProcesses.MassActionJump) with a variable rate (should I use a [`VariableRateJump`](https://docs.sciml.ai/JumpProcesses/stable/jump_types/#Defining-a-Variable-Rate-Jump), or does that one not work with the `RSSA()` method?).

> **(CODE) With a fixed rate it's not hard to create the stoichiometry.**
>
> ```julia
> function catalystplasmidrssa(;T = 100.0)
> rateidxs = [1,1,2,2,3,4]
> reactant_stoich = [
> [1 => 1], # 1*F (reproduction)
> [2 => 1], # 1*P (reproduction)
> [1 => 1], # 1*F (death)
> [2 => 1], # 1*P (death)
> [2 => 1], # 1*P (recovery, segregation error)
> [1 => 1, 2=> 1] # 1*F 1*P (infection, conjugation)
> ]
> net_stoich = [
> [1 => 1], # F --> 2F
> [2 => 1], # P --> 2P
> [1 => -1], # F --> 0
> [2 => -1], # P --> 0
> [1 => 1, 2 => -1], # P --> F
> [1 => -1, 2 => 1] # F+P --> 2P
> ]
>     
> u0 = [1000,10]
> tspan = (0.0, T)
> K = 1e4
> # η μ ρ γ
> p = (1.0, 0.1, 0.1, 1e-4)
> 
> dprob = DiscreteProblem(u0, tspan, p)
> mass_action_jump = MassActionJump(reactant_stoich, net_stoich; param_idxs=rateidxs)
> jump_prob = JumpProblem(dprob, RSSA(), mass_action_jump; save_positions=(false,false))
> return jump_prob
> end
> 
> ```

As the carrying capacity term is not included this leads to infinite growth (i.e. depending on \gamma we have either F\rightarrow \infty or P\rightarrow \infty, so do not choose large (\gtrsim 10^2) timespans or your REPL will hang due to the integers becoming very large very quick.

But, as mentioned in the links above, I see nothing in the tutorial about variable rates outside of the example that I provided earlier with `DirectFW()`. So for larger systems I will still need to figure it out. Once I do I will make a nice example to add to the documentation perhaps.

Also, if this needs to be made into an issue (either for `Catalyst.jl` or for `JumpProcesses.jl`?) for better tracking, let me know as well.

---

<div class="post-metadata">

**Author:** ![isaacsas](https://avatars.discourse-cdn.com/v4/letter/i/f6c823/32.png) [@isaacsas](https://discourse.julialang.org/u/isaacsas)\
**Post date:** [March 28, 2023, 11:58am UTC](https://discourse.julialang.org/t/memory-usage-while-solving-jumpproblem-increases-with-run-time-even-with-save-positions-false-false/96656/31 "2023-03-28T11:58:27Z")

</div>

@johannesnauta have you gone through the JumpProcesses tutorials? You want to use a mix of `MassActionJump`s and `ConstantRateJump`s for your problem. The latter let you have arbitrary rate functions as long as they are not explicitly time-dependent so can be used to model a carrying capacity. For a pure jump problem you would only use `VariableRateJump`s if your rate expressions explicitly include `t`. See for example:

[https://docs.sciml.ai/JumpProcesses/stable/tutorials/discrete\_stochastic\_example/](https://docs.sciml.ai/JumpProcesses/stable/tutorials/discrete_stochastic_example/)

---

<div class="post-metadata">

**Author:** ![johannesnauta](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/johannesnauta/32/47434_2.png) [@johannesnauta](https://discourse.julialang.org/u/johannesnauta)\
**Post date:** [March 28, 2023, 12:32pm UTC](https://discourse.julialang.org/t/memory-usage-while-solving-jumpproblem-increases-with-run-time-even-with-save-positions-false-false/96656/32 "2023-03-28T12:32:38Z")

</div>

Yes I went through the `JumpProcesses` tutorial (more specifically [this part](https://docs.sciml.ai/JumpProcesses/stable/tutorials/discrete_stochastic_example/#ConstantRateJumpSect), which is why I used the `ConstantRateJumps` as illustrated [above](https://discourse.julialang.org/t/memory-usage-while-solving-jumpproblem-increases-with-run-time-even-with-save-positions-false-false/96656/25) in the code details). However, as you mentioned, that code used `Direct()` instead of a method that uses `FunctionWrappers`.  
While it worked fine when using `DirectFW()`, it does not work with other methods, such as `RSSA()` as:

```julia
ERROR: To use the RSSA algorithm a map from variables to dependent jumps must be supplied.

```

So I was under the impression I needed to implement the stoichiometry directly, as explained in the [`MassActionJump`](https://docs.sciml.ai/JumpProcesses/stable/tutorials/discrete_stochastic_example/#MassActionJumpSect) documentation/example.

Edit: also there is a note on this error here: [Jump types and JumpProblem · JumpProcesses.jl](https://docs.sciml.ai/JumpProcesses/stable/jump_types/#Jump-Aggregators-Requiring-Dependency-Graphs), but I am unsure how to implement it from that documentation alone.

---

<div class="post-metadata">

**Author:** ![isaacsas](https://avatars.discourse-cdn.com/v4/letter/i/f6c823/32.png) [@isaacsas](https://discourse.julialang.org/u/isaacsas)\
**Post date:** [March 29, 2023, 12:57pm UTC](https://discourse.julialang.org/t/memory-usage-while-solving-jumpproblem-increases-with-run-time-even-with-save-positions-false-false/96656/33 "2023-03-29T12:57:36Z")

</div>

Can you try `Direct` with JumpProcesses 9.6.1? I put in what seems to be a fix for the memory issue (when directly constructing jumps, it doesn’t fix the issue with the codegen from Symbolics unfortunately).

---

<div class="post-metadata">

**Author:** ![isaacsas](https://avatars.discourse-cdn.com/v4/letter/i/f6c823/32.png) [@isaacsas](https://discourse.julialang.org/u/isaacsas)\
**Post date:** [March 29, 2023, 5:13pm UTC](https://discourse.julialang.org/t/memory-usage-while-solving-jumpproblem-increases-with-run-time-even-with-save-positions-false-false/96656/34 "2023-03-29T17:13:49Z")

</div>

Regarding dependency graphs, for a reaction-reaction dependency graph you need a mapping from the index representing the `i`th reaction to a vector of the indices of all reactions that depend on the `i`th reaction. Basically this is a mapping that says when reaction `i` occurs, which reactions have rate functions that depend on a species that is modified by reaction `i`.

Reaction indices are ordered internally by the mass action jumps (in the order they are passed to `MassActionJump`) and then by `ConstantRateJump`s in the order they are passed, see this example for how the ordering should work:

“[FAQ · JumpProcesses.jl](https://docs.sciml.ai/JumpProcesses/dev/faq/#How-should-jumps-be-ordered-in-dependency-graphs?)”

---

<div class="post-metadata">

**Author:** ![isaacsas](https://avatars.discourse-cdn.com/v4/letter/i/f6c823/32.png) [@isaacsas](https://discourse.julialang.org/u/isaacsas)\
**Post date:** [March 29, 2023, 10:18pm UTC](https://discourse.julialang.org/t/memory-usage-while-solving-jumpproblem-increases-with-run-time-even-with-save-positions-false-false/96656/35 "2023-03-29T22:18:11Z")

</div>

@johannesnauta I’ve also checked that with the new `Direct` in JumpProcesses 9.6.1 your Catalyst model from `catalyst_plasmid` does not appear to show the memory growth anymore (but there is still growth for other solvers).

---

<div class="post-metadata">

**Author:** ![johannesnauta](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/johannesnauta/32/47434_2.png) [@johannesnauta](https://discourse.julialang.org/u/johannesnauta)\
**Post date:** [April 10, 2023, 7:22am UTC](https://discourse.julialang.org/t/memory-usage-while-solving-jumpproblem-increases-with-run-time-even-with-save-positions-false-false/96656/36 "2023-04-10T07:22:18Z")

</div>

Sorry for the late reply. I was in the middle of moving and things did not go as planned.

But, with `v.9.6.1` of `JumpProcesses` the `catalyst_plasmid` function indeed does not show the memory growth anymore. In fact, the overall memory usage has decreased drastically in general. I will further explore the functions that define the `MassActionJump`s and `ConstantRateJump`s directly, but checking [here](https://docs.sciml.ai/JumpProcesses/dev/jump_types/#Recommendations-for-exact-methods) I believe that `Direct()` will do for now – until I scale my problem to larger networks. Even still, I do not think I will have many updates per jump, so perhaps `Direct()` will remain the fastest method.

Thanks a lot for the effort and big apology for the long delay.

---

<div class="post-metadata">

**Author:** ![isaacsas](https://avatars.discourse-cdn.com/v4/letter/i/f6c823/32.png) [@isaacsas](https://discourse.julialang.org/u/isaacsas)\
**Post date:** [April 10, 2023, 2:42pm UTC](https://discourse.julialang.org/t/memory-usage-while-solving-jumpproblem-increases-with-run-time-even-with-save-positions-false-false/96656/37 "2023-04-10T14:42:32Z")

</div>

I have a PR that appears to fix the issues for all the non-direct methods for models coming from Catalyst too:

> <https://github.com/SciML/JumpProcesses.jl/pull/309>
>
> @ChrisRackauckas what do you think about this approach? Basically, when not usin…g tuples for affects we store them as \`affects!::Any\` (but they are concretely a \`Vector{Any}\`). When \`init\` is called the \`Vector{Any}\` that is stored is concretized into a vector of type-stable function wrappers using the integrator type as input. Then I add a manual dispatch when \`affects!\` isn't a tuple to handle pulling out this vector from the \`affects!::Any\` field.
> 
> Preliminary testing is that overall I'm seeing a half-percent or less speed decrease when using \`Direct\` with tuples (so still much faster than before the recent generated function approach I put in), but a ~17% or more speed up when using function wrappers. Moreover, the memory allocation issues go away, so I think we now have type-stability outside the highest level call into the jump aggregator callback.
> 
> If this approach looks reasonable I'll cleanup and refactor the code from \`Direct\` to work with all the SSAs. 
> 
> One nice thing about the setup here is that it wouldn't be too bad to support affect! tuples for any method now, which can give a nice speed up (Direct vs. DirectFW on a small system is still like a 40% speed difference). We might want to add a similar manual dispatch for \`rate\` functions so that we can also support them as tuples with other SSAs. I’m thinking we could then switch based on the number of jumps to automatically use tuples vs. function wrappers for any method.

(It also gives a 10-25% speedup for the other methods courtesy of the fix it appears.)

I probably won’t get it done this week, but hopefully next. In any case, once that gets merged you should be able to use Catalyst-generated models with any of the constant rate aggregators without issue. I’ll let you know once a new release with those fixes is out.

---

<div class="post-metadata">

**Author:** ![isaacsas](https://avatars.discourse-cdn.com/v4/letter/i/f6c823/32.png) [@isaacsas](https://discourse.julialang.org/u/isaacsas)\
**Post date:** [April 25, 2023, 1:23am UTC](https://discourse.julialang.org/t/memory-usage-while-solving-jumpproblem-increases-with-run-time-even-with-save-positions-false-false/96656/38 "2023-04-25T01:23:11Z")

</div>

Ok, this should now be fixed for all methods as of version 9.6.3.

[Previous page](https://discourse.julialang.org/t/memory-usage-while-solving-jumpproblem-increases-with-run-time-even-with-save-positions-false-false/96656.md?page=1)
