Skip to content

Commit cde97db

Browse files
committed
ensure the array constructors are generally using leaftypes
this makes for faster dynamic dispatch and apply-type construction ref #16128
1 parent b390d66 commit cde97db

File tree

4 files changed

+43
-43
lines changed

4 files changed

+43
-43
lines changed

base/abstractarray.jl

Lines changed: 15 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -17,14 +17,14 @@ end
1717

1818
## Basic functions ##
1919

20-
vect() = Array{Any}(0)
20+
vect() = Array{Any,1}(0)
2121
vect{T}(X::T...) = T[ X[i] for i=1:length(X) ]
2222

2323
function vect(X...)
2424
T = promote_typeof(X...)
2525
#T[ X[i] for i=1:length(X) ]
2626
# TODO: this is currently much faster. should figure out why. not clear.
27-
copy!(Array{T}(length(X)), X)
27+
copy!(Array{T,1}(length(X)), X)
2828
end
2929

3030
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
348348
similar( a::AbstractArray, T::Type, dims::DimOrInd...) = similar(a, T, to_shape(dims))
349349
similar( a::AbstractArray, T::Type, dims) = similar(a, T, to_shape(dims))
350350
# similar creates an Array by default
351-
similar( a::AbstractArray, T::Type, dims::Dims) = Array{T}(dims)
351+
similar( a::AbstractArray, T::Type, dims::Dims) = Array{T,nfields(dims)}(dims)
352352

353353
to_shape(::Tuple{}) = ()
354354
to_shape(dims::Dims) = dims
@@ -870,12 +870,12 @@ promote_eltype() = Bottom
870870
promote_eltype(v1, vs...) = promote_type(eltype(v1), promote_eltype(vs...))
871871

872872
#TODO: ERROR CHECK
873-
cat(catdim::Integer) = Array{Any}(0)
873+
cat(catdim::Integer) = Array{Any,1}(0)
874874

875-
vcat() = Array{Any}(0)
876-
hcat() = Array{Any}(0)
877-
typed_vcat{T}(::Type{T}) = Array{T}(0)
878-
typed_hcat{T}(::Type{T}) = Array{T}(0)
875+
vcat() = Array{Any,1}(0)
876+
hcat() = Array{Any,1}(0)
877+
typed_vcat{T}(::Type{T}) = Array{T,1}(0)
878+
typed_hcat{T}(::Type{T}) = Array{T,1}(0)
879879

880880
## cat: special cases
881881
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) ]
885885

886886
vcat(X::Number...) = hvcat_fill(Array{promote_typeof(X...)}(length(X)), X)
887887
hcat(X::Number...) = hvcat_fill(Array{promote_typeof(X...)}(1,length(X)), X)
888-
typed_vcat{T}(::Type{T}, X::Number...) = hvcat_fill(Array{T}(length(X)), X)
889-
typed_hcat{T}(::Type{T}, X::Number...) = hvcat_fill(Array{T}(1,length(X)), X)
888+
typed_vcat{T}(::Type{T}, X::Number...) = hvcat_fill(Array{T,1}(length(X)), X)
889+
typed_hcat{T}(::Type{T}, X::Number...) = hvcat_fill(Array{T,2}(1,length(X)), X)
890890

891891
vcat(V::AbstractVector...) = typed_vcat(promote_eltype(V...), V...)
892892
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.
11041104
end
11051105

11061106
hvcat(rows::Tuple{Vararg{Int}}) = []
1107-
typed_hvcat{T}(::Type{T}, rows::Tuple{Vararg{Int}}) = Array{T}(0)
1107+
typed_hvcat{T}(::Type{T}, rows::Tuple{Vararg{Int}}) = Array{T,1}(0)
11081108

11091109
function hvcat{T<:Number}(rows::Tuple{Vararg{Int}}, xs::T...)
11101110
nr = length(rows)
11111111
nc = rows[1]
11121112

1113-
a = Array{T}(nr, nc)
1113+
a = Array{T,2}(nr, nc)
11141114
if length(a) != length(xs)
11151115
throw(ArgumentError("argument count does not match specified shape (expected $(length(a)), got $(length(xs)))"))
11161116
end
@@ -1153,13 +1153,13 @@ function typed_hvcat{T}(::Type{T}, rows::Tuple{Vararg{Int}}, xs::Number...)
11531153
if nr*nc != len
11541154
throw(ArgumentError("argument count $(len) does not match specified shape $((nr,nc))"))
11551155
end
1156-
hvcat_fill(Array{T}(nr, nc), xs)
1156+
hvcat_fill(Array{T,2}(nr, nc), xs)
11571157
end
11581158

11591159
# fallback definition of hvcat in terms of hcat and vcat
11601160
function hvcat(rows::Tuple{Vararg{Int}}, as...)
11611161
nbr = length(rows) # number of block rows
1162-
rs = Array{Any}(nbr)
1162+
rs = Array{Any,1}(nbr)
11631163
a = 1
11641164
for i = 1:nbr
11651165
rs[i] = hcat(as[a:a-1+rows[i]]...)
@@ -1170,7 +1170,7 @@ end
11701170

11711171
function typed_hvcat{T}(::Type{T}, rows::Tuple{Vararg{Int}}, as...)
11721172
nbr = length(rows) # number of block rows
1173-
rs = Array{Any}(nbr)
1173+
rs = Array{Any,1}(nbr)
11741174
a = 1
11751175
for i = 1:nbr
11761176
rs[i] = hcat(as[a:a-1+rows[i]]...)

base/array.jl

Lines changed: 25 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -114,38 +114,38 @@ end
114114

115115
## Constructors ##
116116

117-
similar(a::Array, T::Type, dims::Dims) = Array{T}(dims)
118-
similar{T}(a::Array{T,1}) = Array{T}(size(a,1))
119-
similar{T}(a::Array{T,2}) = Array{T}(size(a,1), size(a,2))
120-
similar{T}(a::Array{T,1}, dims::Dims) = Array{T}(dims)
121-
similar{T}(a::Array{T,1}, m::Int) = Array{T}(m)
122-
similar{T}(a::Array{T,1}, S::Type) = Array{S}(size(a,1))
123-
similar{T}(a::Array{T,2}, dims::Dims) = Array{T}(dims)
124-
similar{T}(a::Array{T,2}, m::Int) = Array{T}(m)
125-
similar{T}(a::Array{T,2}, S::Type) = Array{S}(size(a,1), size(a,2))
117+
similar(a::Array, T::Type, dims::Dims) = Array{T,nfields(dims)}(dims)
118+
similar{T}(a::Array{T,1}) = Array{T,1}(size(a,1))
119+
similar{T}(a::Array{T,2}) = Array{T,2}(size(a,1), size(a,2))
120+
similar{T}(a::Array{T,1}, dims::Dims) = Array{T,nfields(dims)}(dims)
121+
similar{T}(a::Array{T,1}, m::Int) = Array{T,1}(m)
122+
similar{T}(a::Array{T,1}, S::Type) = Array{S,1}(size(a,1))
123+
similar{T}(a::Array{T,2}, dims::Dims) = Array{T,nfields(dims)}(dims)
124+
similar{T}(a::Array{T,2}, m::Int) = Array{T,1}(m)
125+
similar{T}(a::Array{T,2}, S::Type) = Array{S,2}(size(a,1), size(a,2))
126126

127127
# T[x...] constructs Array{T,1}
128128
function getindex(T::Type, vals...)
129-
a = Array{T}(length(vals))
129+
a = Array{T,1}(length(vals))
130130
@inbounds for i = 1:length(vals)
131131
a[i] = vals[i]
132132
end
133133
return a
134134
end
135135

136-
getindex(T::Type) = Array{T}(0)
137-
getindex(T::Type, x) = (a = Array{T}(1); @inbounds a[1] = x; a)
138-
getindex(T::Type, x, y) = (a = Array{T}(2); @inbounds (a[1] = x; a[2] = y); a)
139-
getindex(T::Type, x, y, z) = (a = Array{T}(3); @inbounds (a[1] = x; a[2] = y; a[3] = z); a)
136+
getindex(T::Type) = Array{T,1}(0)
137+
getindex(T::Type, x) = (a = Array{T,1}(1); @inbounds a[1] = x; a)
138+
getindex(T::Type, x, y) = (a = Array{T,1}(2); @inbounds (a[1] = x; a[2] = y); a)
139+
getindex(T::Type, x, y, z) = (a = Array{T,1}(3); @inbounds (a[1] = x; a[2] = y; a[3] = z); a)
140140

141141
function getindex(::Type{Any}, vals::ANY...)
142-
a = Array{Any}(length(vals))
142+
a = Array{Any,1}(length(vals))
143143
@inbounds for i = 1:length(vals)
144144
a[i] = vals[i]
145145
end
146146
return a
147147
end
148-
getindex(::Type{Any}) = Array{Any}(0)
148+
getindex(::Type{Any}) = Array{Any,1}(0)
149149

150150
function fill!(a::Union{Array{UInt8}, Array{Int8}}, x::Integer)
151151
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
195195
convert{T,n}(::Type{Array{T,n}}, x::Array{T,n}) = x
196196

197197
convert{T,n,S}(::Type{Array{T}}, x::AbstractArray{S, n}) = convert(Array{T, n}, x)
198-
convert{T,n,S}(::Type{Array{T,n}}, x::AbstractArray{S,n}) = copy!(Array{T}(size(x)), x)
198+
convert{T,n,S}(::Type{Array{T,n}}, x::AbstractArray{S,n}) = copy!(Array{T,n}(size(x)), x)
199199

200200
promote_rule{T,n,S}(::Type{Array{T,n}}, ::Type{Array{S,n}}) = Array{promote_type(T,S),n}
201201

@@ -647,7 +647,7 @@ function vcat{T}(arrays::Vector{T}...)
647647
for a in arrays
648648
n += length(a)
649649
end
650-
arr = Array{T}(n)
650+
arr = Array{T,1}(n)
651651
ptr = pointer(arr)
652652
if isbits(T)
653653
elsz = Core.sizeof(T)
@@ -763,13 +763,13 @@ findlast(testf::Function, A) = findprev(testf, A, length(A))
763763
function find(testf::Function, A)
764764
# use a dynamic-length array to store the indexes, then copy to a non-padded
765765
# array for the return
766-
tmpI = Array{Int}(0)
766+
tmpI = Array{Int,1}(0)
767767
for (i,a) = enumerate(A)
768768
if testf(a)
769769
push!(tmpI, i)
770770
end
771771
end
772-
I = Array{Int}(length(tmpI))
772+
I = Array{Int,1}(length(tmpI))
773773
copy!(I, tmpI)
774774
return I
775775
end
@@ -787,8 +787,8 @@ function find(A)
787787
return I
788788
end
789789

790-
find(x::Number) = x == 0 ? Array{Int}(0) : [1]
791-
find(testf::Function, x::Number) = !testf(x) ? Array{Int}(0) : [1]
790+
find(x::Number) = x == 0 ? Array{Int,1}(0) : [1]
791+
find(testf::Function, x::Number) = !testf(x) ? Array{Int,1}(0) : [1]
792792

793793
findn(A::AbstractVector) = find(A)
794794

@@ -811,7 +811,7 @@ function findnz{T}(A::AbstractMatrix{T})
811811
nnzA = countnz(A)
812812
I = zeros(Int, nnzA)
813813
J = zeros(Int, nnzA)
814-
NZs = Array{T}(nnzA)
814+
NZs = Array{T,1}(nnzA)
815815
count = 1
816816
if nnzA > 0
817817
for j=1:size(A,2), i=1:size(A,1)
@@ -874,7 +874,7 @@ function indexin(a::AbstractArray, b::AbstractArray)
874874
end
875875

876876
function findin(a, b)
877-
ind = Array{Int}(0)
877+
ind = Array{Int,1}(0)
878878
bset = Set(b)
879879
@inbounds for (i,ai) in enumerate(a)
880880
ai in bset && push!(ind, i)
@@ -969,7 +969,7 @@ end
969969
function setdiff(a, b)
970970
args_type = promote_type(eltype(a), eltype(b))
971971
bset = Set(b)
972-
ret = Array{args_type}(0)
972+
ret = Array{args_type,1}(0)
973973
seen = Set{eltype(a)}()
974974
for a_elem in a
975975
if !in(a_elem, seen) && !in(a_elem, bset)

base/boot.jl

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -325,8 +325,8 @@ typealias NTuple{N,T} Tuple{Vararg{T,N}}
325325
(::Type{Array{T,2}}){T}() = Array{T,2}(0, 0)
326326

327327
# TODO: possibly turn these into deprecations
328-
Array{T,N}(::Type{T}, d::NTuple{N,Int}) = Array{T}(d)
329-
Array{T}(::Type{T}, d::Int...) = Array{T}(d)
328+
Array{T,N}(::Type{T}, d::NTuple{N,Int}) = Array{T,N}(d)
329+
Array{T}(::Type{T}, d::Int...) = Array(T, d)
330330
Array{T}(::Type{T}, m::Int) = Array{T,1}(m)
331331
Array{T}(::Type{T}, m::Int,n::Int) = Array{T,2}(m,n)
332332
Array{T}(::Type{T}, m::Int,n::Int,o::Int) = Array{T,3}(m,n,o)

base/sysimg.jl

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -85,7 +85,7 @@ include("array.jl")
8585
(::Type{Matrix})(m::Integer, n::Integer) = Matrix{Any}(Int(m), Int(n))
8686

8787
# TODO: possibly turn these into deprecations
88-
Array{T}(::Type{T}, d::Integer...) = Array{T}(convert(Tuple{Vararg{Int}}, d))
88+
Array{T}(::Type{T}, d::Integer...) = Array(T, convert(Tuple{Vararg{Int}}, d))
8989
Array{T}(::Type{T}, m::Integer) = Array{T,1}(Int(m))
9090
Array{T}(::Type{T}, m::Integer,n::Integer) = Array{T,2}(Int(m),Int(n))
9191
Array{T}(::Type{T}, m::Integer,n::Integer,o::Integer) = Array{T,3}(Int(m),Int(n),Int(o))

0 commit comments

Comments
 (0)