# Type stability and array of matrices

**URL:** https://discourse.julialang.org/t/type-stability-and-array-of-matrices/12483
**Category:** Performance
**Tags:** arrays, type-stability
**Created:** [July 19, 2018, 7:29am UTC](https://discourse.julialang.org/t/type-stability-and-array-of-matrices/12483 "2018-07-19T07:29:58Z")
**Posts on this page:** 6
**Page:** 1

<div class="post-metadata">

### Author: ![jarl](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/jarl/32/4365_2.png) [@jarl](https://discourse.julialang.org/u/jarl)
#### Post date: [July 19, 2018, 7:29am UTC](https://discourse.julialang.org/t/type-stability-and-array-of-matrices/12483/1 "2018-07-19T07:29:58Z")

</div>

I am having some trouble getting a deeper understanding of inference / type stability. I have a struct with an array of matrices and a function. I want to extract something from a matrix in the array, say the size of the first matrix:

```julia
type MyType
   A::Array{<:AbstractArray,1}
end 
function firstsize(b::MyType); return size(b.A[1]); end
a=MyType([randn(3,3),randn(3,3)]);
firstsize(a)   
# gives (3,3)

```

If I run code\_warntype on this I get:

```julia
 @code_warntype(firstsize(a))
Variables:
  #self# <optimized out>
  b::MyType

Body:
  begin 
      return (Main.size)((Base.arrayref)((Core.getfield)(b::MyType, :A)::Array{#s231,1} where #s231<:AbstractArray, 1)::AbstractArray)::Any
  end::Any

```

This function will be called many times in an inner loop. Should I worry about the `::Any` as a return type? Why do I get `::Any` and not `Tuple{Int64,Int64}`? Is there a way to get around it? I would preferable have the work for both sparse and dense matrices.

---

<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: [July 19, 2018, 7:36am UTC](https://discourse.julialang.org/t/type-stability-and-array-of-matrices/12483/2 "2018-07-19T07:36:13Z")

</div>

[https://docs.julialang.org/en/latest/manual/performance-tips/#Avoid-fields-with-abstract-type-1](https://docs.julialang.org/en/latest/manual/performance-tips/#Avoid-fields-with-abstract-type-1)

---

<div class="post-metadata">

### Author: ![bkamins](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/bkamins/32/208538_2.png) [@bkamins](https://discourse.julialang.org/u/bkamins)
#### Post date: [July 19, 2018, 7:46am UTC](https://discourse.julialang.org/t/type-stability-and-array-of-matrices/12483/3 "2018-07-19T07:46:04Z")

</div>

Use:

```julia
type MyType{T<:AbstractArray}
   A::Array{T,1}
end

```

and as long as `A` has homogenous type you will have full type stability.

Probably you also should change `type` to `struct` or `mutable struct` as `type` is deprecated.

---

<div class="post-metadata">

### Author: ![jarl](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/jarl/32/4365_2.png) [@jarl](https://discourse.julialang.org/u/jarl)
#### Post date: [July 19, 2018, 9:41am UTC](https://discourse.julialang.org/t/type-stability-and-array-of-matrices/12483/4 "2018-07-19T09:41:45Z")

</div>

Great. Thanks for the extremely quick response and link. It seems to work. After reading the documentation still I have difficulties seeing the difference between `MyType2` and `MyType3`:

```julia
julia> struct MyType1
          A::Array{AbstractArray,1}
       end
julia> struct MyType2
          A::Array{<:AbstractArray,1}
       end
julia> struct MyType3{T<:AbstractArray}
          A::Array{T,1}
       end

```

---

<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: [July 19, 2018, 9:44am UTC](https://discourse.julialang.org/t/type-stability-and-array-of-matrices/12483/5 "2018-07-19T09:44:46Z")

</div>

In `MyType3` you fix `T` and then all of the elements in `A` must be of the same type (`T`). In `MyType2` it is enough that all of the elements in `A` or `<:AbstractArray` but they do not all have to be of the same type.

---

<div class="post-metadata">

### Author: ![jarl](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/jarl/32/4365_2.png) [@jarl](https://discourse.julialang.org/u/jarl)
#### Post date: [July 19, 2018, 9:45am UTC](https://discourse.julialang.org/t/type-stability-and-array-of-matrices/12483/6 "2018-07-19T09:45:53Z")

</div>

Ah! Great. Thanks!
