Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

convert: for arrays, copy or view? #20826

Closed
timholy opened this issue Feb 27, 2017 · 2 comments
Closed

convert: for arrays, copy or view? #20826

timholy opened this issue Feb 27, 2017 · 2 comments
Labels
needs decision A decision on this change is needed
Milestone

Comments

@timholy
Copy link
Sponsor Member

timholy commented Feb 27, 2017

The docstring for convert is wonderfully explicit about providing details about the underlying assumptions in many cases, but I've found one that isn't yet covered. convert(T, x) has the behavior that if x already has type T, we just return x. But for AbstractArrays, things get more complicated:

julia> A = rand(3,5)
3×5 Array{Float64,2}:
 0.324103  0.45017   0.469432  0.382722  0.241098 
 0.336333  0.481648  0.165303  0.616973  0.0452947
 0.567737  0.964775  0.82846   0.300232  0.140285 

julia> pointer(A)
Ptr{Float64} @0x00007fad96e33ba0

julia> pointer(convert(Array, A))
Ptr{Float64} @0x00007fad96e33ba0

julia> S = view(A, :, :)
3×5 SubArray{Float64,2,Array{Float64,2},Tuple{Colon,Colon},true}:
 0.324103  0.45017   0.469432  0.382722  0.241098 
 0.336333  0.481648  0.165303  0.616973  0.0452947
 0.567737  0.964775  0.82846   0.300232  0.140285 

julia> pointer(convert(Array, S))
Ptr{Float64} @0x00007fad970d8e20

In the first case we didn't copy, in the second we did. Often there is no choice but to copy, for example if we had defined S = view(A, 1:2, 1:3); however, in cases like view(A, :, :) it would be possible to return the original without making a copy (and it wouldn't violate type-stability).

An even more intermediate case comes from array types like

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

For this type, convert(Array, A::MyArray) could always return A.data directly. But is this advisable? Or should it be a copy any time typeof(A) != T?

@nalimilan
Copy link
Member

Duplicate of #12441?

Although it could be made even more explicit, the current docstring mentions that aliasing may happen (introduced in #17519):

If T is a collection type and x a collection, the result of convert(T, x) may alias x.

So in general I think convert should avoid copies as much as possible. That means we need a way to convert with the guarantee of getting a copy (similar to copy_oftype discussed at #12441).

@timholy
Copy link
Sponsor Member Author

timholy commented Feb 27, 2017

Indeed, thanks @nalimilan. With 223 open issues mentioning convert and 54 of those mentioning copy, I just missed that.

@timholy timholy closed this as completed Feb 27, 2017
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
needs decision A decision on this change is needed
Projects
None yet
Development

No branches or pull requests

2 participants