# When does julia infer optimal code, when not?

**URL:** <https://discourse.julialang.org/t/when-does-julia-infer-optimal-code-when-not/17592>\
**Category:** Performance\
**Tags:** question\
**Created:** [November 16, 2018, 9:53am UTC](https://discourse.julialang.org/t/when-does-julia-infer-optimal-code-when-not/17592 "2018-11-16T09:53:30Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![schlichtanders](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/schlichtanders/32/32145_2.png) [@schlichtanders](https://discourse.julialang.org/u/schlichtanders)\
**Post date:** [November 16, 2018, 9:53am UTC](https://discourse.julialang.org/t/when-does-julia-infer-optimal-code-when-not/17592/1 "2018-11-16T09:53:30Z")

</div>

Dear julia pros,

lets say I have a basic function `fn(i::Integer) = 5` which and another function `gn(i::Integer) = ...`

what is all allowed in `gn` so that

```julia
@code_llvm fn(3)
@code_llvm gn(3)

```

return very similar results, i.e. gn is reduced to very simple code?

thanks a lot!

---

<div class="post-metadata">

**Author:** ![schlichtanders](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/schlichtanders/32/32145_2.png) [@schlichtanders](https://discourse.julialang.org/u/schlichtanders)\
**Post date:** [November 16, 2018, 10:23am UTC](https://discourse.julialang.org/t/when-does-julia-infer-optimal-code-when-not/17592/2 "2018-11-16T10:23:15Z")

</div>

some examples

# works

```julia-auto
function g(a::Integer)
  a = 0
  for i ∈ 1:4
    a += i
  end
  a
end

@code_llvm g(1)

```

returns

```julia-auto
; Function g
; Location: D:\ProjectsJulia\Traits\test\tmp.jl:68
; Function Attrs: uwtable
define i64 @julia_g_36250(i64) #0 {
top:
; Location: D:\ProjectsJulia\Traits\test\tmp.jl:72
  ret i64 10
}

```

which optimizes the whole loop away

# does not work

other times it does not work, for instance

```julia-auto
function g2(a::Integer)
  methods(reduce)
  4
end

@code_llvm g2(1)

```

gives

```julia-auto
; Function g2
; Location: D:\ProjectsJulia\Traits\test\tmp.jl:72
; Function Attrs: uwtable
define i64 @julia_g2_36717(i64) #0 {
top:
  %1 = alloca %jl_value_t addrspace(10)*, i32 2
; Function methods; {
; Location: reflection.jl:769
  %2 = getelementptr %jl_value_t addrspace(10)*, %jl_value_t addrspace(10)** %1, i32 0
  store %jl_value_t addrspace(10)* addrspacecast (%jl_value_t* inttoptr (i64 21682165440 to %jl_valu
e_t*) to %jl_value_t addrspace(10)*), %jl_value_t addrspace(10)** %2
  %3 = getelementptr %jl_value_t addrspace(10)*, %jl_value_t addrspace(10)** %1, i32 1
  store %jl_value_t addrspace(10)* addrspacecast (%jl_value_t* inttoptr (i64 77858240 to %jl_value_t
*) to %jl_value_t addrspace(10)*), %jl_value_t addrspace(10)** %3
  %4 = call nonnull %jl_value_t addrspace(10)* @japi1_methods_35966(%jl_value_t addrspace(10)* addrs
pacecast (%jl_value_t* inttoptr (i64 21689050048 to %jl_value_t*) to %jl_value_t addrspace(10)*), %j
l_value_t addrspace(10)** %1, i32 2)
;}
; Location: D:\ProjectsJulia\Traits\test\tmp.jl:73
  ret i64 4
}

```

---

<div class="post-metadata">

**Author:** ![louisponet](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/louisponet/32/2070_2.png) [@louisponet](https://discourse.julialang.org/u/louisponet)\
**Post date:** [November 16, 2018, 10:49am UTC](https://discourse.julialang.org/t/when-does-julia-infer-optimal-code-when-not/17592/3 "2018-11-16T10:49:14Z")

</div>

You might find some answers to your questions in this video from Juliacon 2018: [Information Overload: tools for making program analysis and debugging manageable](https://www.youtube.com/watch?v=l0Go2S_L95M&t=1219s)

---

<div class="post-metadata">

**Author:** ![Tamas\_Papp](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/tamas_papp/32/25949_2.png) [@Tamas\_Papp](https://discourse.julialang.org/u/Tamas_Papp)\
**Post date:** [November 16, 2018, 11:43am UTC](https://discourse.julialang.org/t/when-does-julia-infer-optimal-code-when-not/17592/4 "2018-11-16T11:43:37Z")

</div>

The question is not well-posed, as there are many `gn` variations which the compiler would reduce — basically anything that is independent of `i` and constant propagation works.

That said, while constant propagation is very useful optimization, IMO it is not the most important thing you should care about if you want to write performant Julia code for anything that is of medium complexity. When the compiler fails to figure out the **type** of value, you usually pay a much higher price. Looking at `@code_warntype` is much more useful for debugging this.

You may also find the following libraries useful:

> **[GitHub - JunoLab/Traceur.jl](https://github.com/JunoLab/Traceur.jl)**
>
> Contribute to JunoLab/Traceur.jl development by creating an account on GitHub.

> **[GitHub - timholy/ProfileView.jl: Visualization of Julia profiling data](https://github.com/timholy/ProfileView.jl)**
>
> Visualization of Julia profiling data. Contribute to timholy/ProfileView.jl development by creating an account on GitHub.

---

<div class="post-metadata">

**Author:** ![schlichtanders](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/schlichtanders/32/32145_2.png) [@schlichtanders](https://discourse.julialang.org/u/schlichtanders)\
**Post date:** [November 16, 2018, 12:09pm UTC](https://discourse.julialang.org/t/when-does-julia-infer-optimal-code-when-not/17592/5 "2018-11-16T12:09:32Z")

</div>

thanks a lot for the references!

I hope I will find time to look through all of them in detail.

For now I seem to be able to guarantee optimal code by using `@generated` functions for all inference steps which by itself don’t work
