From cde97db76abd242c7b998813d21de21362644eba Mon Sep 17 00:00:00 2001 From: Jameson Nash Date: Thu, 9 Jun 2016 13:47:31 -0400 Subject: [PATCH] ensure the array constructors are generally using leaftypes this makes for faster dynamic dispatch and apply-type construction ref #16128 --- base/abstractarray.jl | 30 +++++++++++++------------- base/array.jl | 50 +++++++++++++++++++++---------------------- base/boot.jl | 4 ++-- base/sysimg.jl | 2 +- 4 files changed, 43 insertions(+), 43 deletions(-) diff --git a/base/abstractarray.jl b/base/abstractarray.jl index 3e82aeb2245fa..3007712734489 100644 --- a/base/abstractarray.jl +++ b/base/abstractarray.jl @@ -17,14 +17,14 @@ end ## Basic functions ## -vect() = Array{Any}(0) +vect() = Array{Any,1}(0) vect{T}(X::T...) = T[ X[i] for i=1:length(X) ] function vect(X...) T = promote_typeof(X...) #T[ X[i] for i=1:length(X) ] # TODO: this is currently much faster. should figure out why. not clear. - copy!(Array{T}(length(X)), X) + copy!(Array{T,1}(length(X)), X) end size{T,N}(t::AbstractArray{T,N}, d) = d <= N ? size(t)[d] : 1 @@ -348,7 +348,7 @@ similar{T}(a::AbstractArray{T}, dims::DimOrInd...) = similar(a, T, to_shap similar( a::AbstractArray, T::Type, dims::DimOrInd...) = similar(a, T, to_shape(dims)) similar( a::AbstractArray, T::Type, dims) = similar(a, T, to_shape(dims)) # similar creates an Array by default -similar( a::AbstractArray, T::Type, dims::Dims) = Array{T}(dims) +similar( a::AbstractArray, T::Type, dims::Dims) = Array{T,nfields(dims)}(dims) to_shape(::Tuple{}) = () to_shape(dims::Dims) = dims @@ -870,12 +870,12 @@ promote_eltype() = Bottom promote_eltype(v1, vs...) = promote_type(eltype(v1), promote_eltype(vs...)) #TODO: ERROR CHECK -cat(catdim::Integer) = Array{Any}(0) +cat(catdim::Integer) = Array{Any,1}(0) -vcat() = Array{Any}(0) -hcat() = Array{Any}(0) -typed_vcat{T}(::Type{T}) = Array{T}(0) -typed_hcat{T}(::Type{T}) = Array{T}(0) +vcat() = Array{Any,1}(0) +hcat() = Array{Any,1}(0) +typed_vcat{T}(::Type{T}) = Array{T,1}(0) +typed_hcat{T}(::Type{T}) = Array{T,1}(0) ## cat: special cases vcat{T}(X::T...) = T[ X[i] for i=1:length(X) ] @@ -885,8 +885,8 @@ hcat{T<:Number}(X::T...) = T[ X[j] for i=1:1, j=1:length(X) ] vcat(X::Number...) = hvcat_fill(Array{promote_typeof(X...)}(length(X)), X) hcat(X::Number...) = hvcat_fill(Array{promote_typeof(X...)}(1,length(X)), X) -typed_vcat{T}(::Type{T}, X::Number...) = hvcat_fill(Array{T}(length(X)), X) -typed_hcat{T}(::Type{T}, X::Number...) = hvcat_fill(Array{T}(1,length(X)), X) +typed_vcat{T}(::Type{T}, X::Number...) = hvcat_fill(Array{T,1}(length(X)), X) +typed_hcat{T}(::Type{T}, X::Number...) = hvcat_fill(Array{T,2}(1,length(X)), X) vcat(V::AbstractVector...) = typed_vcat(promote_eltype(V...), V...) vcat{T}(V::AbstractVector{T}...) = typed_vcat(T, V...) @@ -1104,13 +1104,13 @@ function typed_hvcat{T}(::Type{T}, rows::Tuple{Vararg{Int}}, as::AbstractMatrix. end hvcat(rows::Tuple{Vararg{Int}}) = [] -typed_hvcat{T}(::Type{T}, rows::Tuple{Vararg{Int}}) = Array{T}(0) +typed_hvcat{T}(::Type{T}, rows::Tuple{Vararg{Int}}) = Array{T,1}(0) function hvcat{T<:Number}(rows::Tuple{Vararg{Int}}, xs::T...) nr = length(rows) nc = rows[1] - a = Array{T}(nr, nc) + a = Array{T,2}(nr, nc) if length(a) != length(xs) throw(ArgumentError("argument count does not match specified shape (expected $(length(a)), got $(length(xs)))")) end @@ -1153,13 +1153,13 @@ function typed_hvcat{T}(::Type{T}, rows::Tuple{Vararg{Int}}, xs::Number...) if nr*nc != len throw(ArgumentError("argument count $(len) does not match specified shape $((nr,nc))")) end - hvcat_fill(Array{T}(nr, nc), xs) + hvcat_fill(Array{T,2}(nr, nc), xs) end # fallback definition of hvcat in terms of hcat and vcat function hvcat(rows::Tuple{Vararg{Int}}, as...) nbr = length(rows) # number of block rows - rs = Array{Any}(nbr) + rs = Array{Any,1}(nbr) a = 1 for i = 1:nbr rs[i] = hcat(as[a:a-1+rows[i]]...) @@ -1170,7 +1170,7 @@ end function typed_hvcat{T}(::Type{T}, rows::Tuple{Vararg{Int}}, as...) nbr = length(rows) # number of block rows - rs = Array{Any}(nbr) + rs = Array{Any,1}(nbr) a = 1 for i = 1:nbr rs[i] = hcat(as[a:a-1+rows[i]]...) diff --git a/base/array.jl b/base/array.jl index 2a19d201cabda..7aa4f03ec4d56 100644 --- a/base/array.jl +++ b/base/array.jl @@ -114,38 +114,38 @@ end ## Constructors ## -similar(a::Array, T::Type, dims::Dims) = Array{T}(dims) -similar{T}(a::Array{T,1}) = Array{T}(size(a,1)) -similar{T}(a::Array{T,2}) = Array{T}(size(a,1), size(a,2)) -similar{T}(a::Array{T,1}, dims::Dims) = Array{T}(dims) -similar{T}(a::Array{T,1}, m::Int) = Array{T}(m) -similar{T}(a::Array{T,1}, S::Type) = Array{S}(size(a,1)) -similar{T}(a::Array{T,2}, dims::Dims) = Array{T}(dims) -similar{T}(a::Array{T,2}, m::Int) = Array{T}(m) -similar{T}(a::Array{T,2}, S::Type) = Array{S}(size(a,1), size(a,2)) +similar(a::Array, T::Type, dims::Dims) = Array{T,nfields(dims)}(dims) +similar{T}(a::Array{T,1}) = Array{T,1}(size(a,1)) +similar{T}(a::Array{T,2}) = Array{T,2}(size(a,1), size(a,2)) +similar{T}(a::Array{T,1}, dims::Dims) = Array{T,nfields(dims)}(dims) +similar{T}(a::Array{T,1}, m::Int) = Array{T,1}(m) +similar{T}(a::Array{T,1}, S::Type) = Array{S,1}(size(a,1)) +similar{T}(a::Array{T,2}, dims::Dims) = Array{T,nfields(dims)}(dims) +similar{T}(a::Array{T,2}, m::Int) = Array{T,1}(m) +similar{T}(a::Array{T,2}, S::Type) = Array{S,2}(size(a,1), size(a,2)) # T[x...] constructs Array{T,1} function getindex(T::Type, vals...) - a = Array{T}(length(vals)) + a = Array{T,1}(length(vals)) @inbounds for i = 1:length(vals) a[i] = vals[i] end return a end -getindex(T::Type) = Array{T}(0) -getindex(T::Type, x) = (a = Array{T}(1); @inbounds a[1] = x; a) -getindex(T::Type, x, y) = (a = Array{T}(2); @inbounds (a[1] = x; a[2] = y); a) -getindex(T::Type, x, y, z) = (a = Array{T}(3); @inbounds (a[1] = x; a[2] = y; a[3] = z); a) +getindex(T::Type) = Array{T,1}(0) +getindex(T::Type, x) = (a = Array{T,1}(1); @inbounds a[1] = x; a) +getindex(T::Type, x, y) = (a = Array{T,1}(2); @inbounds (a[1] = x; a[2] = y); a) +getindex(T::Type, x, y, z) = (a = Array{T,1}(3); @inbounds (a[1] = x; a[2] = y; a[3] = z); a) function getindex(::Type{Any}, vals::ANY...) - a = Array{Any}(length(vals)) + a = Array{Any,1}(length(vals)) @inbounds for i = 1:length(vals) a[i] = vals[i] end return a end -getindex(::Type{Any}) = Array{Any}(0) +getindex(::Type{Any}) = Array{Any,1}(0) function fill!(a::Union{Array{UInt8}, Array{Int8}}, x::Integer) ccall(:memset, Ptr{Void}, (Ptr{Void}, Cint, Csize_t), a, x, length(a)) @@ -195,7 +195,7 @@ convert{T,n}(::Type{Array{T}}, x::Array{T,n}) = x convert{T,n}(::Type{Array{T,n}}, x::Array{T,n}) = x convert{T,n,S}(::Type{Array{T}}, x::AbstractArray{S, n}) = convert(Array{T, n}, x) -convert{T,n,S}(::Type{Array{T,n}}, x::AbstractArray{S,n}) = copy!(Array{T}(size(x)), x) +convert{T,n,S}(::Type{Array{T,n}}, x::AbstractArray{S,n}) = copy!(Array{T,n}(size(x)), x) promote_rule{T,n,S}(::Type{Array{T,n}}, ::Type{Array{S,n}}) = Array{promote_type(T,S),n} @@ -647,7 +647,7 @@ function vcat{T}(arrays::Vector{T}...) for a in arrays n += length(a) end - arr = Array{T}(n) + arr = Array{T,1}(n) ptr = pointer(arr) if isbits(T) elsz = Core.sizeof(T) @@ -763,13 +763,13 @@ findlast(testf::Function, A) = findprev(testf, A, length(A)) function find(testf::Function, A) # use a dynamic-length array to store the indexes, then copy to a non-padded # array for the return - tmpI = Array{Int}(0) + tmpI = Array{Int,1}(0) for (i,a) = enumerate(A) if testf(a) push!(tmpI, i) end end - I = Array{Int}(length(tmpI)) + I = Array{Int,1}(length(tmpI)) copy!(I, tmpI) return I end @@ -787,8 +787,8 @@ function find(A) return I end -find(x::Number) = x == 0 ? Array{Int}(0) : [1] -find(testf::Function, x::Number) = !testf(x) ? Array{Int}(0) : [1] +find(x::Number) = x == 0 ? Array{Int,1}(0) : [1] +find(testf::Function, x::Number) = !testf(x) ? Array{Int,1}(0) : [1] findn(A::AbstractVector) = find(A) @@ -811,7 +811,7 @@ function findnz{T}(A::AbstractMatrix{T}) nnzA = countnz(A) I = zeros(Int, nnzA) J = zeros(Int, nnzA) - NZs = Array{T}(nnzA) + NZs = Array{T,1}(nnzA) count = 1 if nnzA > 0 for j=1:size(A,2), i=1:size(A,1) @@ -874,7 +874,7 @@ function indexin(a::AbstractArray, b::AbstractArray) end function findin(a, b) - ind = Array{Int}(0) + ind = Array{Int,1}(0) bset = Set(b) @inbounds for (i,ai) in enumerate(a) ai in bset && push!(ind, i) @@ -969,7 +969,7 @@ end function setdiff(a, b) args_type = promote_type(eltype(a), eltype(b)) bset = Set(b) - ret = Array{args_type}(0) + ret = Array{args_type,1}(0) seen = Set{eltype(a)}() for a_elem in a if !in(a_elem, seen) && !in(a_elem, bset) diff --git a/base/boot.jl b/base/boot.jl index 170815e85a7e0..88d7ed85972fe 100644 --- a/base/boot.jl +++ b/base/boot.jl @@ -325,8 +325,8 @@ typealias NTuple{N,T} Tuple{Vararg{T,N}} (::Type{Array{T,2}}){T}() = Array{T,2}(0, 0) # TODO: possibly turn these into deprecations -Array{T,N}(::Type{T}, d::NTuple{N,Int}) = Array{T}(d) -Array{T}(::Type{T}, d::Int...) = Array{T}(d) +Array{T,N}(::Type{T}, d::NTuple{N,Int}) = Array{T,N}(d) +Array{T}(::Type{T}, d::Int...) = Array(T, d) Array{T}(::Type{T}, m::Int) = Array{T,1}(m) Array{T}(::Type{T}, m::Int,n::Int) = Array{T,2}(m,n) Array{T}(::Type{T}, m::Int,n::Int,o::Int) = Array{T,3}(m,n,o) diff --git a/base/sysimg.jl b/base/sysimg.jl index a2ec4b59a0cd5..89217dbf71b8a 100644 --- a/base/sysimg.jl +++ b/base/sysimg.jl @@ -85,7 +85,7 @@ include("array.jl") (::Type{Matrix})(m::Integer, n::Integer) = Matrix{Any}(Int(m), Int(n)) # TODO: possibly turn these into deprecations -Array{T}(::Type{T}, d::Integer...) = Array{T}(convert(Tuple{Vararg{Int}}, d)) +Array{T}(::Type{T}, d::Integer...) = Array(T, convert(Tuple{Vararg{Int}}, d)) Array{T}(::Type{T}, m::Integer) = Array{T,1}(Int(m)) Array{T}(::Type{T}, m::Integer,n::Integer) = Array{T,2}(Int(m),Int(n)) Array{T}(::Type{T}, m::Integer,n::Integer,o::Integer) = Array{T,3}(Int(m),Int(n),Int(o))