No, I think array.c should do all the thinking for reinterpreting arrays in some Base.unsafe_reinterpret. That is, this kind of reinterpret is the job for someone who knows all the array flags, all the multi-threaded and multiprocessor special cases, who knows which array to lock when, etc. Think of the following:
julia> A=[1,2,3];
julia> B=reinterpret(UInt64, A);
julia> push!(A, 1);
ERROR: cannot resize array with shared data
julia> push!(B, 1);
I am sceptical that allowing push! to B is a good idea. But some author of array.c thought about this, and every end-user resorting to pointer games is definitely worse than core people giving us the best and safest thing that can be done with zero-overhead, and all the segfaults we asked for when zero-overhead is not possible.
The amount of code is not always a good sign of how much will happen in the runtime. It has been confirmed that this all used to optimize away but now fails to do so.
Fair enough, but I remember coming to the conclusion that in many cases this would not be a no-op (it’s been quite a while since I’ve looked at it carefully, so maybe I don’t know what I’m talking about).
I don’t ever remember a time when there weren’t problems with this though, at what point did it get broken?
Even if true, this does not make a Base.unsafe_reinterpret obsolete. Alone the fact that y? reinterpret(some_T, x): something::Array{some_T} is now type-unstable makes it necessary to sometimes really reinterpret memory regions, not julia objects. And the old reinterpret code is perfectly fine for this job, and so much better than unsafe_wrap hacks that will break on first contact with arrays that have nonzero offset.
Having an unsafe_reinterpret that is basically just a safe copy of an unsafe_wraped array does seem like it would be a useful thing. Basically my current workaround of the reinterpret slowness is to do this by default, but also to allow views of unsafe_wraped arrays when it is guaranteed safe.
After you unsafe_wrap an array it is indistinguishable from a “proper” array. That’s what the old reinterpret does. But a one-liner that grabs the pointer and unsafe wraps it is not optimal: In principle you should handle offsets, lock arrays against resizing and possibly handle other array flags. Views of unsafe_wrap arrays are OK by default. Consider:
Wow, awesome @keno! All that work on the optimizer and then a reinterpret PR almost immediately, you are one prolific coder! I’m sure all of us will hugely appreciate whatever improvement this can offer.
I’m not sure I know what you mean. Are you thinking about reinterpreting an Array of Arrays? In that case, I’d think you cannot use reinterpret, which is actually for Arrays of bitstypes
No, I mean compared to getindex for a normal array b[5] where b::Vector{SVector{3, Float64}}. In v0.6 this would have been the output of the reshape(reinterpret(..)). So I am interested in knowing the cost of these 2 operations in v1.1.0-DEV.
I am asking because I don’t have access to a Linux machine currently and building v1.1.0-DEV on Windows didn’t work for me.