Skip to content

Commit b5dd8a1

Browse files
authored
Merge pull request #38906 from JuliaLang/teh/precompiles_revise
Reduce latency of Revise.revise()
2 parents 3853060 + cc1623b commit b5dd8a1

File tree

2 files changed

+82
-13
lines changed

2 files changed

+82
-13
lines changed

base/Base.jl

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -391,6 +391,42 @@ end_base_include = time_ns()
391391
const _sysimage_modules = PkgId[]
392392
in_sysimage(pkgid::PkgId) = pkgid in _sysimage_modules
393393

394+
# Precompiles for Revise
395+
# TODO: move these to contrib/generate_precompile.jl
396+
# The problem is they don't work there
397+
let m = which(+, (Int, Int))
398+
while true # defeat interpreter heuristic to force compilation
399+
delete!(push!(Set{Method}(), m), m)
400+
copy(Core.Compiler.retrieve_code_info(Core.Compiler.specialize_method(m, [Int, Int], Core.svec())))
401+
402+
empty!(Set())
403+
push!(push!(Set{Union{GlobalRef,Symbol}}(), :two), GlobalRef(Base, :two))
404+
(setindex!(Dict{String,Base.PkgId}(), Base.PkgId(Base), "file.jl"))["file.jl"]
405+
(setindex!(Dict{Symbol,Vector{Int}}(), [1], :two))[:two]
406+
(setindex!(Dict{Base.PkgId,String}(), "file.jl", Base.PkgId(Base)))[Base.PkgId(Base)]
407+
(setindex!(Dict{Union{GlobalRef,Symbol}, Vector{Int}}(), [1], :two))[:two]
408+
(setindex!(IdDict{Type, Union{Missing, Vector{Tuple{LineNumberNode, Expr}}}}(), missing, Int))[Int]
409+
Dict{Symbol, Union{Nothing, Bool, Symbol}}(:one => false)[:one]
410+
Dict(Base => [:(1+1)])[Base]
411+
Dict(:one => [1])[:one]
412+
Dict("abc" => Set())["abc"]
413+
pushfirst!([], sum)
414+
get(Base.pkgorigins, Base.PkgId(Base), nothing)
415+
sort!([1,2,3])
416+
unique!([1,2,3])
417+
cumsum([1,2,3])
418+
append!(Int[], BitSet())
419+
isempty(BitSet())
420+
delete!(BitSet([1,2]), 3)
421+
deleteat!(Int32[1,2,3], [1,3])
422+
deleteat!(Any[1,2,3], [1,3])
423+
Core.svec(1, 2) == Core.svec(3, 4)
424+
any(t->t[1].line > 1, [(LineNumberNode(2,:none), :(1+1))])
425+
426+
break # end defeat interpreter heuristic
427+
end
428+
end
429+
394430
if is_primary_base_module
395431
function __init__()
396432
# try to ensuremake sure OpenBLAS does not set CPU affinity (#1070, #9639)

contrib/generate_precompile.jl

Lines changed: 46 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -16,12 +16,11 @@ UP_ARROW = "\e[A"
1616
DOWN_ARROW = "\e[B"
1717

1818
hardcoded_precompile_statements = """
19-
# used by JuliaInterpreter.jl and Revise.jl
19+
# used by Revise.jl
2020
@assert precompile(Tuple{typeof(Base.parse_cache_header), String})
21-
@assert precompile(Tuple{typeof(pushfirst!), Vector{Any}, Function})
22-
@assert precompile(Tuple{typeof(push!), Set{Module}, Module})
23-
@assert precompile(Tuple{typeof(push!), Set{Method}, Method})
24-
@assert precompile(Tuple{typeof(empty!), Set{Any}})
21+
@assert precompile(Base.read_dependency_src, (String, String))
22+
@assert precompile(Base.CoreLogging.current_logger_for_env, (Base.CoreLogging.LogLevel, String, Module))
23+
2524
# used by Requires.jl
2625
@assert precompile(Tuple{typeof(get!), Type{Vector{Function}}, Dict{Base.PkgId,Vector{Function}}, Base.PkgId})
2726
@assert precompile(Tuple{typeof(haskey), Dict{Base.PkgId,Vector{Function}}, Base.PkgId})
@@ -57,10 +56,38 @@ cd("complet_path\t\t$CTRL_C
5756
"""
5857

5958
precompile_script = """
60-
# Used by Revise
61-
(setindex!(Dict{String,Base.PkgId}(), Base.PkgId(Base), "file.jl"))["file.jl"]
62-
(setindex!(Dict{Base.PkgId,String}(), "file.jl", Base.PkgId(Base)))[Base.PkgId(Base)]
63-
get(Base.pkgorigins, Base.PkgId(Base), nothing)
59+
# NOTE: these were moved to the end of Base.jl. TODO: move back here.
60+
# # Used by Revise & its dependencies
61+
# while true # force inference
62+
# delete!(push!(Set{Module}(), Base), Main)
63+
# m = first(methods(+))
64+
# delete!(push!(Set{Method}(), m), m)
65+
# empty!(Set())
66+
# push!(push!(Set{Union{GlobalRef,Symbol}}(), :two), GlobalRef(Base, :two))
67+
# (setindex!(Dict{String,Base.PkgId}(), Base.PkgId(Base), "file.jl"))["file.jl"]
68+
# (setindex!(Dict{Symbol,Vector{Int}}(), [1], :two))[:two]
69+
# (setindex!(Dict{Base.PkgId,String}(), "file.jl", Base.PkgId(Base)))[Base.PkgId(Base)]
70+
# (setindex!(Dict{Union{GlobalRef,Symbol}, Vector{Int}}(), [1], :two))[:two]
71+
# (setindex!(IdDict{Type, Union{Missing, Vector{Tuple{LineNumberNode, Expr}}}}(), missing, Int))[Int]
72+
# Dict{Symbol, Union{Nothing, Bool, Symbol}}(:one => false)[:one]
73+
# Dict(Base => [:(1+1)])[Base]
74+
# Dict(:one => [1])[:one]
75+
# Dict("abc" => Set())["abc"]
76+
# pushfirst!([], sum)
77+
# get(Base.pkgorigins, Base.PkgId(Base), nothing)
78+
# sort!([1,2,3])
79+
# unique!([1,2,3])
80+
# cumsum([1,2,3])
81+
# append!(Int[], BitSet())
82+
# isempty(BitSet())
83+
# delete!(BitSet([1,2]), 3)
84+
# deleteat!(Int32[1,2,3], [1,3])
85+
# deleteat!(Any[1,2,3], [1,3])
86+
# Core.svec(1, 2) == Core.svec(3, 4)
87+
# # copy(Core.Compiler.retrieve_code_info(Core.Compiler.specialize_method(which(+, (Int, Int)), [Int, Int], Core.svec())))
88+
# any(t->t[1].line > 1, [(LineNumberNode(2,:none),:(1+1))])
89+
# break # end force inference
90+
# end
6491
"""
6592

6693
julia_exepath() = joinpath(Sys.BINDIR::String, Base.julia_exename())
@@ -73,21 +100,26 @@ if have_repl
73100
"""
74101
end
75102

76-
# This is disabled because it doesn't give much benefit
77-
# and the code in Distributed is poorly typed causing many invalidations
78-
#=
79103
Distributed = get(Base.loaded_modules,
80104
Base.PkgId(Base.UUID("8ba89e20-285c-5b6f-9357-94700520ee1b"), "Distributed"),
81105
nothing)
82106
if Distributed !== nothing
107+
hardcoded_precompile_statements *= """
108+
@assert precompile(Tuple{typeof(Distributed.remotecall),Function,Int,Module,Vararg{Any, 100}})
109+
@assert precompile(Tuple{typeof(Distributed.procs)})
110+
@assert precompile(Tuple{typeof(Distributed.finalize_ref), Distributed.Future})
111+
"""
112+
# This is disabled because it doesn't give much benefit
113+
# and the code in Distributed is poorly typed causing many invalidations
114+
#=
83115
precompile_script *= """
84116
using Distributed
85117
addprocs(2)
86118
pmap(x->iseven(x) ? 1 : 0, 1:4)
87119
@distributed (+) for i = 1:100 Int(rand(Bool)) end
88120
"""
89-
end
90121
=#
122+
end
91123

92124

93125
Artifacts = get(Base.loaded_modules,
@@ -122,6 +154,7 @@ if FileWatching !== nothing
122154
hardcoded_precompile_statements *= """
123155
@assert precompile(Tuple{typeof(FileWatching.watch_file), String, Float64})
124156
@assert precompile(Tuple{typeof(FileWatching.watch_file), String, Int})
157+
@assert precompile(Tuple{typeof(FileWatching._uv_hook_close), FileWatching.FileMonitor})
125158
"""
126159
end
127160

0 commit comments

Comments
 (0)