# Performance regression with indexing a \`reinterpret\`ed array in v0.7

**URL:** <https://discourse.julialang.org/t/performance-regression-with-indexing-a-reinterpret-ed-array-in-v0-7/12443>\
**Category:** Performance\
**Created:** [July 17, 2018, 8:41pm UTC](https://discourse.julialang.org/t/performance-regression-with-indexing-a-reinterpret-ed-array-in-v0-7/12443 "2018-07-17T20:41:38Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![gasagna](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/gasagna/32/1275_2.png) [@gasagna](https://discourse.julialang.org/u/gasagna)\
**Post date:** [July 17, 2018, 8:41pm UTC](https://discourse.julialang.org/t/performance-regression-with-indexing-a-reinterpret-ed-array-in-v0-7/12443/1 "2018-07-17T20:41:38Z")

</div>

Hi,

while updating a package of mine I stumbled into what I think is a regression associated to indexing a `reinterpret`ed array. Basically I have a custom array-like type as

```julia
struct MyArray{T<:Real, V1<:AbstractArray{Complex{T}}, V2<:AbstractArray{T}}
    data::V1
    linear_data::V2
    function MyArray(data::AbstractArray{Complex{T}}) where {T}
        linear_data = reinterpret(T, data)
        new{T, typeof(data), typeof(linear_data)}(data, linear_data)
    end
end

```

where the second field holds a linear `view` of the first. I then implement indexing for this type, like

```julia
Base.@propagate_inbounds @inline getindex(a::MyArray, i) = (@inbounds ret = a.linear_data[i]; ret)

```

and so on for `setindex!`.

A much more complicated code using this type is now significantly (4x) slower on v0.7 than it was on v0.6. Profiling the code shows that quite a bit of time is spent in indexing the new `ReinterpretArray` type, that is returned in v0.7 from `reinterpret(T, data)`.

A quick test on the REPL shows that on 2 days old master

```julia
using BenchmarkTools

julia> a = im*[1.0, 2.0, 3.0]
3-element Array{Complex{Float64},1}:
 0.0 + 1.0im
 0.0 + 2.0im
 0.0 + 3.0im

julia> b = reinterpret(Float64, a)
6-element reinterpret(Float64, ::Array{Complex{Float64},1}):
 0.0
 1.0
 0.0
 2.0
 0.0
 3.0

julia> @btime @inbounds getindex($b, 1)
  7.791 ns (0 allocations: 0 bytes)
0.0

```

while on v0.6 I get

```julia
julia> a = im*[1.0, 2.0, 3.0]
3-element Array{Complex{Float64},1}:
 0.0+1.0im
 0.0+2.0im
 0.0+3.0im

julia> b = reinterpret(Float64, a)
6-element Array{Float64,1}:
 0.0
 1.0
 0.0
 2.0
 0.0
 3.0

julia> @btime @inbounds getindex($b, 1)
  1.276 ns (0 allocations: 0 bytes)

```

Is this worth reporting?

---

<div class="post-metadata">

**Author:** ![gasagna](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/gasagna/32/1275_2.png) [@gasagna](https://discourse.julialang.org/u/gasagna)\
**Post date:** [July 17, 2018, 9:05pm UTC](https://discourse.julialang.org/t/performance-regression-with-indexing-a-reinterpret-ed-array-in-v0-7/12443/2 "2018-07-17T21:05:40Z")

</div>

BTW, I have just tried replacing the `reinterpret`ed array with a pointer to the data of the right type plus using `unsafe_load` and `unsafe_store!`. With this fix I recover the original performance of 0.6 (actually slightly better).

---

<div class="post-metadata">

**Author:** ![favba](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/favba/32/2735_2.png) [@favba](https://discourse.julialang.org/u/favba)\
**Post date:** [July 17, 2018, 9:37pm UTC](https://discourse.julialang.org/t/performance-regression-with-indexing-a-reinterpret-ed-array-in-v0-7/12443/3 "2018-07-17T21:37:35Z")

</div>

See [https://github.com/JuliaLang/julia/pull/27213](https://github.com/JuliaLang/julia/pull/27213)
