Casting a ReinterpretArray to Array without significant allocations


#1

I noticed in v0.7 that if I want a Vector out of a reinterpret operation rather than a ReinterpretArray that using the Vector constructor to do the conversion comes with a significant allocation cost proportional to the size of the array, seems like data is being copied. Is there a straightforward way to get a Vector essentially pointing to the same data instead of copying?

Similar post: Big overhead with the new lazy reshape/reinterpret

MWE:

a = ones(Int, 10);
b =  reinterpret(NTuple{2,Int}, a);
c =  Array(reinterpret(NTuple{2,Int}, a));

a[1:2]
#=
2-element Array{Int64,1}:
 1
 1
=#

b[1]
#(1, 1)

c[1]
#(1, 1)

a[1] = 2;

b[1]
#(2, 1)

c[1]
#(1, 1)

#2

From what I understand, at the moment your only option in v0.7 is to use the “hack” in the thread you quote. Otherwise you either copy or use the lazy wrapper. An issue has been opened to try to improve the performance of the latter.


#3

I see, I hope this hack can somehow be formalized and embedded in the language in an Array! function or something. reinterpret is really convenient but this penalty will make me think twice before using it.