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

narrow array conversions #26330

Merged
merged 1 commit into from
Mar 8, 2018
Merged

narrow array conversions #26330

merged 1 commit into from
Mar 8, 2018

Conversation

JeffBezanson
Copy link
Member

Not all array types can convert from any AbstractArray via a 1-argument constructor call. Structured array types like Diagonal have different constructor behaviors, and some types like ReshapedArray don't have any such constructors.

fixes #26294, fixes #26178

@JeffBezanson JeffBezanson added the arrays [a, r, r, a, y, s] label Mar 5, 2018
@mbauman
Copy link
Member

mbauman commented Mar 5, 2018

Wow, that was easier than I expected. I assume that this then hits the convert deprecation for ::Any for all custom arrays you've not explicitly allowed here?

base/array.jl Outdated
convert(::Type{T}, a::T) where {T<:Array} = a
convert(::Type{T}, a::AbstractArray) where {T<:Array} = T(a)

convert(::Type{AbstractArray{T}}, a::AbstractArray) where {T} = AbstractArray{T}(a)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Did you try removing this one, too?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ah, nevermind. I was getting confused by Type{<:AbstractArray} and Type{AbstractArray}. This looks great. For anyone else getting into the same confusion, this dispatches to the AbstractArray{T}(…) constructor, which definitely exists and just copyto!s into a similar array.

@JeffBezanson
Copy link
Member Author

From this I've discovered that it's possible to convert between sparse matrices and vectors (in the case of matrices, it checks that there's only one column). Other array types don't support that. Do we want to keep it?

@JeffBezanson
Copy link
Member Author

Holy cow, this seems to have a huge effect on sysimg build time and size --- basecompiler.ji goes from ~19MB to ~80MB.

@JeffBezanson
Copy link
Member Author

Ok, I thought this caused it but now I've seen the same behavior on master. Appears to be non-deterministic?

@StefanKarpinski
Copy link
Member

Non-deterministic compiled code size has been reported a few times before.

@mbauman
Copy link
Member

mbauman commented Mar 6, 2018

(#25900)

@mbauman
Copy link
Member

mbauman commented Mar 6, 2018

I'm tentatively in support of having convert support moving between arrays of supported dimensionalities. See #23821.

@JeffBezanson
Copy link
Member Author

OK, I agree that might be fine. I'll keep these methods for now then.

@nalimilan
Copy link
Member

Probably worth marking #23821 for triage? It doesn't make much sense to keep these convert methods if we do not add more generic ones.

@JeffBezanson
Copy link
Member Author

I'm finding the structured matrix constructors a bit hard to navigate. It's not clear when they (should?) check to see if their argument is representable. For example

julia> s = Symmetric(rand(3,3));
# allowed; virtually copies upper triangle to lower

julia> SymTridiagonal(s)
# allowed; ignores off-band elements

julia> tr = Tridiagonal(rand(3,3))
# allowed; ignores off-band elements

julia> SymTridiagonal(tr)
ERROR: ArgumentError: matrix cannot be represented as SymTridiagonal

This might be partly my fault from the constructor-to-convert fallback removal. But I'm guessing the intent is to have all such operations accessible as T(x), with the safe subset of those also callable via convert? That still doesn't seem totally optimal though. For example maybe the constructors should try harder to avoid throwing errors. Or maybe there's a pattern I'm missing of which operations are allowable?

@JeffBezanson
Copy link
Member Author

#23821 should be non-breaking.

@nalimilan
Copy link
Member

#23821 should be non-breaking.

Yes, it's non-breaking, but if we eventually decide not to do it, we won't be able to remove convert methods between sparse matrices and vectors, which will be an unjustified exception. So I'd rather remove these methods now.

Not all array types can convert from any AbstractArray via a
1-argument constructor call.
@JeffBezanson JeffBezanson added triage This should be discussed on a triage call and removed triage This should be discussed on a triage call labels Mar 8, 2018
@JeffBezanson JeffBezanson merged commit 31edd5d into master Mar 8, 2018
@JeffBezanson JeffBezanson deleted the jb/arrayconvert branch March 8, 2018 21:12
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
arrays [a, r, r, a, y, s]
Projects
None yet
4 participants