# AbstractArray vs. Array in Julia types

**URL:** <https://discourse.julialang.org/t/abstractarray-vs-array-in-julia-types/3937>\
**Category:** General Usage\
**Tags:** question, performance\
**Created:** [May 26, 2017, 12:15am UTC](https://discourse.julialang.org/t/abstractarray-vs-array-in-julia-types/3937 "2017-05-26T00:15:28Z")\
**Posts on this page:** 16\
**Page:** 1

<div class="post-metadata">

**Author:** ![juliohm](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/juliohm/32/215266_2.png) [@juliohm](https://discourse.julialang.org/u/juliohm)\
**Post date:** [May 26, 2017, 12:15am UTC](https://discourse.julialang.org/t/abstractarray-vs-array-in-julia-types/3937/1 "2017-05-26T00:15:28Z")

</div>

Consider the simple type:

```julia
type Foo{T}
A::AbstractArray{T} # (1)
end

# (2) update Foo.A with some operation on external array A
update{T}(foo::Foo{T}, A::AbstractArray{T}) = foo.A = 2*A

```

I created two marks in the code `(1)` and `(2)` and I would like to ask you what are the best practices regarding the choice `AbstractArray` vs. `Array` in Julia.

I understand that by replacing `AbstractArray` with a dense `Array` in both `(1)` and `(2)` we make the type concrete and the compiler can perform all sorts of memory optimizations to store the arrays in contiguous memory chunks. I wonder how we should reason through this tradeoff between generic functions that accept any type of array and efficient memory layouts with dense arrays. Could you please share your thoughts? Specifically for `(1)` and `(2)`, how do you decide between using `AbstractArray` vs. `Array`?

---

<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:** [May 26, 2017, 12:22am UTC](https://discourse.julialang.org/t/abstractarray-vs-array-in-julia-types/3937/2 "2017-05-26T00:22:31Z")

</div>

```nohighlight
julia> struct Foo{A_t <: AbstractArray{T} where T}
           A::A_t
       end

julia> Foo(1:10)
Foo{UnitRange{Int64}}(1:10)

julia> Foo(rand(3))
Foo{Array{Float64,1}}([0.118515, 0.232194, 0.177065])

```

---

<div class="post-metadata">

**Author:** ![ChrisRackauckas](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/chrisrackauckas/32/77_2.png) [@ChrisRackauckas](https://discourse.julialang.org/u/ChrisRackauckas)\
**Post date:** [May 26, 2017, 12:25am UTC](https://discourse.julialang.org/t/abstractarray-vs-array-in-julia-types/3937/3 "2017-05-26T00:25:01Z")

</div>

> [@juliohm](#):
>
> I understand that by replacing AbstractArray with a dense Array in both (1) and (2) we make the type concrete and the compiler can perform all sorts of memory optimizations to store the arrays in contiguous memory chunks. I wonder how we should reason through this tradeoff between generic functions that accept any type of array and efficient memory layouts with dense arrays. Could you please share your thoughts? Specifically for (1) and (2), how do you decide between using AbstractArray vs. Array?

If you just make it parametric on the type of array

```julia
type Foo{T<:AbstractArray}
A::T
end

```

then you don’t lose performance, and on v0.6 you can do this recursively, so

```julia
type Foo{T<:AbstractArray{T2} where T2}
A::T
end

```

(or something like that, I’m not too familiar with v0.6 where syntax yet but I know you can do this).

Generally your algorithm is compatible with `AbstractArray` if it’s compatible with `Array`. Pretty much the only case where that’s not true is when you’re making a call to C/Fortran which expects a contiguous array. Otherwise, if it’s pure Julia code, you can usually expect the array type to do all of the “normal stuff”, like have a linear index and broadcast mathematical expressions. Then you leave it to the user to give you a GPUArray or whatnot.

---

<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:** [May 26, 2017, 12:30am UTC](https://discourse.julialang.org/t/abstractarray-vs-array-in-julia-types/3937/4 "2017-05-26T00:30:45Z")

</div>

Perhaps something like:

```julia
julia> struct Foo{T, N, A_t <: AbstractArray{T,N}} <: AbstractArray{T,N}
           A::A_t
       end

julia> Foo(v::AbstractArray{T, N}) where{T, N} = Foo{T, N, typeof(v)}(v) # Aux constructor

```

also works?

---

<div class="post-metadata">

**Author:** ![juliohm](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/juliohm/32/215266_2.png) [@juliohm](https://discourse.julialang.org/u/juliohm)\
**Post date:** [May 26, 2017, 2:47am UTC](https://discourse.julialang.org/t/abstractarray-vs-array-in-julia-types/3937/5 "2017-05-26T02:47:41Z")

</div>

Thanks @kristoffer.carlsson, so I think I will require Julia v0.6 in my package to enjoy both generic and performant code.

---

<div class="post-metadata">

**Author:** ![juliohm](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/juliohm/32/215266_2.png) [@juliohm](https://discourse.julialang.org/u/juliohm)\
**Post date:** [May 26, 2017, 2:49am UTC](https://discourse.julialang.org/t/abstractarray-vs-array-in-julia-types/3937/6 "2017-05-26T02:49:49Z")

</div>

Thanks @ChrisRackauckas, I think I will have to go with Julia v0.6 then.

---

<div class="post-metadata">

**Author:** ![PetrKryslUCSD](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/petrkryslucsd/32/215825_2.png) [@PetrKryslUCSD](https://discourse.julialang.org/u/PetrKryslUCSD)\
**Post date:** [May 26, 2017, 2:33pm UTC](https://discourse.julialang.org/t/abstractarray-vs-array-in-julia-types/3937/7 "2017-05-26T14:33:36Z")

</div>

> [@ChrisRackauckas](#):
>
> (or something like that, I’m not too familiar with v0.6 where syntax yet but I know you can do this).

@ChrisRackauckas, @kristoffer.carlsson

By the way, how does one read up on the upcoming features for the version 0.6 in one place? I’ve seen voluminous discussions in different places, but often it is not quite clear what will eventually make it and the documentation on the Julia language website only seems to refer to the 0.5 (?) version.

Thanks,

Petr

---

<div class="post-metadata">

**Author:** ![mbauman](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/mbauman/32/31082_2.png) [@mbauman](https://discourse.julialang.org/u/mbauman)\
**Post date:** [May 26, 2017, 2:49pm UTC](https://discourse.julialang.org/t/abstractarray-vs-array-in-julia-types/3937/8 "2017-05-26T14:49:39Z")

</div>

NEWS.md contains major changes. [https://github.com/JuliaLang/julia/blob/release-0.6/NEWS.md](https://github.com/JuliaLang/julia/blob/release-0.6/NEWS.md)

---

<div class="post-metadata">

**Author:** ![PetrKryslUCSD](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/petrkryslucsd/32/215825_2.png) [@PetrKryslUCSD](https://discourse.julialang.org/u/PetrKryslUCSD)\
**Post date:** [May 26, 2017, 3:00pm UTC](https://discourse.julialang.org/t/abstractarray-vs-array-in-julia-types/3937/9 "2017-05-26T15:00:33Z")

</div>

Fantastic. Many thanks.

---

<div class="post-metadata">

**Author:** ![juliohm](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/juliohm/32/215266_2.png) [@juliohm](https://discourse.julialang.org/u/juliohm)\
**Post date:** [May 26, 2017, 3:00pm UTC](https://discourse.julialang.org/t/abstractarray-vs-array-in-julia-types/3937/10 "2017-05-26T15:00:37Z")

</div>

@PetrKryslUCSD, I believe that the development deviated from the original plans regarding Julia v0.6. We don’t have documentation nor official release on the downloads page. The nightly builds are already targeting Julia v0.7, my guess is that the next official release will be either v0.7 or v1.0.

---

<div class="post-metadata">

**Author:** ![PetrKryslUCSD](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/petrkryslucsd/32/215825_2.png) [@PetrKryslUCSD](https://discourse.julialang.org/u/PetrKryslUCSD)\
**Post date:** [May 26, 2017, 3:01pm UTC](https://discourse.julialang.org/t/abstractarray-vs-array-in-julia-types/3937/11 "2017-05-26T15:01:30Z")

</div>

Right, thanks for the heads up.

---

<div class="post-metadata">

**Author:** ![ChrisRackauckas](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/chrisrackauckas/32/77_2.png) [@ChrisRackauckas](https://discourse.julialang.org/u/ChrisRackauckas)\
**Post date:** [May 26, 2017, 3:03pm UTC](https://discourse.julialang.org/t/abstractarray-vs-array-in-julia-types/3937/12 "2017-05-26T15:03:35Z")

</div>

> [@juliohm](#):
>
> @PetrKryslUCSD, I believe that the development deviated from the original plans regarding Julia v0.6. We don’t have documentation nor official release on the downloads page. The nightly builds are already targeting Julia v0.7, my guess is that the next official release will be either v0.7 or v1.0.

That is false. The downloads page has a release candidate for v0.6 (currently release candidate 2: v0.6-rc2). When “all of the bugs are fixed” there, that will be the next release: v0.6. The changes are described in the news page @mbauman linked, and the new Documenter.jl docs will become the standard docs.

---

<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:** [May 26, 2017, 3:06pm UTC](https://discourse.julialang.org/t/abstractarray-vs-array-in-julia-types/3937/13 "2017-05-26T15:06:35Z")

</div>

Please be more careful with the information you spread. This is blatantly false.

---

<div class="post-metadata">

**Author:** ![juliohm](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/juliohm/32/215266_2.png) [@juliohm](https://discourse.julialang.org/u/juliohm)\
**Post date:** [May 26, 2017, 3:15pm UTC](https://discourse.julialang.org/t/abstractarray-vs-array-in-julia-types/3937/14 "2017-05-26T15:15:33Z")

</div>

@ChrisRackauckas thanks for pointing out the release candidates, these I know exist. They are there for a while. I remember the planned deadlines to release v0.6 have passed, please correct me if I am wrong. Anyways, I wish I had the documentation for v0.6 as well.

---

<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:** [May 26, 2017, 3:17pm UTC](https://discourse.julialang.org/t/abstractarray-vs-array-in-julia-types/3937/15 "2017-05-26T15:17:26Z")

</div>

[https://docs.julialang.org/en/latest/](https://docs.julialang.org/en/latest/)

---

<div class="post-metadata">

**Author:** ![juliohm](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/juliohm/32/215266_2.png) [@juliohm](https://discourse.julialang.org/u/juliohm)\
**Post date:** [May 26, 2017, 3:25pm UTC](https://discourse.julialang.org/t/abstractarray-vs-array-in-julia-types/3937/16 "2017-05-26T15:25:08Z")

</div>

Hi @kristoffer.carlsson, isn’t latest referring to v0.7? Maybe my confusion is because I thought latest and nightly were the same thing.
