From 560bf4a81201db103e3b4a597af80644d830554d Mon Sep 17 00:00:00 2001 From: Jeff Bezanson Date: Thu, 7 Feb 2019 14:38:59 -0500 Subject: [PATCH] improve some type information to reduce backedges; fixes #29166 reuse `Base._array_for` in lowering instead of manually inlining it --- base/abstractarray.jl | 4 ++-- base/complex.jl | 2 +- base/fastmath.jl | 4 ++-- base/gmp.jl | 6 +++--- base/intfuncs.jl | 23 +++++++++++++---------- base/io.jl | 2 +- base/mpfr.jl | 2 +- base/rational.jl | 2 +- base/reflection.jl | 4 ++-- base/show.jl | 19 +++++++++++-------- base/strings/basic.jl | 3 +-- base/strings/io.jl | 4 ++-- base/strings/util.jl | 2 +- src/julia-syntax.scm | 6 +----- stdlib/Distributed/src/cluster.jl | 6 +++--- stdlib/Distributed/src/managers.jl | 2 +- stdlib/LibGit2/src/callbacks.jl | 2 +- stdlib/Serialization/src/Serialization.jl | 10 +++++----- 18 files changed, 52 insertions(+), 51 deletions(-) diff --git a/base/abstractarray.jl b/base/abstractarray.jl index feec8c6d1ff4b..8e9bff0c78da2 100644 --- a/base/abstractarray.jl +++ b/base/abstractarray.jl @@ -669,7 +669,7 @@ function copyto!(dest::AbstractArray, src) y = iterate(destiter) for x in src y === nothing && - throw(ArgumentError(string("destination has fewer elements than required"))) + throw(ArgumentError("destination has fewer elements than required")) dest[y[1]] = x y = iterate(destiter, y[2]) end @@ -700,7 +700,7 @@ function copyto!(dest::AbstractArray, dstart::Integer, src, sstart::Integer) end if y === nothing throw(ArgumentError(string("source has fewer elements than required, ", - "expected at least ",sstart,", got ",sstart-1))) + "expected at least ",sstart,", got ",sstart-1))) end i = Int(dstart) while y != nothing diff --git a/base/complex.jl b/base/complex.jl index f34290d30bc4c..23117f755b799 100644 --- a/base/complex.jl +++ b/base/complex.jl @@ -35,7 +35,7 @@ const ComplexF16 = Complex{Float16} Complex{T}(x::Real) where {T<:Real} = Complex{T}(x,0) Complex{T}(z::Complex) where {T<:Real} = Complex{T}(real(z),imag(z)) (::Type{T})(z::Complex) where {T<:Real} = - isreal(z) ? T(real(z))::T : throw(InexactError(Symbol(string(T)), T, z)) + isreal(z) ? T(real(z))::T : throw(InexactError(nameof(T), T, z)) Complex(z::Complex) = z diff --git a/base/fastmath.jl b/base/fastmath.jl index 0dccf8ba4bfc2..7ad6fc9bd3e60 100644 --- a/base/fastmath.jl +++ b/base/fastmath.jl @@ -105,9 +105,9 @@ function make_fastmath(expr::Expr) elseif isa(var, Expr) && var.head === :ref # array reference arr = var.args[1] - inds = tuple(var.args[2:end]...) + inds = var.args[2:end] arrvar = gensym() - indvars = tuple([gensym() for i in inds]...) + indvars = Any[gensym() for i in inds] expr = quote $(Expr(:(=), arrvar, arr)) $(Expr(:(=), Expr(:tuple, indvars...), Expr(:tuple, inds...))) diff --git a/base/gmp.jl b/base/gmp.jl index ff85e670d3c71..09898ad108246 100644 --- a/base/gmp.jl +++ b/base/gmp.jl @@ -321,7 +321,7 @@ function (::Type{T})(x::BigInt) where T<:Base.BitUnsigned if sizeof(T) < sizeof(Limb) convert(T, convert(Limb,x)) else - 0 <= x.size <= cld(sizeof(T),sizeof(Limb)) || throw(InexactError(Symbol(string(T)), T, x)) + 0 <= x.size <= cld(sizeof(T),sizeof(Limb)) || throw(InexactError(nameof(T), T, x)) x % T end end @@ -332,9 +332,9 @@ function (::Type{T})(x::BigInt) where T<:Base.BitSigned SLimb = typeof(Signed(one(Limb))) convert(T, convert(SLimb, x)) else - 0 <= n <= cld(sizeof(T),sizeof(Limb)) || throw(InexactError(Symbol(string(T)), T, x)) + 0 <= n <= cld(sizeof(T),sizeof(Limb)) || throw(InexactError(nameof(T), T, x)) y = x % T - ispos(x) ⊻ (y > 0) && throw(InexactError(Symbol(string(T)), T, x)) # catch overflow + ispos(x) ⊻ (y > 0) && throw(InexactError(nameof(T), T, x)) # catch overflow y end end diff --git a/base/intfuncs.jl b/base/intfuncs.jl index da758c2c60941..9141ce53bffa8 100644 --- a/base/intfuncs.jl +++ b/base/intfuncs.jl @@ -409,12 +409,12 @@ const powers_of_ten = [ 0x000000e8d4a51000, 0x000009184e72a000, 0x00005af3107a4000, 0x00038d7ea4c68000, 0x002386f26fc10000, 0x016345785d8a0000, 0x0de0b6b3a7640000, 0x8ac7230489e80000, ] -function ndigits0z(x::Base.BitUnsigned64) +function bit_ndigits0z(x::Base.BitUnsigned64) lz = (sizeof(x)<<3)-leading_zeros(x) nd = (1233*lz)>>12+1 nd -= x < powers_of_ten[nd] end -function ndigits0z(x::UInt128) +function bit_ndigits0z(x::UInt128) n = 0 while x > 0x8ac7230489e80000 x = div(x,0x8ac7230489e80000) @@ -423,16 +423,20 @@ function ndigits0z(x::UInt128) return n + ndigits0z(UInt64(x)) end -ndigits0z(x::BitSigned) = ndigits0z(unsigned(abs(x))) - +ndigits0z(x::BitSigned) = bit_ndigits0z(unsigned(abs(x))) +ndigits0z(x::BitUnsigned) = bit_ndigits0z(x) ndigits0z(x::Integer) = ndigits0zpb(x, 10) ## ndigits with specified base ## # The suffix "nb" stands for "negative base" function ndigits0znb(x::Integer, b::Integer) - # precondition: b < -1 && !(typeof(x) <: Unsigned) d = 0 + if x isa Unsigned + d += (x != 0)::Bool + x = -signed(fld(x, -b)) + end + # precondition: b < -1 && !(typeof(x) <: Unsigned) while x != 0 x = cld(x,b) d += 1 @@ -441,7 +445,6 @@ function ndigits0znb(x::Integer, b::Integer) end # do first division before conversion with signed here, which can otherwise overflow -ndigits0znb(x::Unsigned, b::Integer) = ndigits0znb(-signed(fld(x, -b)), b) + (x != 0) ndigits0znb(x::Bool, b::Integer) = x % Int # The suffix "pb" stands for "positive base" @@ -451,11 +454,11 @@ function ndigits0zpb(x::Integer, b::Integer) b = Int(b) x = abs(x) if x isa Base.BitInteger - x = unsigned(x) + x = unsigned(x)::Unsigned b == 2 && return sizeof(x)<<3 - leading_zeros(x) b == 8 && return (sizeof(x)<<3 - leading_zeros(x) + 2) ÷ 3 b == 16 && return sizeof(x)<<1 - leading_zeros(x)>>2 - b == 10 && return ndigits0z(x) + b == 10 && return bit_ndigits0z(x) end d = 0 @@ -595,7 +598,7 @@ const base62digits = ['0':'9';'A':'Z';'a':'z'] function _base(b::Integer, x::Integer, pad::Integer, neg::Bool) (x >= 0) | (b < 0) || throw(DomainError(x, "For negative `x`, `b` must be negative.")) - 2 <= abs(b) <= 62 || throw(ArgumentError("base must satisfy 2 ≤ abs(base) ≤ 62, got $b")) + 2 <= abs(b) <= 62 || throw(DomainError(b, "base must satisfy 2 ≤ abs(base) ≤ 62")) digits = abs(b) <= 36 ? base36digits : base62digits i = neg + ndigits(x, base=b, pad=pad) a = StringVector(i) @@ -745,7 +748,7 @@ julia> digits!([2,2,2,2,2,2], 10, base = 2) ``` """ function digits!(a::AbstractVector{T}, n::Integer; base::Integer = 10) where T<:Integer - 2 <= abs(base) || throw(ArgumentError("base must be ≥ 2 or ≤ -2, got $base")) + 2 <= abs(base) || throw(DomainError(base, "base must be ≥ 2 or ≤ -2")) hastypemax(T) && abs(base) - 1 > typemax(T) && throw(ArgumentError("type $T too small for base $base")) isempty(a) && return a diff --git a/base/io.jl b/base/io.jl index e9bbc474016fb..e14caf4ac323b 100644 --- a/base/io.jl +++ b/base/io.jl @@ -379,7 +379,7 @@ function readline(filename::AbstractString; keep::Bool=false) end end -function readline(s::IO=stdin; keep::Bool=false) +function readline(s::IO=stdin; keep::Bool=false)::String line = readuntil(s, 0x0a, keep=true) i = length(line) if keep || i == 0 || line[i] != 0x0a diff --git a/base/mpfr.jl b/base/mpfr.jl index 7b35d436b690f..b3a704c0b084f 100644 --- a/base/mpfr.jl +++ b/base/mpfr.jl @@ -331,7 +331,7 @@ function BigInt(x::BigFloat) end function (::Type{T})(x::BigFloat) where T<:Integer - isinteger(x) || throw(InexactError(Symbol(string(T)), T, x)) + isinteger(x) || throw(InexactError(nameof(T), T, x)) trunc(T,x) end diff --git a/base/rational.jl b/base/rational.jl index b6542a8f05b27..1e8382584a597 100644 --- a/base/rational.jl +++ b/base/rational.jl @@ -85,7 +85,7 @@ Rational(x::Rational) = x Bool(x::Rational) = x==0 ? false : x==1 ? true : throw(InexactError(:Bool, Bool, x)) # to resolve ambiguity (::Type{T})(x::Rational) where {T<:Integer} = (isinteger(x) ? convert(T, x.num) : - throw(InexactError(Symbol(string(T)), T, x))) + throw(InexactError(nameof(T), T, x))) AbstractFloat(x::Rational) = float(x.num)/float(x.den) function (::Type{T})(x::Rational{S}) where T<:AbstractFloat where S diff --git a/base/reflection.jl b/base/reflection.jl index f366c467451ce..c02600fcd8967 100644 --- a/base/reflection.jl +++ b/base/reflection.jl @@ -208,7 +208,7 @@ julia> nameof(Foo.S{T} where T) ``` """ nameof(t::DataType) = t.name.name -nameof(t::UnionAll) = nameof(unwrap_unionall(t)) +nameof(t::UnionAll) = nameof(unwrap_unionall(t))::Symbol """ parentmodule(t::DataType) -> Module @@ -1043,7 +1043,7 @@ end Get the name of a generic `Function` as a symbol, or `:anonymous`. """ -nameof(f::Function) = typeof(f).name.mt.name +nameof(f::Function) = (typeof(f).name.mt::Core.MethodTable).name functionloc(m::Core.MethodInstance) = functionloc(m.def) diff --git a/base/show.jl b/base/show.jl index 410a48137e016..b377a511f9018 100644 --- a/base/show.jl +++ b/base/show.jl @@ -399,11 +399,6 @@ end show(io::IO, ::Core.TypeofBottom) = print(io, "Union{}") -function show(io::IO, x::Union) - print(io, "Union") - show_delim_array(io, uniontypes(x), '{', ',', '}', false) -end - function print_without_params(@nospecialize(x)) if isa(x,UnionAll) b = unwrap_unionall(x) @@ -424,7 +419,17 @@ function io_has_tvar_name(io::IOContext, name::Symbol, @nospecialize(x)) end io_has_tvar_name(io::IO, name::Symbol, @nospecialize(x)) = false -function show(io::IO, x::UnionAll) +function show(io::IO, @nospecialize(x::Type)) + if x isa DataType + show_datatype(io, x) + return + elseif x isa Union + print(io, "Union") + show_delim_array(io, uniontypes(x), '{', ',', '}', false) + return + end + x::UnionAll + if print_without_params(x) return show(io, unwrap_unionall(x).name) end @@ -447,8 +452,6 @@ function show(io::IO, x::UnionAll) show(io, x.var) end -show(io::IO, x::DataType) = show_datatype(io, x) - # Check whether 'sym' (defined in module 'parent') is visible from module 'from' # If an object with this name exists in 'from', we need to check that it's the same binding # and that it's not deprecated. diff --git a/base/strings/basic.jl b/base/strings/basic.jl index 97c7e5e1467c5..d7c9cb279f7e4 100644 --- a/base/strings/basic.jl +++ b/base/strings/basic.jl @@ -571,8 +571,7 @@ function map(f, s::AbstractString) for c in s c′ = f(c) isa(c′, AbstractChar) || throw(ArgumentError( - "map(f, s::AbstractString) requires f to return AbstractChar; " * - "try map(f, collect(s)) or a comprehension instead")) + "map(f, s::AbstractString) requires f to return AbstractChar; try map(f, collect(s)) or a comprehension instead")) write(out, c′::AbstractChar) end String(take!(out)) diff --git a/base/strings/io.jl b/base/strings/io.jl index 3125331b49ec7..d9bfc3cf4f462 100644 --- a/base/strings/io.jl +++ b/base/strings/io.jl @@ -119,7 +119,7 @@ function print_to_string(xs...) if isempty(xs) return "" end - siz = 0 + siz::Int = 0 for x in xs siz += tostr_sizehint(x) end @@ -135,7 +135,7 @@ function string_with_env(env, xs...) if isempty(xs) return "" end - siz = 0 + siz::Int = 0 for x in xs siz += tostr_sizehint(x) end diff --git a/base/strings/util.jl b/base/strings/util.jl index 270e23dfeca61..a2cf7f23c6693 100644 --- a/base/strings/util.jl +++ b/base/strings/util.jl @@ -602,7 +602,7 @@ function ascii(s::String) end return s end -@noinline __throw_invalid_ascii(s, i) = throw(ArgumentError("invalid ASCII at index $i in $(repr(s))")) +@noinline __throw_invalid_ascii(s::String, i::Int) = throw(ArgumentError("invalid ASCII at index $i in $(repr(s))")) """ ascii(s::AbstractString) diff --git a/src/julia-syntax.scm b/src/julia-syntax.scm index 508615d87f07f..ec3dd5af3cdaf 100644 --- a/src/julia-syntax.scm +++ b/src/julia-syntax.scm @@ -2319,11 +2319,7 @@ (= ,szunk (call (core isa) ,isz (top SizeUnknown))) (if ,szunk (= ,result (call (curly (core Array) ,ty 1) (core undef) 0)) - (if (call (core isa) ,isz (top HasShape)) - (= ,result (call (top similar) (curly (core Array) ,ty) (call (top axes) ,overall-itr))) - (= ,result (call (curly (core Array) ,ty 1) (core undef) (call (core Int) - (|::| (call (top length) ,overall-itr) - (core Integer))))))) + (= ,result (call (top _array_for) ,ty ,overall-itr ,isz))) (= ,idx (call (top first) (call (top LinearIndices) ,result))) ,(construct-loops (reverse itrs) (reverse iv)) ,result))))) diff --git a/stdlib/Distributed/src/cluster.jl b/stdlib/Distributed/src/cluster.jl index 2afb376b16e0a..f753fe607250c 100644 --- a/stdlib/Distributed/src/cluster.jl +++ b/stdlib/Distributed/src/cluster.jl @@ -6,7 +6,7 @@ mutable struct WorkerConfig # Common fields relevant to all cluster managers io::Union{IO, Nothing} host::Union{AbstractString, Nothing} - port::Union{Integer, Nothing} + port::Union{Int, Nothing} # Used when launching additional workers at a host count::Union{Int, Symbol, Nothing} @@ -21,13 +21,13 @@ mutable struct WorkerConfig tunnel::Union{Bool, Nothing} bind_addr::Union{AbstractString, Nothing} sshflags::Union{Cmd, Nothing} - max_parallel::Union{Integer, Nothing} + max_parallel::Union{Int, Nothing} # Used by Local/SSH managers connect_at::Any process::Union{Process, Nothing} - ospid::Union{Integer, Nothing} + ospid::Union{Int, Nothing} # Private dictionary used to store temporary information by Local/SSH managers. environ::Union{Dict, Nothing} diff --git a/stdlib/Distributed/src/managers.jl b/stdlib/Distributed/src/managers.jl index 1b48bc8537507..c3a877805b321 100644 --- a/stdlib/Distributed/src/managers.jl +++ b/stdlib/Distributed/src/managers.jl @@ -287,7 +287,7 @@ end # LocalManager struct LocalManager <: ClusterManager - np::Integer + np::Int restrict::Bool # Restrict binding to 127.0.0.1 only end diff --git a/stdlib/LibGit2/src/callbacks.jl b/stdlib/LibGit2/src/callbacks.jl index 37b0414c42118..ed2d9de32c2db 100644 --- a/stdlib/LibGit2/src/callbacks.jl +++ b/stdlib/LibGit2/src/callbacks.jl @@ -344,7 +344,7 @@ end function credentials_callback(libgit2credptr::Ptr{Ptr{Cvoid}}, url_ptr::Cstring, username_ptr::Cstring, allowed_types::Cuint, - payloads::Dict) + payloads::Dict{Symbol, Any}) p = payloads[:credentials] return credentials_callback(libgit2credptr, url_ptr, username_ptr, allowed_types, p) end diff --git a/stdlib/Serialization/src/Serialization.jl b/stdlib/Serialization/src/Serialization.jl index 2367400ebee6b..f5779b73d2603 100644 --- a/stdlib/Serialization/src/Serialization.jl +++ b/stdlib/Serialization/src/Serialization.jl @@ -512,7 +512,7 @@ function should_send_whole_type(s, t::DataType) return false end -function serialize_type_data(s, t::DataType) +function serialize_type_data(s, @nospecialize(t::DataType)) whole = should_send_whole_type(s, t) iswrapper = (t === unwrap_unionall(t.name.wrapper)) if whole && iswrapper @@ -557,7 +557,7 @@ function serialize(s::AbstractSerializer, t::DataType) serialize_type_data(s, t) end -function serialize_type(s::AbstractSerializer, t::DataType, ref::Bool = false) +function serialize_type(s::AbstractSerializer, @nospecialize(t::DataType), ref::Bool = false) tag = sertag(t) tag > 0 && return writetag(s.io, tag) writetag(s.io, ref ? REF_OBJECT_TAG : OBJECT_TAG) @@ -974,15 +974,15 @@ function deserialize_array(s::AbstractSerializer) else elty = UInt8 end - if isa(d1, Integer) + if isa(d1, Int) if elty !== Bool && isbitstype(elty) a = Vector{elty}(undef, d1) s.table[slot] = a return read!(s.io, a) end - dims = (Int(d1),) + dims = (d1,) else - dims = convert(Dims, d1)::Dims + dims = d1::Dims end if isbitstype(elty) n = prod(dims)::Int