# Type stability of \`quadgk\`

**URL:** <https://discourse.julialang.org/t/type-stability-of-quadgk/10006>\
**Category:** Performance\
**Tags:** type\
**Created:** [March 27, 2018, 2:34pm UTC](https://discourse.julialang.org/t/type-stability-of-quadgk/10006 "2018-03-27T14:34:23Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![kaslusimoes](https://avatars.discourse-cdn.com/v4/letter/k/b19c9b/32.png) [@kaslusimoes](https://discourse.julialang.org/u/kaslusimoes)\
**Post date:** [March 27, 2018, 2:34pm UTC](https://discourse.julialang.org/t/type-stability-of-quadgk/10006/1 "2018-03-27T14:34:23Z")

</div>

Hi,

I am doing several calculations using `QuadGK` inside functions and I’m trying to optimize the performance of my code. Not sure if it is the correct procedure but I started looking for type instabilities in my functions and found this behaviour which I found odd enough to be worth asking here:

```julia
julia> using QuadGK

julia> fr(r::Real) = quadgk(h -> r* h^2, -1, 1)[1]
fr (generic function with 1 method)

julia> @code_warntype fr(2.)
Variables:
  #self# <optimized out>
  r::Float64
  #48::##48#49{Float64}

Body:
  begin 
      #48::##48#49{Float64} = $(Expr(:new, ##48#49{Float64}, :(r)))
      SSAValue(1) = $(Expr(:invoke, MethodInstance for #quadgk#15(::Array{Any,1}, ::Function, ::Function, ::Int64, ::Int64), :(QuadGK.#quadgk#15), :($(Expr(:foreigncall, :(:jl_alloc_array_1d), Array{Any,1}, svec(Any, Int64), Array{Any,1}, 0, 0, 0))), :(Main.quadgk), :(#48), -1, 1))
      return (Base.getfield)(SSAValue(1), 1)::Any
  end::Any

```

It seems to me that the `quadgk()`function is type-instable, because it the compiler sets its’ type to `Any`. Even if I set the result from `quadgk` to `Float64` I still get a `SSAValue(2) = (Base.getfield)(SSAValue(1), 1)::Any`…

Is this wanted behaviour? Am I missing something or doing something wrong?

Thanks!

---

<div class="post-metadata">

**Author:** ![yakir12](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/yakir12/32/297_2.png) [@yakir12](https://discourse.julialang.org/u/yakir12)\
**Post date:** [March 27, 2018, 2:54pm UTC](https://discourse.julialang.org/t/type-stability-of-quadgk/10006/2 "2018-03-27T14:54:18Z")

</div>

> [@kaslusimoes](#):
>
> fr(r::Real)

Not sure, but `Real` is an abstract type, so you might want to:

```julia
function fr(r::T) where T <: Real

```

to set the type.

---

<div class="post-metadata">

**Author:** ![kristoffer.carlsson](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/kristoffer.carlsson/32/22_2.png) [@kristoffer.carlsson](https://discourse.julialang.org/u/kristoffer.carlsson)\
**Post date:** [March 27, 2018, 2:59pm UTC](https://discourse.julialang.org/t/type-stability-of-quadgk/10006/3 "2018-03-27T14:59:01Z")

</div>

The argument declaration does not impact performance here. It only drives dispatch.

---

<div class="post-metadata">

**Author:** ![josuagrw](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/josuagrw/32/1015_2.png) [@josuagrw](https://discourse.julialang.org/u/josuagrw)\
**Post date:** [April 23, 2018, 2:48pm UTC](https://discourse.julialang.org/t/type-stability-of-quadgk/10006/4 "2018-04-23T14:48:35Z")

</div>

Ran into this problem, too. My best guess is the definition of `Segment` is the problem here:

```
struct Segment
    a::Number
    b::Number
    I
    E
end

```

(It doesn’t specialize for the various subtypes of `Number`.) There is an open bug report:  
[https://github.com/JuliaMath/QuadGK.jl/issues/15](https://github.com/JuliaMath/QuadGK.jl/issues/15)
