# Help reduce large gc time

**URL:** <https://discourse.julialang.org/t/help-reduce-large-gc-time/17215>\
**Category:** Performance\
**Tags:** diffeq\
**Created:** [November 6, 2018, 3:55pm UTC](https://discourse.julialang.org/t/help-reduce-large-gc-time/17215 "2018-11-06T15:55:51Z")\
**Posts on this page:** 13\
**Page:** 2

<div class="post-metadata">

**Author:** ![rveltz](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/rveltz/32/2707_2.png) [@rveltz](https://discourse.julialang.org/u/rveltz)\
**Post date:** [November 16, 2018, 6:43pm UTC](https://discourse.julialang.org/t/help-reduce-large-gc-time/17215/21 "2018-11-16T18:43:43Z")

</div>

> [@foobar\_lv2](#):
>
> ulia does support enforced interfaces. Therefore, you must be concrete in order to permit inference: Nobody guarantees that `xc[i]` is of type `Tc` just because `xc::AbstractVector{Tc}` .

I see, thank you. I pushed something new. I get:

```julia
--> inplace implementation,
 ----> cvode
[0.0, 1.77942]
  0.019272 seconds (149.65 k allocations: 5.006 MiB)
 ----> lsoda
[0.0, 1.77942]
  0.005362 seconds (78.45 k allocations: 3.329 MiB)
 ----> DiffEq
[0.0, 1.77942]
  0.001536 seconds (4.45 k allocations: 157.031 KiB)
--> stopping time == tf? (not more) false
#jumps = 1000

```

---

<div class="post-metadata">

**Author:** ![foobar\_lv2](https://avatars.discourse-cdn.com/v4/letter/f/ee59a6/32.png) [@foobar\_lv2](https://discourse.julialang.org/u/foobar_lv2)\
**Post date:** [November 16, 2018, 7:43pm UTC](https://discourse.julialang.org/t/help-reduce-large-gc-time/17215/22 "2018-11-16T19:43:46Z")

</div>

You’re getting somewhere. Now the `Xc, Xd` fields are still untyped; can you fix that?

Your mean alloc size is now ~40 bytes. That is exactly the size of a standard view. If this is correct, i.e. you mostly have views, then you need to think about getting rid of them.

If you determine that they impact performance, then you need to figure out which views are allocating (using `julia --track-allocation`) and do one of two things: Either add some `@inline` if possible, or remove the views by writing explicit loops instead of broadcasts.

Views only exist in order to permit people to be lazy about writing an API that uses `AbstractVector`. If you control the function you pass the view to, then you can change the API: For example, turn `function some_fun(v::AbstractVector)` into `function some_fun(M::AbstractMatrix, row_id)` and call it by `some_fun(M, i)` instead of `some_fun(view(M, :, i))`.

This is somewhat ugly. I think it is not recommended to crank out shitty APIs in order to work around the compiler / gc limitation that views sometimes allocate. Sometimes it is necessary, though. Afaict this limitation will stay with us for a while (when I last whined about it, I was told that this is due to lack of resources, different priorities, and carnaval stopping to work on it).

You can of course consider using unsafe views (pointer based); then you need to handle some memory management by hand.

---

<div class="post-metadata">

**Author:** ![rveltz](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/rveltz/32/2707_2.png) [@rveltz](https://discourse.julialang.org/u/rveltz)\
**Post date:** [November 17, 2018, 12:00am UTC](https://discourse.julialang.org/t/help-reduce-large-gc-time/17215/23 "2018-11-17T00:00:57Z")

</div>

> [@foobar\_lv2](#):
>
> You’re getting somewhere. Now the `Xc, Xd` fields are still untyped; can you fix that?

I tried but I am blocked. I would do

```julia
struct A{Tc,vectype_xc<:AbstractVector{Tc},vectype_xc_hist <: AbstractVectorOfArray{Tc}}
   xc::vectype_xc
   Xc::vectype_xc_hist
A{Tc,vectype_xc_hist }(x0::vectype_xc) where {Tc,vectype_xc<:AbstractVector{Tc},vectype_xc_hist <: AbstractVectorOfArray{Tc}} = new(copy(x0), VectorOfArray([copy(xc0)]))
end

```

But this does not work…

ALso, Is it me or it takes more time to compile like `using PDMP`?

---

<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:** [November 17, 2018, 12:05am UTC](https://discourse.julialang.org/t/help-reduce-large-gc-time/17215/24 "2018-11-17T00:05:08Z")

</div>

`<: AbstractVectorOfArray{Tc,1,vectype_xc}`

---

<div class="post-metadata">

**Author:** ![rveltz](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/rveltz/32/2707_2.png) [@rveltz](https://discourse.julialang.org/u/rveltz)\
**Post date:** [November 17, 2018, 7:22am UTC](https://discourse.julialang.org/t/help-reduce-large-gc-time/17215/25 "2018-11-17T07:22:57Z")

</div>

> [@ChrisRackauckas](#):
>
> AbstractVectorOfArray{Tc,1,vectype\_xc}

this gives `too many types for parameters`, `AbstractVectorOfArray{Tc,vectype_xc}` seem to work. Is it what you mean?

---

<div class="post-metadata">

**Author:** ![rveltz](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/rveltz/32/2707_2.png) [@rveltz](https://discourse.julialang.org/u/rveltz)\
**Post date:** [November 17, 2018, 7:40am UTC](https://discourse.julialang.org/t/help-reduce-large-gc-time/17215/26 "2018-11-17T07:40:17Z")

</div>

> [@foobar\_lv2](#):
>
> You’re getting somewhere. Now the `Xc, Xd` fields are still untyped; can you fix that?

It is done! A new version is pushed. It does not change allocation number ☹

```julia
--> inplace implementation,
 ----> cvode
[0.0, 1.77942]
  0.017090 seconds (149.65 k allocations: 5.006 MiB)
 ----> lsoda
[0.0, 1.77942]
  0.003392 seconds (78.45 k allocations: 3.329 MiB)
 ----> DiffEq
[0.0, 1.77942]
  0.001489 seconds (4.45 k allocations: 157.031 KiB)
--> stopping time == tf? (not more) false
#jumps = 1000

```

---

<div class="post-metadata">

**Author:** ![rveltz](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/rveltz/32/2707_2.png) [@rveltz](https://discourse.julialang.org/u/rveltz)\
**Post date:** [November 17, 2018, 9:09am UTC](https://discourse.julialang.org/t/help-reduce-large-gc-time/17215/27 "2018-11-17T09:09:15Z")

</div>

Incredible!

Removing the [line](https://github.com/rveltz/PDMP.jl/blob/109792c8c33954d82acc7037527ca39ac1cdf10c/src/chvdiffeq.jl#L104) removed half of the allocations!! I thought it was a n-op

`t = integrator.u[end]`

is in fact allocating quite a lot. By changing my struct, I can avoid calling this, and get:

```julia
--> inplace implementation,
 ----> cvode
[0.0, 1.77942]
  0.030763 seconds (158.63 k allocations: 5.280 MiB, 29.35% gc time)
 ----> lsoda
[0.0, 1.77942]
  0.004003 seconds (78.45 k allocations: 3.329 MiB)
 ----> DiffEq
[0.0, 1.77942]
  0.001388 seconds (450 allocations: 32.078 KiB)
--> stopping time == tf? (not more) false
#jumps = 1000

```

YESSSSSSS!!

Thanks to all of you!

It is a bit unfortunate I have to try avoid using simple calls like `t = integrator.u[end]`. Is it normal?

---

<div class="post-metadata">

**Author:** ![foobar\_lv2](https://avatars.discourse-cdn.com/v4/letter/f/ee59a6/32.png) [@foobar\_lv2](https://discourse.julialang.org/u/foobar_lv2)\
**Post date:** [November 17, 2018, 12:26pm UTC](https://discourse.julialang.org/t/help-reduce-large-gc-time/17215/28 "2018-11-17T12:26:46Z")

</div>

`t = integrator.u[end]` is probably not allocating; instead it is probably causing a type instability.

What is the type of `t` before, and what is the type of `integrator.u[end]`? Did you tell the compiler about this?

---

<div class="post-metadata">

**Author:** ![rveltz](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/rveltz/32/2707_2.png) [@rveltz](https://discourse.julialang.org/u/rveltz)\
**Post date:** [November 17, 2018, 12:54pm UTC](https://discourse.julialang.org/t/help-reduce-large-gc-time/17215/29 "2018-11-17T12:54:53Z")

</div>

The type of t is `Tc` but for `integrator.u[end]` I am not sure. @ChrisRackauckas Maybe I need to use a parametric call to create `integrator`like `integrator = init{Tc}(ODEProblem...)`?

---

<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:** [November 17, 2018, 12:59pm UTC](https://discourse.julialang.org/t/help-reduce-large-gc-time/17215/30 "2018-11-17T12:59:09Z")

</div>

Maybe use a function barrier or do `let integerator=integrator` after you initiate it. Julia has some issues inferring all of the fields of it due to the keyword arguments, but in most uses like `solve` it hits a function barrier so we never really worried about this. If this is the big issue though we can try to better address this.

---

<div class="post-metadata">

**Author:** ![rveltz](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/rveltz/32/2707_2.png) [@rveltz](https://discourse.julialang.org/u/rveltz)\
**Post date:** [November 17, 2018, 1:03pm UTC](https://discourse.julialang.org/t/help-reduce-large-gc-time/17215/31 "2018-11-17T13:03:24Z")

</div>

> [@ChrisRackauckas](#):
>
> let integerator=integrator

The `let integerator=integrator` tricks did not work.

It is not a big issue for me as I found a way around it. I was just puzzled. Thank you for your answer

---

<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:** [November 17, 2018, 1:04pm UTC](https://discourse.julialang.org/t/help-reduce-large-gc-time/17215/32 "2018-11-17T13:04:35Z")

</div>

Function barrier doesn’t work either?

---

<div class="post-metadata">

**Author:** ![rveltz](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/rveltz/32/2707_2.png) [@rveltz](https://discourse.julialang.org/u/rveltz)\
**Post date:** [November 18, 2018, 6:56am UTC](https://discourse.julialang.org/t/help-reduce-large-gc-time/17215/33 "2018-11-18T06:56:46Z")

</div>

I tried and this function barrier works, thank you.

[Previous page](https://discourse.julialang.org/t/help-reduce-large-gc-time/17215.md?page=1)
