# Custom Array Performance Issues

**URL:** <https://discourse.julialang.org/t/custom-array-performance-issues/29191>\
**Category:** General Usage\
**Created:** [September 26, 2019, 11:20am UTC](https://discourse.julialang.org/t/custom-array-performance-issues/29191 "2019-09-26T11:20:36Z")\
**Posts on this page:** 9\
**Page:** 1

<div class="post-metadata">

**Author:** ![stakaz](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/stakaz/32/4740_2.png) [@stakaz](https://discourse.julialang.org/u/stakaz)\
**Post date:** [September 26, 2019, 11:20am UTC](https://discourse.julialang.org/t/custom-array-performance-issues/29191/1 "2019-09-26T11:20:36Z")

</div>

Hello, I have the following example of a custom array and I do not know why there is a nearly factor 2 overhead in the performance for my custom array type `MyArray`

```julia
using BenchmarkTools

struct MyArray{T,N} <: AbstractArray{T,N} 
	A::Array{T,N}
end

Base.size(A::MyArray) = size(A.A)
Base.getindex(A::MyArray, i::Int) = getindex(A.A, i)
Base.setindex!(A::MyArray, v, i::Int) = setindex!(A.A, v, i)
Base.IndexStyle(::Type{<:MyArray}) = IndexLinear()

A = rand(1000, 1000)
B = MyArray(rand(1000, 1000))

@btime sum(A)
@btime sum(B)

```

This results in

```julia
  1.368 ms (1 allocation: 16 bytes)
  2.016 ms (1 allocation: 16 bytes)

```

Could someone help me to find the bottleneck?

---

<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:** [September 26, 2019, 11:26am UTC](https://discourse.julialang.org/t/custom-array-performance-issues/29191/2 "2019-09-26T11:26:55Z")

</div>

Try

```julia
Base.@propagate_inbounds Base.getindex(A::MyArray, i::Int) = getindex(A.A, i)

```

See  
[https://docs.julialang.org/en/v1/devdocs/boundscheck/#Propagating-inbounds-1](https://docs.julialang.org/en/v1/devdocs/boundscheck/#Propagating-inbounds-1)

---

<div class="post-metadata">

**Author:** ![stakaz](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/stakaz/32/4740_2.png) [@stakaz](https://discourse.julialang.org/u/stakaz)\
**Post date:** [September 26, 2019, 12:08pm UTC](https://discourse.julialang.org/t/custom-array-performance-issues/29191/3 "2019-09-26T12:08:08Z")

</div>

> [@Tamas\_Papp](#):
>
> Base.@propagate\_inbounds

This was exactly the missing part. Now the time is the same. Considering how often one simply wraps an array to have a dispatch on custom types, maybe there should be a note on this in the `Interface` documentation…

---

<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:** [September 26, 2019, 12:19pm UTC](https://discourse.julialang.org/t/custom-array-performance-issues/29191/4 "2019-09-26T12:19:56Z")

</div>

Possibly. Can you make a pull request to the docs?

---

<div class="post-metadata">

**Author:** ![stakaz](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/stakaz/32/4740_2.png) [@stakaz](https://discourse.julialang.org/u/stakaz)\
**Post date:** [September 26, 2019, 12:20pm UTC](https://discourse.julialang.org/t/custom-array-performance-issues/29191/5 "2019-09-26T12:20:56Z")

</div>

Would be my first time but I could try 😉

---

<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:** [September 26, 2019, 12:23pm UTC](https://discourse.julialang.org/t/custom-array-performance-issues/29191/6 "2019-09-26T12:23:13Z")

</div>

see [https://github.com/JuliaLang/julia/blob/master/CONTRIBUTING.md#improving-documentation](https://github.com/JuliaLang/julia/blob/master/CONTRIBUTING.md#improving-documentation) got get started.

---

<div class="post-metadata">

**Author:** ![fredrikekre](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/fredrikekre/32/1688_2.png) [@fredrikekre](https://discourse.julialang.org/u/fredrikekre)\
**Post date:** [September 26, 2019, 1:28pm UTC](https://discourse.julialang.org/t/custom-array-performance-issues/29191/7 "2019-09-26T13:28:47Z")

</div>

Matt’s talk about array implementations from JuliaCon 2018 might also interest you: [An introduction to high performance custom arrays | Matt Bauman - YouTube](https://www.youtube.com/watch?v=jS9eouMJf_Y)

---

<div class="post-metadata">

**Author:** ![stakaz](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/stakaz/32/4740_2.png) [@stakaz](https://discourse.julialang.org/u/stakaz)\
**Post date:** [September 26, 2019, 1:34pm UTC](https://discourse.julialang.org/t/custom-array-performance-issues/29191/8 "2019-09-26T13:34:41Z")

</div>

Thanks 😉 I even was there but it has been a while so I probably forgotten everything…

---

<div class="post-metadata">

**Author:** ![stakaz](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/stakaz/32/4740_2.png) [@stakaz](https://discourse.julialang.org/u/stakaz)\
**Post date:** [September 30, 2019, 9:07am UTC](https://discourse.julialang.org/t/custom-array-performance-issues/29191/9 "2019-09-30T09:07:57Z")

</div>

Here is the PR [https://github.com/JuliaLang/julia/pull/33420](https://github.com/JuliaLang/julia/pull/33420)
