Skip to content

Commit

Permalink
Reduce reliance on promotion operations via container typing (JuliaLa…
Browse files Browse the repository at this point in the history
…ng#37088)

While building Julia, we have a *lot* of calls to promote, often
with abstract types. This short-circuits most of these by declaring
the container type at the outset.

This has one user-visible outcome, improved inference for `walkdir`.
  • Loading branch information
timholy committed Aug 19, 2020
1 parent 0dab9e3 commit 4c7026c
Show file tree
Hide file tree
Showing 14 changed files with 41 additions and 41 deletions.
2 changes: 1 addition & 1 deletion base/docs/Docs.jl
Original file line number Diff line number Diff line change
Expand Up @@ -608,7 +608,7 @@ Core.atdoc!(docm)

function loaddocs(docs)
for (mod, ex, str, file, line) in docs
data = Dict(:path => string(file), :linenumber => line)
data = Dict{Symbol,Any}(:path => string(file), :linenumber => line)
doc = docstr(str, data)
docstring = docm(LineNumberNode(line, file), mod, doc, ex, false) # expand the real @doc macro now
Core.eval(mod, Expr(Core.unescape, docstring, Docs))
Expand Down
4 changes: 2 additions & 2 deletions base/errorshow.jl
Original file line number Diff line number Diff line change
Expand Up @@ -281,7 +281,7 @@ function showerror(io::IO, ex::MethodError)
if any(x -> x <: AbstractArray{<:Number}, arg_types_param) &&
any(x -> x <: Number, arg_types_param)

nouns = Dict(
nouns = Dict{Any,String}(
Base.:+ => "addition",
Base.:- => "subtraction",
)
Expand Down Expand Up @@ -381,7 +381,7 @@ function show_method_candidates(io::IO, ex::MethodError, @nospecialize kwargs=()
lines = []
# These functions are special cased to only show if first argument is matched.
special = f === convert || f === getindex || f === setindex!
funcs = Any[(f, arg_types_param)]
funcs = Tuple{Any,Vector{Any}}[(f, arg_types_param)]

# An incorrect call method produces a MethodError for convert.
# It also happens that users type convert when they mean call. So
Expand Down
2 changes: 1 addition & 1 deletion base/file.jl
Original file line number Diff line number Diff line change
Expand Up @@ -876,7 +876,7 @@ function walkdir(root; topdown=true, follow_symlinks=false, onerror=throw)
end
nothing
end
return Channel(chnl -> _walkdir(chnl, root))
return Channel{Tuple{String,Vector{String},Vector{String}}}(chnl -> _walkdir(chnl, root))
end

function unlink(p::AbstractString)
Expand Down
2 changes: 1 addition & 1 deletion base/initdefs.jl
Original file line number Diff line number Diff line change
Expand Up @@ -210,7 +210,7 @@ function init_load_path()
paths = parse_load_path(ENV["JULIA_LOAD_PATH"])
else
paths = filter!(env -> env !== nothing,
[env == "@." ? current_project() : env for env in DEFAULT_LOAD_PATH])
String[env == "@." ? current_project() : env for env in DEFAULT_LOAD_PATH])
end
append!(empty!(LOAD_PATH), paths)
end
Expand Down
2 changes: 1 addition & 1 deletion base/multidimensional.jl
Original file line number Diff line number Diff line change
Expand Up @@ -1419,7 +1419,7 @@ function checkdims_perm(P::AbstractArray{TP,N}, B::AbstractArray{TB,N}, perm) wh
nothing
end

for (V, PT, BT) in [((:N,), BitArray, BitArray), ((:T,:N), Array, StridedArray)]
for (V, PT, BT) in Any[((:N,), BitArray, BitArray), ((:T,:N), Array, StridedArray)]
@eval @generated function permutedims!(P::$PT{$(V...)}, B::$BT{$(V...)}, perm) where $(V...)
quote
checkdims_perm(P, B, perm)
Expand Down
27 changes: 13 additions & 14 deletions base/version.jl
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@

## semantic version numbers (https://semver.org/)

const VerTuple = Tuple{Vararg{Union{UInt64,String}}}

const VInt = UInt32
"""
VersionNumber
Expand All @@ -24,12 +26,12 @@ struct VersionNumber
major::VInt
minor::VInt
patch::VInt
prerelease::Tuple{Vararg{Union{UInt64,String}}}
build::Tuple{Vararg{Union{UInt64,String}}}
prerelease::VerTuple
build::VerTuple

function VersionNumber(major::VInt, minor::VInt, patch::VInt,
pre::Tuple{Vararg{Union{UInt64,String}}},
bld::Tuple{Vararg{Union{UInt64,String}}})
pre::VerTuple,
bld::VerTuple)
major >= 0 || throw(ArgumentError("invalid negative major version: $major"))
minor >= 0 || throw(ArgumentError("invalid negative minor version: $minor"))
patch >= 0 || throw(ArgumentError("invalid negative patch version: $patch"))
Expand Down Expand Up @@ -118,7 +120,7 @@ function VersionNumber(v::AbstractString)
end
prerl = prerl !== nothing ? split_idents(prerl) : minus !== nothing ? ("",) : ()
build = build !== nothing ? split_idents(build) : plus !== nothing ? ("",) : ()
return VersionNumber(major, minor, patch, prerl, build)
return VersionNumber(major, minor, patch, prerl::VerTuple, build::VerTuple)
end

parse(::Type{VersionNumber}, v::AbstractString) = VersionNumber(v)
Expand Down Expand Up @@ -149,25 +151,22 @@ v"2.0.1-rc1"
"""
macro v_str(v); VersionNumber(v); end

typemin(::Type{VersionNumber}) = v"0-"

function typemax(::Type{VersionNumber})
= typemax(VInt)
VersionNumber(∞, ∞, ∞, (), ("",))
end

typemin(::Type{VersionNumber}) = v"0-"

ident_cmp(a::Integer, b::Integer) = cmp(a, b)
ident_cmp(a::Integer, b::String ) = isempty(b) ? +1 : -1
ident_cmp(a::String, b::Integer) = isempty(a) ? -1 : +1
ident_cmp(a::String, b::String ) = cmp(a, b)

function ident_cmp(
A::Tuple{Vararg{Union{Integer,String}}},
B::Tuple{Vararg{Union{Integer,String}}},
)
for (a, b) in zip(A, B)
c = ident_cmp(a,b)::Int
(c != 0) && return c
function ident_cmp(A::VerTuple, B::VerTuple)
for (a, b) in Iterators.Zip{Tuple{VerTuple,VerTuple}}((A, B))
c = ident_cmp(a, b)
(c != 0) && return c
end
length(A) < length(B) ? -1 :
length(B) < length(A) ? +1 : 0
Expand Down
2 changes: 1 addition & 1 deletion stdlib/Base64/src/encode.jl
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
# This file is a part of Julia. License is MIT: https://julialang.org/license

# Generate encode table.
const BASE64_ENCODE = [UInt8(x) for x in ['A':'Z'; 'a':'z'; '0':'9'; '+'; '/']]
const BASE64_ENCODE = [UInt8(x) for x in Char['A':'Z'; 'a':'z'; '0':'9'; '+'; '/']]
encode(x::UInt8) = @inbounds return BASE64_ENCODE[(x & 0x3f) + 1]
encodepadding() = UInt8('=')

Expand Down
8 changes: 4 additions & 4 deletions stdlib/Dates/src/io.jl
Original file line number Diff line number Diff line change
Expand Up @@ -129,7 +129,7 @@ function tryparsenext(d::DatePart{'p'}, str, i, len)
return ap == 'a' ? AM : PM, ii
end

for (tok, fn) in zip("uUeE", [monthabbr_to_value, monthname_to_value, dayabbr_to_value, dayname_to_value])
for (tok, fn) in zip("uUeE", Any[monthabbr_to_value, monthname_to_value, dayabbr_to_value, dayname_to_value])
@eval @inline function tryparsenext(d::DatePart{$tok}, str, i, len, locale)
next = tryparsenext_word(str, i, len, locale, max_width(d))
next === nothing && return nothing
Expand Down Expand Up @@ -161,13 +161,13 @@ end

hour12(dt) = let h = hour(dt); h > 12 ? h - 12 : h == 0 ? 12 : h; end

for (c, fn) in zip("YmdHIMS", [year, month, day, hour, hour12, minute, second])
for (c, fn) in zip("YmdHIMS", Any[year, month, day, hour, hour12, minute, second])
@eval function format(io, d::DatePart{$c}, dt)
print(io, string($fn(dt), base = 10, pad = d.width))
end
end

for (tok, fn) in zip("uU", [monthabbr, monthname])
for (tok, fn) in zip("uU", Any[monthabbr, monthname])
@eval function format(io, d::DatePart{$tok}, dt, locale)
print(io, $fn(month(dt), locale))
end
Expand All @@ -178,7 +178,7 @@ function format(io, d::DatePart{'p'}, dt, locale)
print(io, ampm)
end

for (tok, fn) in zip("eE", [dayabbr, dayname])
for (tok, fn) in zip("eE", Any[dayabbr, dayname])
@eval function format(io, ::DatePart{$tok}, dt, locale)
print(io, $fn(dayofweek(dt), locale))
end
Expand Down
6 changes: 3 additions & 3 deletions stdlib/InteractiveUtils/src/editless.jl
Original file line number Diff line number Diff line change
Expand Up @@ -114,11 +114,11 @@ function define_default_editors()
define_editor(r".*") do cmd, path, line
`$cmd $path`
end
define_editor([r"\bemacs", "gedit", r"\bgvim"]) do cmd, path, line
define_editor(Any[r"\bemacs", "gedit", r"\bgvim"]) do cmd, path, line
`$cmd +$line $path`
end
# Must check that emacs not running in -t/-nw before regex match for general emacs
define_editor([
define_editor(Any[
"vim", "vi", "nvim", "mvim", "nano", "micro",
r"\bemacs\b.*\s(-nw|--no-window-system)\b",
r"\bemacsclient\b.\s*-(-?nw|t|-?tty)\b"], wait=true) do cmd, path, line
Expand All @@ -127,7 +127,7 @@ function define_default_editors()
define_editor(["textmate", "mate", "kate"]) do cmd, path, line
`$cmd $path -l $line`
end
define_editor([r"\bsubl", r"\batom", "pycharm", "bbedit"]) do cmd, path, line
define_editor(Any[r"\bsubl", r"\batom", "pycharm", "bbedit"]) do cmd, path, line
`$cmd $path:$line`
end
define_editor(["code", "code-insiders"]) do cmd, path, line
Expand Down
2 changes: 1 addition & 1 deletion stdlib/LibGit2/src/types.jl
Original file line number Diff line number Diff line change
Expand Up @@ -1007,7 +1007,7 @@ function Base.getproperty(obj::GitObject, name::Symbol)
end
end

for (typ, owntyp, sup, cname) in [
for (typ, owntyp, sup, cname) in Tuple{Symbol,Any,Symbol,Symbol}[
(:GitRepo, nothing, :AbstractGitObject, :git_repository),
(:GitConfig, :(Union{GitRepo, Nothing}), :AbstractGitObject, :git_config),
(:GitIndex, :(Union{GitRepo, Nothing}), :AbstractGitObject, :git_index),
Expand Down
12 changes: 6 additions & 6 deletions stdlib/LinearAlgebra/src/triangular.jl
Original file line number Diff line number Diff line change
Expand Up @@ -467,12 +467,12 @@ end

# Define `mul!` for (Unit){Upper,Lower}Triangular matrices times a
# number.
for (Trig, UnitTrig) in [(UpperTriangular, UnitUpperTriangular),
(LowerTriangular, UnitLowerTriangular)]
for (TB, TC) in [(Trig, Number),
(Number, Trig),
(UnitTrig, Number),
(Number, UnitTrig)]
for (Trig, UnitTrig) in Any[(UpperTriangular, UnitUpperTriangular),
(LowerTriangular, UnitLowerTriangular)]
for (TB, TC) in Any[(Trig, Number),
(Number, Trig),
(UnitTrig, Number),
(Number, UnitTrig)]
@eval @inline mul!(A::$Trig, B::$TB, C::$TC, alpha::Number, beta::Number) =
_mul!(A, B, C, MulAddMul(alpha, beta))
end
Expand Down
3 changes: 2 additions & 1 deletion stdlib/REPL/src/LineEdit.jl
Original file line number Diff line number Diff line change
Expand Up @@ -201,7 +201,8 @@ end
beep(::ModeState) = nothing
cancel_beep(::ModeState) = nothing

for f in [:terminal, :on_enter, :add_history, :_buffer, :(Base.isempty),
for f in Union{Symbol,Expr}[
:terminal, :on_enter, :add_history, :_buffer, :(Base.isempty),
:replace_line, :refresh_multi_line, :input_string, :update_display_buffer,
:empty_undo, :push_undo, :pop_undo, :options, :cancel_beep, :beep,
:deactivate_region, :activate_region, :is_region_active, :region_active]
Expand Down
8 changes: 4 additions & 4 deletions stdlib/REPL/src/REPL.jl
Original file line number Diff line number Diff line change
Expand Up @@ -994,9 +994,9 @@ function setup_interface(

# Setup history
# We will have a unified history for all REPL modes
hp = REPLHistoryProvider(Dict{Symbol,Any}(:julia => julia_prompt,
:shell => shell_mode,
:help => help_mode))
hp = REPLHistoryProvider(Dict{Symbol,Prompt}(:julia => julia_prompt,
:shell => shell_mode,
:help => help_mode))
if repl.history_file
try
hist_path = find_hist_file()
Expand Down Expand Up @@ -1172,7 +1172,7 @@ function setup_interface(

shell_mode.keymap_dict = help_mode.keymap_dict = LineEdit.keymap(b)

allprompts = [julia_prompt, shell_mode, help_mode, search_prompt, prefix_prompt]
allprompts = LineEdit.TextInterface[julia_prompt, shell_mode, help_mode, search_prompt, prefix_prompt]
return ModalInterface(allprompts)
end

Expand Down
2 changes: 1 addition & 1 deletion test/file.jl
Original file line number Diff line number Diff line change
Expand Up @@ -1213,7 +1213,7 @@ cd(dirwalk) do
has_symlinks && symlink(abspath("sub_dir2"), joinpath("sub_dir1", "link"))
for follow_symlinks in follow_symlink_vec
chnl = walkdir(".", follow_symlinks=follow_symlinks)
root, dirs, files = take!(chnl)
root, dirs, files = @inferred(take!(chnl))
@test root == "."
@test dirs == ["sub_dir1", "sub_dir2"]
@test files == ["file1", "file2"]
Expand Down

0 comments on commit 4c7026c

Please sign in to comment.