# How does cache works in JuliaDiffEq?

**URL:** <https://discourse.julialang.org/t/how-does-cache-works-in-juliadiffeq/14214>\
**Category:** Numerics\
**Tags:** package, diffeq\
**Created:** [August 28, 2018, 9:49pm UTC](https://discourse.julialang.org/t/how-does-cache-works-in-juliadiffeq/14214 "2018-08-28T21:49:25Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![wsrinin](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/wsrinin/32/7676_2.png) [@wsrinin](https://discourse.julialang.org/u/wsrinin)\
**Post date:** [August 28, 2018, 9:49pm UTC](https://discourse.julialang.org/t/how-does-cache-works-in-juliadiffeq/14214/1 "2018-08-28T21:49:25Z")

</div>

Hi,

I am trying to understand the internal of JuliaDiffEq package. I have encountered many files names sth\_cache.jl. Can anyone explain to me what do these files do and why we need it? I tried to find the explanations in the document but I came up with nothing.

---

<div class="post-metadata">

**Author:** ![YingboMa](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/yingboma/32/2181_2.png) [@YingboMa](https://discourse.julialang.org/u/YingboMa)\
**Post date:** [August 29, 2018, 12:15am UTC](https://discourse.julialang.org/t/how-does-cache-works-in-juliadiffeq/14214/2 "2018-08-29T00:15:28Z")

</div>

It is documented very briefly [here](http://devdocs.juliadiffeq.org/latest/contributing/adding_algorithms.html#Adding-new-algorithms-to-OrdinaryDiffEq-1). The gist is that one can allocate all the cache arrays only once and put them into a mutable cache, so when the integrator [performs the stepping](https://github.com/JuliaDiffEq/OrdinaryDiffEq.jl/blob/master/src/perform_step/fixed_timestep_perform_step.jl#L278-L291), it won’t have any memory allocatation. For instance, the [RK4 mutable cache](https://github.com/JuliaDiffEq/OrdinaryDiffEq.jl/blob/master/src/caches/low_order_rk_caches.jl#L99-L124) has `fsalfirst`(k1), `k2`, `k3`, `k4` and `k` for derivatives, `tmp` for [temporary evaluations](https://github.com/JuliaDiffEq/OrdinaryDiffEq.jl/blob/master/src/perform_step/fixed_timestep_perform_step.jl#L284), and `atmp` for the [error estimation](https://github.com/JuliaDiffEq/OrdinaryDiffEq.jl/blob/master/src/perform_step/fixed_timestep_perform_step.jl#L303).

However, sometimes users may not want to mutate the cache arrays (e.g. one might use [`SVector`](https://github.com/JuliaArrays/StaticArrays.jl) or a scalar in the out-of-place form of the derivative function `u' = f(u,p,t)`), hence the [constant cache](https://github.com/JuliaDiffEq/OrdinaryDiffEq.jl/blob/master/src/caches/low_order_rk_caches.jl#L114).

Hopefully, this clears some confusions that you have.
