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

rewrite all zeros(...) calls #25392

Closed
wants to merge 10 commits into from
12 changes: 6 additions & 6 deletions base/abstractarray.jl
Original file line number Diff line number Diff line change
Expand Up @@ -560,7 +560,7 @@ indices of the result will match `A`.
would create a 1-dimensional logical array whose indices match those
of the columns of `A`.

similar(dims->zeros(Int, dims), axes(A))
similar(dims->fill(0, dims), axes(A))

would create an array of `Int`, initialized to zero, matching the
indices of `A`.
Expand Down Expand Up @@ -1244,9 +1244,9 @@ function cat_t(dims, T::Type, X...)
end

function _cat(A, shape::NTuple{N}, catdims, X...) where N
offsets = zeros(Int, N)
offsets = fill(0, N)
inds = Vector{UnitRange{Int}}(uninitialized, N)
concat = copyto!(zeros(Bool, N), catdims)
concat = copyto!(Vector{Bool}(uninitialized, N), catdims)
for x in X
for i = 1:N
if concat[i]
Expand Down Expand Up @@ -1907,11 +1907,11 @@ collection. `destination` must be at least as large as the first collection.

# Examples
```jldoctest
julia> x = zeros(3);
julia> A = [0.0, 0.0, 0.0];

julia> map!(x -> x * 2, x, [1, 2, 3]);
julia> map!(x -> x * 2, A, [1, 2, 3]);

julia> x
julia> A
3-element Array{Float64,1}:
2.0
4.0
Expand Down
4 changes: 2 additions & 2 deletions base/array.jl
Original file line number Diff line number Diff line change
Expand Up @@ -1828,8 +1828,8 @@ julia> findnz(A)
"""
function findnz(A::AbstractMatrix{T}) where T
nnzA = count(t -> t != 0, A)
I = zeros(Int, nnzA)
J = zeros(Int, nnzA)
I = Vector{Int}(uninitialized, nnzA)
J = Vector{Int}(uninitialized, nnzA)
NZs = Vector{T}(uninitialized, nnzA)
cnt = 1
if nnzA > 0
Expand Down
2 changes: 1 addition & 1 deletion base/bitarray.jl
Original file line number Diff line number Diff line change
Expand Up @@ -1059,7 +1059,7 @@ end
## Unary operators ##

function (-)(B::BitArray)
A = zeros(Int, size(B))
A = fill(0, size(B))
l = length(B)
l == 0 && return A
Bc = B.chunks
Expand Down
2 changes: 1 addition & 1 deletion base/bitset.jl
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ mutable struct BitSet <: AbstractSet{Int}
# 1st stored Int equals 64*offset
offset::Int

BitSet() = new(sizehint!(zeros(UInt64, 0), 4), NO_OFFSET)
BitSet() = new(sizehint!(UInt64[], 4), NO_OFFSET)
end

"""
Expand Down
4 changes: 2 additions & 2 deletions base/dict.jl
Original file line number Diff line number Diff line change
Expand Up @@ -101,7 +101,7 @@ mutable struct Dict{K,V} <: AbstractDict{K,V}

function Dict{K,V}() where V where K
n = 16
new(zeros(UInt8,n), Vector{K}(uninitialized, n), Vector{V}(uninitialized, n), 0, 0, 0, 1, 0)
new(fill(0x0, n), Vector{K}(uninitialized, n), Vector{V}(uninitialized, n), 0, 0, 0, 1, 0)
end
function Dict{K,V}(d::Dict{K,V}) where V where K
new(copy(d.slots), copy(d.keys), copy(d.vals), d.ndel, d.count, d.age,
Expand Down Expand Up @@ -213,7 +213,7 @@ function rehash!(h::Dict{K,V}, newsz = length(h.keys)) where V where K
return h
end

slots = zeros(UInt8,newsz)
slots = fill(0x0, newsz)
keys = Vector{K}(uninitialized, newsz)
vals = Vector{V}(uninitialized, newsz)
age0 = h.age
Expand Down
2 changes: 1 addition & 1 deletion base/env.jl
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ if Sys.iswindows()
if len == 0
return Libc.GetLastError() != ERROR_ENVVAR_NOT_FOUND ? "" : onError(str)
end
val = zeros(UInt16,len)
val = fill(zero(UInt16), len)
Copy link
Member

@Sacha0 Sacha0 Jan 16, 2018

Choose a reason for hiding this comment

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

Slightly simpler: UInt16(0). (Edit: Likewise below with a number of different concrete types.)

ret = ccall(:GetEnvironmentVariableW,stdcall,UInt32,(Ptr{UInt16},Ptr{UInt16},UInt32),var,val,len)
if (ret == 0 && len != 1) || ret != len-1 || val[end] != 0
error(string("getenv: ", str, ' ', len, "-1 != ", ret, ": ", Libc.FormatMessage()))
Expand Down
2 changes: 1 addition & 1 deletion base/file.jl
Original file line number Diff line number Diff line change
Expand Up @@ -407,7 +407,7 @@ Return the files and directories in the directory `dir` (or the current working
"""
function readdir(path::AbstractString)
# Allocate space for uv_fs_t struct
uv_readdir_req = zeros(UInt8, ccall(:jl_sizeof_uv_fs_t, Int32, ()))
uv_readdir_req = fill(0x0, ccall(:jl_sizeof_uv_fs_t, Int32, ()))

# defined in sys.c, to call uv_fs_readdir, which sets errno on error.
err = ccall(:uv_fs_scandir, Int32, (Ptr{Cvoid}, Ptr{UInt8}, Cstring, Cint, Ptr{Cvoid}),
Expand Down
10 changes: 5 additions & 5 deletions base/inference.jl
Original file line number Diff line number Diff line change
Expand Up @@ -3091,7 +3091,7 @@ genlabel(sv::OptimizationState) = LabelNode(sv.next_label += 1)

function get_label_map(body::Vector{Any})
nlabels = label_counter(body)
labelmap = zeros(Int, nlabels)
labelmap = fill(0, nlabels)
for i = 1:length(body)
el = body[i]
if isa(el, LabelNode)
Expand Down Expand Up @@ -3127,7 +3127,7 @@ function find_ssavalue_uses(e::Expr, uses::Vector{BitSet}, line::Int)
end

function find_ssavalue_defs(body::Vector{Any}, nvals::Int)
defs = zeros(Int, nvals)
defs = fill(0, nvals)
for line in 1:length(body)
e = body[line]
if isa(e, Expr) && e.head === :(=)
Expand Down Expand Up @@ -4942,7 +4942,7 @@ function inlineable(@nospecialize(f), @nospecialize(ft), e::Expr, atypes::Vector

# make labels / goto statements unique
# relocate inlining information
newlabels = zeros(Int, label_counter(body.args))
newlabels = fill(0, label_counter(body.args))
for i = 1:length(body.args)
a = body.args[i]
if isa(a, LabelNode)
Expand Down Expand Up @@ -5779,7 +5779,7 @@ function split_undef_flag_pass!(sv::OptimizationState)
len = length(body)
next_i = 1
norigslots = length(sv.src.slotflags)
flagslots = zeros(Int, norigslots)
flagslots = fill(0, norigslots)
while next_i <= len
i = next_i
next_i += 1
Expand Down Expand Up @@ -6657,7 +6657,7 @@ function split_struct_alloc!(ctx::AllocOptContext, info, key)
end
if ndef > 1
allowed_idx = trues(min_nf)
idx_count = zeros(Int, min_nf)
idx_count = fill(0, min_nf)
for v in values(ctx.sym_count)
# Ignore field names that will not be allowed to be optimized
if isa(v, Pair{Int,Int})
Expand Down
4 changes: 2 additions & 2 deletions base/intfuncs.jl
Original file line number Diff line number Diff line change
Expand Up @@ -780,8 +780,8 @@ julia> digits(10, 2, 6)
"""
digits(n::Integer, base::T=10, pad::Integer=1) where {T<:Integer} = digits(T, n, base, pad)

function digits(T::Type{<:Integer}, n::Integer, base::Integer=10, pad::Integer=1)
digits!(zeros(T, ndigits(n, base, pad)), n, base)
function digits(::Type{T}, n::Integer, base::Integer=10, pad::Integer=1) where {T<:Integer}
digits!(Vector{T}(uninitialized, ndigits(n, base, pad)), n, base)
end

"""
Expand Down
2 changes: 1 addition & 1 deletion base/io.jl
Original file line number Diff line number Diff line change
Expand Up @@ -680,7 +680,7 @@ function readuntil_indexable(io::IO, target#=::Indexable{T}=#, out)
else
# grow cache to contain up to `pos`
if !@isdefined(cache)
cache = zeros(Int, len)
cache = fill(0, len)
end
while max_pos < pos
b = cache[max_pos] + first
Expand Down
2 changes: 1 addition & 1 deletion base/iostream.jl
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ end
# TODO: delay adding finalizer, e.g. for memio with a small buffer, or
# in the case where we take! it.
function IOStream(name::AbstractString, finalize::Bool)
buf = zeros(UInt8,sizeof_ios_t)
buf = fill(0x0, sizeof_ios_t)
x = IOStream(name, buf)
if finalize
finalizer(close, x)
Expand Down
4 changes: 2 additions & 2 deletions base/linalg/bidiag.jl
Original file line number Diff line number Diff line change
Expand Up @@ -135,7 +135,7 @@ end
#Converting from Bidiagonal to dense Matrix
function Matrix{T}(A::Bidiagonal) where T
n = size(A, 1)
B = zeros(T, n, n)
B = fill(zero(T), n, n)
for i = 1:n - 1
B[i,i] = A.dv[i]
if A.uplo == 'U'
Expand Down Expand Up @@ -605,7 +605,7 @@ function eigvecs(M::Bidiagonal{T}) where T
n = length(M.dv)
Q = Matrix{T}(uninitialized, n,n)
blks = [0; find(x -> x == 0, M.ev); n]
v = zeros(T, n)
v = fill(zero(T), n)
if M.uplo == 'U'
for idx_block = 1:length(blks) - 1, i = blks[idx_block] + 1:blks[idx_block + 1] #index of eigenvector
fill!(v, zero(T))
Expand Down
4 changes: 2 additions & 2 deletions base/linalg/bitarray.jl
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,8 @@ end
#if mA == 0; return C; end
#col_ch = num_bit_chunks(mA)
## TODO: avoid using aux chunks and copy (?)
#aux_chunksA = zeros(UInt64, col_ch)
#aux_chunksB = [zeros(UInt64, col_ch) for j=1:nB]
#aux_chunksA = fill(zero(UInt64), col_ch)
#aux_chunksB = [fill(zero(UInt64), col_ch) for j=1:nB]
#for j = 1:nB
#Base.copy_chunks!(aux_chunksB[j], 1, B.chunks, (j-1)*mA+1, mA)
#end
Expand Down
2 changes: 1 addition & 1 deletion base/linalg/cholesky.jl
Original file line number Diff line number Diff line change
Expand Up @@ -404,7 +404,7 @@ function getproperty(C::CholeskyPivoted{T}, d::Symbol) where T<:BlasFloat
return getfield(C, :piv)
elseif d == :P
n = size(C, 1)
P = zeros(T, n, n)
P = fill(zero(T), n, n)
for i = 1:n
P[getfield(C, :piv)[i], i] = one(T)
end
Expand Down
6 changes: 3 additions & 3 deletions base/linalg/dense.jl
Original file line number Diff line number Diff line change
Expand Up @@ -335,7 +335,7 @@ end
function diagm_container(kv::Pair{<:Integer,<:AbstractVector}...)
T = promote_type(map(x -> eltype(x.second), kv)...)
n = mapreduce(x -> length(x.second) + abs(x.first), max, kv)
return zeros(T, n, n)
return fill(zero(T), n, n)
end
function diagm_container(kv::Pair{<:Integer,<:BitVector}...)
n = mapreduce(x -> length(x.second) + abs(x.first), max, kv)
Expand Down Expand Up @@ -1280,7 +1280,7 @@ function pinv(A::StridedMatrix{T}, tol::Real) where T
if istril(A)
if istriu(A)
maxabsA = maximum(abs.(diag(A)))
B = zeros(Tout, n, m)
B = fill(zero(Tout), n, m)
for i = 1:min(m, n)
if abs(A[i,i]) > tol*maxabsA
Aii = inv(A[i,i])
Expand All @@ -1294,7 +1294,7 @@ function pinv(A::StridedMatrix{T}, tol::Real) where T
end
SVD = svdfact(A, full = false)
Stype = eltype(SVD.S)
Sinv = zeros(Stype, length(SVD.S))
Sinv = fill(zero(Stype), length(SVD.S))
index = SVD.S .> tol*maximum(SVD.S)
Sinv[index] = one(Stype) ./ SVD.S[index]
Sinv[find(.!isfinite.(Sinv))] = zero(Stype)
Expand Down
2 changes: 1 addition & 1 deletion base/linalg/diagonal.jl
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,7 @@ end
r
end
diagzero(::Diagonal{T},i,j) where {T} = zero(T)
diagzero(D::Diagonal{Matrix{T}},i,j) where {T} = zeros(T, size(D.diag[i], 1), size(D.diag[j], 2))
diagzero(D::Diagonal{Matrix{T}},i,j) where {T} = fill(zero(T), size(D.diag[i], 1), size(D.diag[j], 2))

function setindex!(D::Diagonal, v, i::Int, j::Int)
@boundscheck checkbounds(D, i, j)
Expand Down
8 changes: 4 additions & 4 deletions base/linalg/eigen.jl
Original file line number Diff line number Diff line change
Expand Up @@ -30,11 +30,11 @@ Same as [`eigfact`](@ref), but saves space by overwriting the input `A` (and
"""
function eigfact!(A::StridedMatrix{T}; permute::Bool=true, scale::Bool=true) where T<:BlasReal
n = size(A, 2)
n == 0 && return Eigen(zeros(T, 0), zeros(T, 0, 0))
n == 0 && return Eigen(T[], Matrix{T}(unininitialized, 0, 0))
issymmetric(A) && return eigfact!(Symmetric(A))
A, WR, WI, VL, VR, _ = LAPACK.geevx!(permute ? (scale ? 'B' : 'P') : (scale ? 'S' : 'N'), 'N', 'V', 'N', A)
iszero(WI) && return Eigen(WR, VR)
evec = zeros(Complex{T}, n, n)
evec = Matrix{Complex{T}}(unininitialized, n, n)
j = 1
while j <= n
if WI[j] == 0
Expand All @@ -53,7 +53,7 @@ end

function eigfact!(A::StridedMatrix{T}; permute::Bool=true, scale::Bool=true) where T<:BlasComplex
n = size(A, 2)
n == 0 && return Eigen(zeros(T, 0), zeros(T, 0, 0))
n == 0 && return Eigen(T[], Matrix{T}(unininitialized, 0, 0))
ishermitian(A) && return eigfact!(Hermitian(A))
return Eigen(LAPACK.geevx!(permute ? (scale ? 'B' : 'P') : (scale ? 'S' : 'N'), 'N', 'V', 'N', A)[[2,4]]...)
end
Expand Down Expand Up @@ -317,7 +317,7 @@ function eigfact!(A::StridedMatrix{T}, B::StridedMatrix{T}) where T<:BlasReal
alphar, alphai, beta, _, vr = LAPACK.ggev!('N', 'V', A, B)
iszero(alphai) && return GeneralizedEigen(alphar ./ beta, vr)

vecs = zeros(Complex{T}, n, n)
vecs = Matrix{Complex{T}}(unininitialized, n, n)
j = 1
while j <= n
if alphai[j] == 0
Expand Down
4 changes: 2 additions & 2 deletions base/linalg/hessenberg.jl
Original file line number Diff line number Diff line change
Expand Up @@ -67,9 +67,9 @@ function getproperty(F::Hessenberg, d::Symbol)
end

function getindex(A::HessenbergQ, i::Integer, j::Integer)
x = zeros(eltype(A), size(A, 1))
x = fill(zero(eltype(A)), size(A, 1))
x[i] = 1
y = zeros(eltype(A), size(A, 2))
y = fill(zero(eltype(A)), size(A, 2))
y[j] = 1
return dot(x, mul!(A, y))
end
Expand Down
23 changes: 11 additions & 12 deletions base/linalg/lapack.jl
Original file line number Diff line number Diff line change
Expand Up @@ -736,7 +736,7 @@ and `tau`, which stores the elementary reflectors.
"""
function geqp3!(A::AbstractMatrix{<:BlasFloat})
m, n = size(A)
geqp3!(A, zeros(BlasInt, n), similar(A, min(m, n)))
geqp3!(A, fill(zero(BlasInt), n), similar(A, min(m, n)))
end

## Complete orthogonaliztion tools
Expand Down Expand Up @@ -1206,7 +1206,7 @@ for (gelsd, gelsy, elty) in
if size(B, 1) != m
throw(DimensionMismatch("B has leading dimension $(size(B,1)) but needs $m"))
end
newB = [B; zeros($elty, max(0, n - size(B, 1)), size(B, 2))]
newB = [B; fill(zero($elty), max(0, n - size(B, 1)), size(B, 2))]
s = similar(A, $elty, min(m, n))
rnk = Ref{BlasInt}()
info = Ref{BlasInt}()
Expand Down Expand Up @@ -1250,10 +1250,10 @@ for (gelsd, gelsy, elty) in
if size(B, 1) != m
throw(DimensionMismatch("B has leading dimension $(size(B,1)) but needs $m"))
end
newB = [B; zeros($elty, max(0, n - size(B, 1)), size(B, 2))]
newB = [B; fill(zero($elty), max(0, n - size(B, 1)), size(B, 2))]
lda = max(1, stride(A,2))
ldb = max(1, stride(newB,2))
jpvt = zeros(BlasInt, n)
jpvt = fill(zero(BlasInt), n)
rnk = Ref{BlasInt}()
work = Vector{$elty}(uninitialized, 1)
lwork = BlasInt(-1)
Expand Down Expand Up @@ -1299,7 +1299,7 @@ for (gelsd, gelsy, elty, relty) in
if size(B, 1) != m
throw(DimensionMismatch("B has leading dimension $(size(B,1)) but needs $m"))
end
newB = [B; zeros($elty, max(0, n - size(B, 1)), size(B, 2))]
newB = [B; fill(zero($elty), max(0, n - size(B, 1)), size(B, 2))]
s = similar(A, $relty, min(m, n))
rnk = Ref{BlasInt}()
info = Ref{BlasInt}()
Expand Down Expand Up @@ -1345,10 +1345,10 @@ for (gelsd, gelsy, elty, relty) in
if size(B, 1) != m
throw(DimensionMismatch("B has leading dimension $(size(B,1)) but needs $m"))
end
newB = [B; zeros($elty, max(0, n - size(B, 1)), size(B, 2))]
newB = [B; fill(zero($elty), max(0, n - size(B, 1)), size(B, 2))]
lda = max(1, m)
ldb = max(1, m, n)
jpvt = zeros(BlasInt, n)
jpvt = fill(zero(BlasInt), n)
rnk = Ref{BlasInt}(1)
work = Vector{$elty}(uninitialized, 1)
lwork = BlasInt(-1)
Expand Down Expand Up @@ -1424,7 +1424,7 @@ for (gglse, elty) in ((:dgglse_, :Float64),
if length(d) != p
throw(DimensionMismatch("d has length $(length(d)), needs $p"))
end
X = zeros($elty, n)
X = fill(zero($elty), n)
info = Ref{BlasInt}()
work = Vector{$elty}(uninitialized, 1)
lwork = BlasInt(-1)
Expand Down Expand Up @@ -3721,7 +3721,7 @@ for (stev, stebz, stegr, stein, elty) in
if length(ev_in) != n - 1
throw(DimensionMismatch("ev_in has length $(length(ev_in)) but needs one less than dv's length, $n)"))
end
ev = [ev_in; zeros($elty,1)]
ev = [ev_in; fill(zero($elty),1)]
ldz = n #Leading dimension
#Number of eigenvalues to find
if !(1 <= length(w_in) <= n)
Expand Down Expand Up @@ -3768,10 +3768,9 @@ end
stegr!(jobz::Char, dv::AbstractVector, ev::AbstractVector) = stegr!(jobz, 'A', dv, ev, 0.0, 0.0, 0, 0)

# Allow user to skip specification of iblock and isplit
stein!(dv::AbstractVector, ev::AbstractVector, w_in::AbstractVector) = stein!(dv, ev, w_in, zeros(BlasInt,0), zeros(BlasInt,0))
stein!(dv::AbstractVector, ev::AbstractVector, w_in::AbstractVector) = stein!(dv, ev, w_in, BlasInt[], BlasInt[])
# Allow user to specify just one eigenvector to get in stein!
stein!(dv::AbstractVector, ev::AbstractVector, eval::Real) = stein!(dv, ev, [eval], zeros(BlasInt,0), zeros(BlasInt,0))

stein!(dv::AbstractVector, ev::AbstractVector, eval::Real) = stein!(dv, ev, [eval], BlasInt[], BlasInt[])
"""
stev!(job, dv, ev) -> (dv, Zmat)

Expand Down
Loading