# Performance of type annotations

**URL:** https://discourse.julialang.org/t/performance-of-type-annotations/8152
**Category:** Internals & Design
**Created:** [January 4, 2018, 10:59am UTC](https://discourse.julialang.org/t/performance-of-type-annotations/8152 "2018-01-04T10:59:45Z")
**Posts on this page:** 3
**Page:** 1

<div class="post-metadata">

### Author: ![julbinb](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/julbinb/32/3079_2.png) [@julbinb](https://discourse.julialang.org/u/julbinb)
#### Post date: [January 4, 2018, 10:59am UTC](https://discourse.julialang.org/t/performance-of-type-annotations/8152/1 "2018-01-04T10:59:45Z")

</div>

Docs say that type annotations on variables and return types of functions facilitate type inference and performance. But does the opposite hold? If I have a function:

```julia
function foo(x)::Number
    if isa(x, Number)
        return x
    else
        return 0
    end
end

```

Will the `convert(Number, ...)` method be called on the return value of `foo`, or it will be a no-op, for it is clear that both `x` and `0` are `Number`s?

In general, if I have a variable with type annotation, is `convert` called at every assignment to the variable or it optimized away whenever type inference is sure about the type of RHS?

---

<div class="post-metadata">

### Author: ![sdanisch](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/sdanisch/32/1406_2.png) [@sdanisch](https://discourse.julialang.org/u/sdanisch)
#### Post date: [January 4, 2018, 11:11am UTC](https://discourse.julialang.org/t/performance-of-type-annotations/8152/2 "2018-01-04T11:11:04Z")

</div>

Julia has excellent tools to answer those questions 🙂  
E.g. use `@code_warntype` and `@code_lowered` to figure out what happens (you can use `?@code_lowered` in the REPL to get the doc string for those macros):

```julia

julia> function foo(x)::Number
           if isa(x, Number)
               return x
           else
               return 0
           end
       end
foo (generic function with 1 method)

julia> @code_lowered foo(1)
CodeInfo(:(begin 
        nothing
        SSAValue(0) = Main.Number
        unless x isa Main.Number goto 6 # line 3:
        return (Core.typeassert)((Base.convert)(SSAValue(0), x), SSAValue(0))
        6: # line 5:
        return (Core.typeassert)((Base.convert)(SSAValue(0), 0), SSAValue(0))
    end))

julia> @code_warntype foo(1)
Variables:
  #self# <optimized out>
  x::Int64

Body:
  begin # line 3:
      return x::Int64 # line 5:
  end::Int64

julia> @code_warntype foo(1.0)
Variables:
  #self# <optimized out>
  x::Float64

Body:
  begin # line 3:
      return x::Float64 # line 5:
  end::Float64

julia> @code_warntype foo("1")
Variables:
  #self# <optimized out>
  x <optimized out>

Body:
  begin 
      goto 3 # line 3:
      3: # line 5:
      return 0
  end::Int64

```

So Julia seems to turn each `return` into `convert(Number, x)::Number`. The type assert and convert get inlined and removed.

---

<div class="post-metadata">

### Author: ![julbinb](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/julbinb/32/3079_2.png) [@julbinb](https://discourse.julialang.org/u/julbinb)
#### Post date: [January 4, 2018, 11:35am UTC](https://discourse.julialang.org/t/performance-of-type-annotations/8152/3 "2018-01-04T11:35:51Z")

</div>

@sdanisch Indeed, thanks a lot!
