Skip to content

Commit 4c7026c

Browse files
authored
Reduce reliance on promotion operations via container typing (#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`.
1 parent 0dab9e3 commit 4c7026c

File tree

14 files changed

+41
-41
lines changed

14 files changed

+41
-41
lines changed

base/docs/Docs.jl

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -608,7 +608,7 @@ Core.atdoc!(docm)
608608

609609
function loaddocs(docs)
610610
for (mod, ex, str, file, line) in docs
611-
data = Dict(:path => string(file), :linenumber => line)
611+
data = Dict{Symbol,Any}(:path => string(file), :linenumber => line)
612612
doc = docstr(str, data)
613613
docstring = docm(LineNumberNode(line, file), mod, doc, ex, false) # expand the real @doc macro now
614614
Core.eval(mod, Expr(Core.unescape, docstring, Docs))

base/errorshow.jl

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -281,7 +281,7 @@ function showerror(io::IO, ex::MethodError)
281281
if any(x -> x <: AbstractArray{<:Number}, arg_types_param) &&
282282
any(x -> x <: Number, arg_types_param)
283283

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

386386
# An incorrect call method produces a MethodError for convert.
387387
# It also happens that users type convert when they mean call. So

base/file.jl

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -876,7 +876,7 @@ function walkdir(root; topdown=true, follow_symlinks=false, onerror=throw)
876876
end
877877
nothing
878878
end
879-
return Channel(chnl -> _walkdir(chnl, root))
879+
return Channel{Tuple{String,Vector{String},Vector{String}}}(chnl -> _walkdir(chnl, root))
880880
end
881881

882882
function unlink(p::AbstractString)

base/initdefs.jl

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -210,7 +210,7 @@ function init_load_path()
210210
paths = parse_load_path(ENV["JULIA_LOAD_PATH"])
211211
else
212212
paths = filter!(env -> env !== nothing,
213-
[env == "@." ? current_project() : env for env in DEFAULT_LOAD_PATH])
213+
String[env == "@." ? current_project() : env for env in DEFAULT_LOAD_PATH])
214214
end
215215
append!(empty!(LOAD_PATH), paths)
216216
end

base/multidimensional.jl

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1419,7 +1419,7 @@ function checkdims_perm(P::AbstractArray{TP,N}, B::AbstractArray{TB,N}, perm) wh
14191419
nothing
14201420
end
14211421

1422-
for (V, PT, BT) in [((:N,), BitArray, BitArray), ((:T,:N), Array, StridedArray)]
1422+
for (V, PT, BT) in Any[((:N,), BitArray, BitArray), ((:T,:N), Array, StridedArray)]
14231423
@eval @generated function permutedims!(P::$PT{$(V...)}, B::$BT{$(V...)}, perm) where $(V...)
14241424
quote
14251425
checkdims_perm(P, B, perm)

base/version.jl

Lines changed: 13 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@
22

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

5+
const VerTuple = Tuple{Vararg{Union{UInt64,String}}}
6+
57
const VInt = UInt32
68
"""
79
VersionNumber
@@ -24,12 +26,12 @@ struct VersionNumber
2426
major::VInt
2527
minor::VInt
2628
patch::VInt
27-
prerelease::Tuple{Vararg{Union{UInt64,String}}}
28-
build::Tuple{Vararg{Union{UInt64,String}}}
29+
prerelease::VerTuple
30+
build::VerTuple
2931

3032
function VersionNumber(major::VInt, minor::VInt, patch::VInt,
31-
pre::Tuple{Vararg{Union{UInt64,String}}},
32-
bld::Tuple{Vararg{Union{UInt64,String}}})
33+
pre::VerTuple,
34+
bld::VerTuple)
3335
major >= 0 || throw(ArgumentError("invalid negative major version: $major"))
3436
minor >= 0 || throw(ArgumentError("invalid negative minor version: $minor"))
3537
patch >= 0 || throw(ArgumentError("invalid negative patch version: $patch"))
@@ -118,7 +120,7 @@ function VersionNumber(v::AbstractString)
118120
end
119121
prerl = prerl !== nothing ? split_idents(prerl) : minus !== nothing ? ("",) : ()
120122
build = build !== nothing ? split_idents(build) : plus !== nothing ? ("",) : ()
121-
return VersionNumber(major, minor, patch, prerl, build)
123+
return VersionNumber(major, minor, patch, prerl::VerTuple, build::VerTuple)
122124
end
123125

124126
parse(::Type{VersionNumber}, v::AbstractString) = VersionNumber(v)
@@ -149,25 +151,22 @@ v"2.0.1-rc1"
149151
"""
150152
macro v_str(v); VersionNumber(v); end
151153

152-
typemin(::Type{VersionNumber}) = v"0-"
153-
154154
function typemax(::Type{VersionNumber})
155155
= typemax(VInt)
156156
VersionNumber(∞, ∞, ∞, (), ("",))
157157
end
158158

159+
typemin(::Type{VersionNumber}) = v"0-"
160+
159161
ident_cmp(a::Integer, b::Integer) = cmp(a, b)
160162
ident_cmp(a::Integer, b::String ) = isempty(b) ? +1 : -1
161163
ident_cmp(a::String, b::Integer) = isempty(a) ? -1 : +1
162164
ident_cmp(a::String, b::String ) = cmp(a, b)
163165

164-
function ident_cmp(
165-
A::Tuple{Vararg{Union{Integer,String}}},
166-
B::Tuple{Vararg{Union{Integer,String}}},
167-
)
168-
for (a, b) in zip(A, B)
169-
c = ident_cmp(a,b)::Int
170-
(c != 0) && return c
166+
function ident_cmp(A::VerTuple, B::VerTuple)
167+
for (a, b) in Iterators.Zip{Tuple{VerTuple,VerTuple}}((A, B))
168+
c = ident_cmp(a, b)
169+
(c != 0) && return c
171170
end
172171
length(A) < length(B) ? -1 :
173172
length(B) < length(A) ? +1 : 0

stdlib/Base64/src/encode.jl

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
# This file is a part of Julia. License is MIT: https://julialang.org/license
22

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

stdlib/Dates/src/io.jl

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -129,7 +129,7 @@ function tryparsenext(d::DatePart{'p'}, str, i, len)
129129
return ap == 'a' ? AM : PM, ii
130130
end
131131

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

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

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

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

181-
for (tok, fn) in zip("eE", [dayabbr, dayname])
181+
for (tok, fn) in zip("eE", Any[dayabbr, dayname])
182182
@eval function format(io, ::DatePart{$tok}, dt, locale)
183183
print(io, $fn(dayofweek(dt), locale))
184184
end

stdlib/InteractiveUtils/src/editless.jl

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -114,11 +114,11 @@ function define_default_editors()
114114
define_editor(r".*") do cmd, path, line
115115
`$cmd $path`
116116
end
117-
define_editor([r"\bemacs", "gedit", r"\bgvim"]) do cmd, path, line
117+
define_editor(Any[r"\bemacs", "gedit", r"\bgvim"]) do cmd, path, line
118118
`$cmd +$line $path`
119119
end
120120
# Must check that emacs not running in -t/-nw before regex match for general emacs
121-
define_editor([
121+
define_editor(Any[
122122
"vim", "vi", "nvim", "mvim", "nano", "micro",
123123
r"\bemacs\b.*\s(-nw|--no-window-system)\b",
124124
r"\bemacsclient\b.\s*-(-?nw|t|-?tty)\b"], wait=true) do cmd, path, line
@@ -127,7 +127,7 @@ function define_default_editors()
127127
define_editor(["textmate", "mate", "kate"]) do cmd, path, line
128128
`$cmd $path -l $line`
129129
end
130-
define_editor([r"\bsubl", r"\batom", "pycharm", "bbedit"]) do cmd, path, line
130+
define_editor(Any[r"\bsubl", r"\batom", "pycharm", "bbedit"]) do cmd, path, line
131131
`$cmd $path:$line`
132132
end
133133
define_editor(["code", "code-insiders"]) do cmd, path, line

stdlib/LibGit2/src/types.jl

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1007,7 +1007,7 @@ function Base.getproperty(obj::GitObject, name::Symbol)
10071007
end
10081008
end
10091009

1010-
for (typ, owntyp, sup, cname) in [
1010+
for (typ, owntyp, sup, cname) in Tuple{Symbol,Any,Symbol,Symbol}[
10111011
(:GitRepo, nothing, :AbstractGitObject, :git_repository),
10121012
(:GitConfig, :(Union{GitRepo, Nothing}), :AbstractGitObject, :git_config),
10131013
(:GitIndex, :(Union{GitRepo, Nothing}), :AbstractGitObject, :git_index),

0 commit comments

Comments
 (0)