# Type instability with SubArray on custom Array type

**URL:** <https://discourse.julialang.org/t/type-instability-with-subarray-on-custom-array-type/11775>\
**Category:** Performance\
**Created:** [June 19, 2018, 6:32am UTC](https://discourse.julialang.org/t/type-instability-with-subarray-on-custom-array-type/11775 "2018-06-19T06:32:49Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![jinliangwei](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/jinliangwei/32/1929_2.png) [@jinliangwei](https://discourse.julialang.org/u/jinliangwei)\
**Post date:** [June 19, 2018, 6:32am UTC](https://discourse.julialang.org/t/type-instability-with-subarray-on-custom-array-type/11775/1 "2018-06-19T06:32:49Z")

</div>

I have a program that requires me to use my own array type. However, my own array definition seems to cause a type instability problem, leaving lots of performance on the table (potentially 2~3x improvements).

The issue can be reduced to the minimal working example below:  
[https://gist.github.com/jinliangwei/a10a2bffc1c4fdfa38acb495ff5c43d4](https://gist.github.com/jinliangwei/a10a2bffc1c4fdfa38acb495ff5c43d4)

My program defines two custom array types `DenseArrayAccessor` and `DenseArray`. The former is essentially a wrapper on a Julia vector and the latter wraps on top of the `DenseArrayAccessor`. In my actual program, `DenseArray` contains other fields and supports other operations but those are unnecessary details for this question.

The `train` function is what I am trying to optimize. `@code_warntype` shows many warnings when `H` and `W` are `DenseArray`s but those warnings are gone when I use `DenseArrayAccessor`. Could someone please help me understand what caused this instability?

---

<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:** [June 19, 2018, 7:32am UTC](https://discourse.julialang.org/t/type-instability-with-subarray-on-custom-array-type/11775/2 "2018-06-19T07:32:06Z")

</div>

```julia
struct DenseArray{T, N, V} <: AbstractArray{T, N}
    accessor::Nullable{V}
    DenseArray{T, N}(accessor::V) where {V <: AbstractArray} where {T, N} = new{T, N, V}(accessor)
    DenseArray{T, N}() where {T, N} = new{T, N, Vector{Float64}}(Nullable{Vector{Float64}}()) # Just put some concrete array type
end

```

The problem was `accessor::Nullable{AbstractArray}`. Knowing `T`, and `N` is not enough to say what is inside the nullable so code using it will not be inferable. Solution is to specialize on the concrete type `V` that will be inside the nullable.

---

<div class="post-metadata">

**Author:** ![jinliangwei](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/jinliangwei/32/1929_2.png) [@jinliangwei](https://discourse.julialang.org/u/jinliangwei)\
**Post date:** [June 20, 2018, 5:44pm UTC](https://discourse.julialang.org/t/type-instability-with-subarray-on-custom-array-type/11775/3 "2018-06-20T17:44:16Z")

</div>

I see. Thank you very much!
