Skip to content

Commit

Permalink
Fix ~700 PyPlot invalidations (JuliaLang#39420)
Browse files Browse the repository at this point in the history
PyCall specializes a lot of low-level methods like
`convert(::Type{Symbol}, arg)`, and this triggers a lot of invalidations.
The worst are for keyword-argument functions, but there are quite
a few others. This drops the number from ~1100 to ~400.
It's quite likely that additional improvements could be made, but this
is major progress.

Related: JuliaLang#39419.
  • Loading branch information
timholy committed Jan 29, 2021
1 parent b9f8b8b commit 3ee63a6
Show file tree
Hide file tree
Showing 7 changed files with 26 additions and 25 deletions.
2 changes: 1 addition & 1 deletion base/binaryplatforms.jl
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ struct Platform <: AbstractPlatform
if isa(value, VersionNumber)
value = string(value)
elseif isa(value, AbstractString)
v = tryparse(VersionNumber, value)
v = tryparse(VersionNumber, String(value))
if isa(v, VersionNumber)
value = string(v)
end
Expand Down
15 changes: 8 additions & 7 deletions base/docs/Docs.jl
Original file line number Diff line number Diff line change
Expand Up @@ -291,14 +291,15 @@ end

uncurly(@nospecialize ex) = isexpr(ex, :curly) ? ex.args[1] : ex

namify(@nospecialize x) = astname(x, isexpr(x, :macro))
namify(@nospecialize x) = astname(x, isexpr(x, :macro))::Union{Symbol,Expr,GlobalRef}

function astname(x::Expr, ismacro::Bool)
if isexpr(x, :.)
head = x.head
if head === :.
ismacro ? macroname(x) : x
# Call overloading, e.g. `(a::A)(b) = b` or `function (a::A)(b) b end` should document `A(b)`
elseif (isexpr(x, :function) || isexpr(x, :(=))) && isexpr(x.args[1], :call) && isexpr(x.args[1].args[1], :(::))
return astname(x.args[1].args[1].args[end], ismacro)
elseif (head === :function || head === :(=)) && isexpr(x.args[1], :call) && isexpr((x.args[1]::Expr).args[1], :(::))
return astname(((x.args[1]::Expr).args[1]::Expr).args[end], ismacro)
else
n = isexpr(x, (:module, :struct)) ? 2 : 1
astname(x.args[n], ismacro)
Expand Down Expand Up @@ -347,19 +348,19 @@ function metadata(__source__, __module__, expr, ismodule)
P = Pair{Symbol,Any}
fields = P[]
last_docstr = nothing
for each in expr.args[3].args
for each in (expr.args[3]::Expr).args
if isa(each, Symbol) || isexpr(each, :(::))
# a field declaration
if last_docstr !== nothing
push!(fields, P(namify(each), last_docstr))
push!(fields, P(namify(each::Union{Symbol,Expr}), last_docstr))
last_docstr = nothing
end
elseif isexpr(each, :function) || isexpr(each, :(=))
break
elseif isa(each, String) || isexpr(each, :string) || isexpr(each, :call) ||
(isexpr(each, :macrocall) && each.args[1] === Symbol("@doc_str"))
# forms that might be doc strings
last_docstr = each
last_docstr = each::Union{String,Expr}
end
end
dict = :($(Dict{Symbol,Any})($([(:($(P)($(quot(f)), $d)))::Expr for (f, d) in fields]...)))
Expand Down
7 changes: 2 additions & 5 deletions base/errorshow.jl
Original file line number Diff line number Diff line change
Expand Up @@ -281,14 +281,11 @@ function showerror(io::IO, ex::MethodError)
if any(x -> x <: AbstractArray{<:Number}, arg_types_param) &&
any(x -> x <: Number, arg_types_param)

nouns = Dict{Any,String}(
Base.:+ => "addition",
Base.:- => "subtraction",
)
nounf = f === Base.:+ ? "addition" : "subtraction"
varnames = ("scalar", "array")
first, second = arg_types_param[1] <: Number ? varnames : reverse(varnames)
fstring = f === Base.:+ ? "+" : "-" # avoid depending on show_default for functions (invalidation)
print(io, "\nFor element-wise $(nouns[f]), use broadcasting with dot syntax: $first .$fstring $second")
print(io, "\nFor element-wise $nounf, use broadcasting with dot syntax: $first .$fstring $second")
end
end
if ft <: AbstractArray
Expand Down
6 changes: 4 additions & 2 deletions base/file.jl
Original file line number Diff line number Diff line change
Expand Up @@ -318,8 +318,8 @@ function checkfor_mv_cp_cptree(src::AbstractString, dst::AbstractString, txt::Ab
end
end

function cptree(src::AbstractString, dst::AbstractString; force::Bool=false,
follow_symlinks::Bool=false)
function cptree(src::String, dst::String; force::Bool=false,
follow_symlinks::Bool=false)
isdir(src) || throw(ArgumentError("'$src' is not a directory. Use `cp(src, dst)`"))
checkfor_mv_cp_cptree(src, dst, "copying"; force=force)
mkdir(dst)
Expand All @@ -335,6 +335,8 @@ function cptree(src::AbstractString, dst::AbstractString; force::Bool=false,
end
end
end
cptree(src::AbstractString, dst::AbstractString; kwargs...) =
cptree(String(src)::String, String(dst)::String; kwargs...)

"""
cp(src::AbstractString, dst::AbstractString; force::Bool=false, follow_symlinks::Bool=false)
Expand Down
3 changes: 2 additions & 1 deletion base/namedtuple.jl
Original file line number Diff line number Diff line change
Expand Up @@ -286,7 +286,8 @@ function merge(a::NamedTuple, itr)
names = Symbol[]
vals = Any[]
inds = IdDict{Symbol,Int}()
for (k::Symbol, v) in itr
for (k, v) in itr
k = k::Symbol
oldind = get(inds, k, 0)
if oldind > 0
vals[oldind] = v
Expand Down
16 changes: 8 additions & 8 deletions base/stacktraces.jl
Original file line number Diff line number Diff line change
Expand Up @@ -111,7 +111,7 @@ function lookup(pointer::Ptr{Cvoid})
for i in 1:length(infos)
info = infos[i]::Core.SimpleVector
@assert(length(info) == 6)
res[i] = StackFrame(info[1], info[2], info[3], info[4], info[5], info[6], pointer)
res[i] = StackFrame(info[1]::Symbol, info[2]::Symbol, info[3]::Int, info[4], info[5]::Bool, info[6]::Bool, pointer)
end
return res
end
Expand All @@ -126,10 +126,10 @@ function lookup(ip::Union{Base.InterpreterIP,Core.Compiler.InterpreterIP})
end
codeinfo = (code isa MethodInstance ? code.uninferred : code)::CodeInfo
# prepare approximate code info
if code isa MethodInstance && code.def isa Method
func = code.def.name
file = code.def.file
line = code.def.line
if code isa MethodInstance && (meth = code.def; meth isa Method)
func = meth.name
file = meth.file
line = meth.line
else
func = top_level_scope_sym
file = empty_sym
Expand All @@ -139,13 +139,13 @@ function lookup(ip::Union{Base.InterpreterIP,Core.Compiler.InterpreterIP})
if i > length(codeinfo.codelocs) || codeinfo.codelocs[i] == 0
return [StackFrame(func, file, line, code, false, false, 0)]
end
lineinfo = codeinfo.linetable[codeinfo.codelocs[i]]
lineinfo = codeinfo.linetable[codeinfo.codelocs[i]]::Core.LineInfoNode
scopes = StackFrame[]
while true
inlined = lineinfo.inlined_at != 0
push!(scopes, StackFrame(lineinfo.method, lineinfo.file, lineinfo.line, inlined ? nothing : code, false, inlined, 0))
push!(scopes, StackFrame(Base.IRShow.method_name(lineinfo)::Symbol, lineinfo.file, lineinfo.line, inlined ? nothing : code, false, inlined, 0))
inlined || break
lineinfo = codeinfo.linetable[lineinfo.inlined_at]
lineinfo = codeinfo.linetable[lineinfo.inlined_at]::Core.LineInfoNode
end
return scopes
end
Expand Down
2 changes: 1 addition & 1 deletion stdlib/Markdown/src/parse/parse.jl
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

mutable struct MD
content::Vector{Any}
meta::Dict{Any, Any}
meta::Dict{Symbol, Any}

MD(content::AbstractVector, meta::Dict = Dict()) =
new(content, meta)
Expand Down

0 comments on commit 3ee63a6

Please sign in to comment.