Skip to content

Commit

Permalink
Precompile watch_file and a few other functions used by Revise (Jul…
Browse files Browse the repository at this point in the history
…iaLang#37403)

Together with some internal changes coming in Revise v3.0, on my
machine this drops the overhead of `using Revise` from 3.5s to 0.5s.
This change only contributes about 0.1s of that time, but out of
0.5s it's a noticeable improvement. The only one of these that
makes a sizable impact is `watch_file`, but this does ensure that
Revise doesn't trigger any additional "generic" compilation.
  • Loading branch information
timholy committed Sep 8, 2020
1 parent c0b6afb commit a36c825
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 7 deletions.
29 changes: 23 additions & 6 deletions contrib/generate_precompile.jl
Original file line number Diff line number Diff line change
Expand Up @@ -15,12 +15,14 @@ UP_ARROW = "\e[A"
DOWN_ARROW = "\e[B"

hardcoded_precompile_statements = """
precompile(Tuple{typeof(Base.stale_cachefile), String, String})
precompile(Tuple{typeof(push!), Set{Module}, Module})
precompile(Tuple{typeof(push!), Set{Method}, Method})
precompile(Tuple{typeof(push!), Set{Base.PkgId}, Base.PkgId})
precompile(Tuple{typeof(setindex!), Dict{String,Base.PkgId}, Base.PkgId, String})
precompile(Tuple{typeof(get!), Type{Vector{Function}}, Dict{Base.PkgId,Vector{Function}}, Base.PkgId})
@assert precompile(Tuple{typeof(Base.stale_cachefile), String, String})
@assert precompile(Tuple{typeof(push!), Set{Module}, Module})
@assert precompile(Tuple{typeof(push!), Set{Method}, Method})
@assert precompile(Tuple{typeof(push!), Set{Base.PkgId}, Base.PkgId})
@assert precompile(Tuple{typeof(setindex!), Dict{String,Base.PkgId}, Base.PkgId, String})
@assert precompile(Tuple{typeof(get!), Type{Vector{Function}}, Dict{Base.PkgId,Vector{Function}}, Base.PkgId})
@assert precompile(Tuple{typeof(isassigned), Core.SimpleVector, Int})
@assert precompile(Tuple{typeof(pushfirst!), Vector{Any}, Function})
"""

precompile_script = """
Expand All @@ -45,6 +47,11 @@ julia_exepath() = joinpath(Sys.BINDIR, Base.julia_exename())

have_repl = haskey(Base.loaded_modules,
Base.PkgId(Base.UUID("3fa0cd96-eef1-5676-8a61-b3b8758bbffb"), "REPL"))
if have_repl
hardcoded_precompile_statements *= """
@assert precompile(Tuple{typeof(getproperty), REPL.REPLBackend, Symbol})
"""
end

Distributed = get(Base.loaded_modules,
Base.PkgId(Base.UUID("8ba89e20-285c-5b6f-9357-94700520ee1b"), "Distributed"),
Expand All @@ -66,6 +73,16 @@ if Pkg !== nothing
precompile_script *= Pkg.precompile_script
end

FileWatching = get(Base.loaded_modules,
Base.PkgId(Base.UUID("7b1f6079-737a-58dc-b8bc-7a2ca5c1b5ee"), "FileWatching"),
nothing)
if FileWatching !== nothing
hardcoded_precompile_statements *= """
@assert precompile(Tuple{typeof(FileWatching.watch_file), String, Float64})
@assert precompile(Tuple{typeof(FileWatching.watch_file), String, Int})
"""
end

function generate_precompile_statements()
start_time = time_ns()
debug_output = devnull # or stdout
Expand Down
3 changes: 2 additions & 1 deletion stdlib/FileWatching/src/FileWatching.jl
Original file line number Diff line number Diff line change
Expand Up @@ -688,7 +688,7 @@ giving the result of watching the file.
This behavior of this function varies slightly across platforms. See
<https://nodejs.org/api/fs.html#fs_caveats> for more detailed information.
"""
function watch_file(s::AbstractString, timeout_s::Real=-1)
function watch_file(s::String, timeout_s::Float64=-1.0)
fm = FileMonitor(s)
local timer
try
Expand All @@ -703,6 +703,7 @@ function watch_file(s::AbstractString, timeout_s::Real=-1)
@isdefined(timer) && close(timer)
end
end
watch_file(s::AbstractString, timeout_s::Real=-1) = watch_file(String(s), Float64(timeout_s))

"""
watch_folder(path::AbstractString, timeout_s::Real=-1)
Expand Down

0 comments on commit a36c825

Please sign in to comment.