# Does Julia optimise when we reuse function?

**URL:** <https://discourse.julialang.org/t/does-julia-optimise-when-we-reuse-function/73057>\
**Category:** General Usage\
**Tags:** question\
**Created:** [December 14, 2021, 3:08am UTC](https://discourse.julialang.org/t/does-julia-optimise-when-we-reuse-function/73057 "2021-12-14T03:08:12Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![Minimum-Pollution-96](https://avatars.discourse-cdn.com/v4/letter/m/a88e4f/32.png) [@Minimum-Pollution-96](https://discourse.julialang.org/u/Minimum-Pollution-96)\
**Post date:** [December 14, 2021, 3:08am UTC](https://discourse.julialang.org/t/does-julia-optimise-when-we-reuse-function/73057/1 "2021-12-14T03:08:12Z")

</div>

I have the following two functions defined separately.

`C(u_nodes, X, t) = F(u_nodes, X, t)'F(u_nodes, X, t)`  
`b(u_nodes, X, t) = F(u_nodes, X, t)* F(u_nodes, X, t)'`

Now, if I create another 2 functions (called in a sequential order) which uses the above functions `S=f(C)`, `σ=f(b)`, does the F function get calculated again or does it get optimized by julia that it is calculated only once? Or, in other words, is calculating the actual matrix necessary or can I just keep on calling function of functions all the way down?

---

<div class="post-metadata">

**Author:** ![Oscar\_Smith](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/oscar_smith/32/25343_2.png) [@Oscar\_Smith](https://discourse.julialang.org/u/Oscar_Smith)\
**Post date:** [December 14, 2021, 3:23am UTC](https://discourse.julialang.org/t/does-julia-optimise-when-we-reuse-function/73057/2 "2021-12-14T03:23:39Z")

</div>

Julia is allowed to make this optimization if it can prove that `F` is pure (doesn’t mutate state, including IO). That said, this is hard optimization to do since it requires `C` and `b` to be inlined before the compiler is able to see that this optimization is possible, and proving purity can be difficult.

---

<div class="post-metadata">

**Author:** ![Minimum-Pollution-96](https://avatars.discourse-cdn.com/v4/letter/m/a88e4f/32.png) [@Minimum-Pollution-96](https://discourse.julialang.org/u/Minimum-Pollution-96)\
**Post date:** [December 14, 2021, 3:29am UTC](https://discourse.julialang.org/t/does-julia-optimise-when-we-reuse-function/73057/3 "2021-12-14T03:29:18Z")

</div>

Thanks for the reply. What is meant by “inlined”?

---

<div class="post-metadata">

**Author:** ![jling](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/jling/32/212909_2.png) [@jling](https://discourse.julialang.org/u/jling)\
**Post date:** [December 14, 2021, 3:32am UTC](https://discourse.julialang.org/t/does-julia-optimise-when-we-reuse-function/73057/4 "2021-12-14T03:32:14Z")

</div>

> **[Inline expansion](https://en.wikipedia.org/wiki/Inline_expansion)**
>
> In computing, inline expansion, or inlining, is a manual or compiler optimization that replaces a function call site with the body of the called function. Inline expansion is similar to macro expansion, but occurs during compilation, without changing the source code (the text), while macro expansion occurs prior to compilation, and results in different text that is then processed by the compiler.
> Inlining is an important optimization, but has complicated effects on performance. As a rule of th...

---

<div class="post-metadata">

**Author:** ![Minimum-Pollution-96](https://avatars.discourse-cdn.com/v4/letter/m/a88e4f/32.png) [@Minimum-Pollution-96](https://discourse.julialang.org/u/Minimum-Pollution-96)\
**Post date:** [December 14, 2021, 4:04am UTC](https://discourse.julialang.org/t/does-julia-optimise-when-we-reuse-function/73057/5 "2021-12-14T04:04:09Z")

</div>

Thank you!
