# Code stability with for loop

**URL:** <https://discourse.julialang.org/t/code-stability-with-for-loop/31509>\
**Category:** New to Julia\
**Created:** [November 25, 2019, 10:14pm UTC](https://discourse.julialang.org/t/code-stability-with-for-loop/31509 "2019-11-25T22:14:13Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![Egwene\_al\_Vere](https://avatars.discourse-cdn.com/v4/letter/e/df788c/32.png) [@Egwene\_al\_Vere](https://discourse.julialang.org/u/Egwene_al_Vere)\
**Post date:** [November 25, 2019, 10:14pm UTC](https://discourse.julialang.org/t/code-stability-with-for-loop/31509/1 "2019-11-25T22:14:13Z")

</div>

With `for` loops, `@code_warntype` can have one yellow/warning about union. Is it ok performance-wise or am I doing something wrong here? example

```julia
function f(a)
    c = zero(eltype(a))
    ns = 10
    for i in 1:10
        c += sin(a[i])
    end
    c
end

a=rand(10)
@code_warntype f(a)

```

```julia
Variables
  #self#::Core.Compiler.Const(f, false)
  a::Array{Float64,1}
  c::Float64
  ns::Int64
  @_5::Union{Nothing, Tuple{Int64,Int64}}
  i::Int64

Body::Float64
1 ─ %1 = Main.eltype(a)::Core.Compiler.Const(Float64, false)
│ (c = Main.zero(%1))
│ (ns = 10)
│ %4 = (1:10)::Core.Compiler.Const(1:10, false)
│ (@_5 = Base.iterate(%4))
│ %6 = (@_5::Core.Compiler.Const((1, 1), false) === nothing)::Core.Compiler.Const(false, false)
│ %7 = Base.not_int(%6)::Core.Compiler.Const(true, false)
└── goto #4 if not %7
2 ┄ %9 = @_5::Tuple{Int64,Int64}::Tuple{Int64,Int64}
│ (i = Core.getfield(%9, 1))
│ %11 = Core.getfield(%9, 2)::Int64
│ %12 = c::Float64
│ %13 = Base.getindex(a, i)::Float64
│ %14 = Main.sin(%13)::Float64
│ (c = %12 + %14)
│ (@_5 = Base.iterate(%4, %11))
│ %17 = (@_5 === nothing)::Bool
│ %18 = Base.not_int(%17)::Bool
└── goto #4 if not %18
3 ─ goto #2
4 ┄ return c

```

here, `@_5::Union{Nothing, Tuple{Int64,Int64}}` is yellow/warning. Thanks!

---

<div class="post-metadata">

**Author:** ![bashonubuntu](https://avatars.discourse-cdn.com/v4/letter/b/f19dbf/32.png) [@bashonubuntu](https://discourse.julialang.org/u/bashonubuntu)\
**Post date:** [November 25, 2019, 10:31pm UTC](https://discourse.julialang.org/t/code-stability-with-for-loop/31509/2 "2019-11-25T22:31:17Z")

</div>

That’s nothing to worry about. The link below may be helpful

[https://docs.julialang.org/en/v1/manual/performance-tips/index.html#man-code-warntype-1](https://docs.julialang.org/en/v1/manual/performance-tips/index.html#man-code-warntype-1)

Basically, the yellow colored warnings are typically low-priority “type instabilities”. The reply here addresses your question too [How to prevent type instability in for loops?](https://discourse.julialang.org/t/how-to-prevent-type-instability-in-for-loops/30508). To quote from the first reply, “(such type instabilities) is a consequence of the [iterator interface](https://docs.julialang.org/en/latest/manual/interfaces/#) which returns `nothing` when the end of the iterator is reached. Note that the line you cited from the `@code_warntype` output is colored in yellow (not red, which indicates “true” type instabilities)”
