Skip to content

Commit bea9c9c

Browse files
KristofferCKristofferCIanButterworth
committed
cache the find_all_in_cache_path call during parallel precompilation (#56369)
Before (in an environment with DifferentialEquations.jl): ```julia julia> @time Pkg.precompile() 0.733576 seconds (3.44 M allocations: 283.676 MiB, 6.24% gc time) julia> isfile_calls[1:10] 10-element Vector{Pair{String, Int64}}: "/home/kc/.julia/juliaup/julia-nightly/share/julia/compiled/v1.12/Printf/3FQLY_zHycD.ji" => 178 "/home/kc/.julia/juliaup/julia-nightly/share/julia/compiled/v1.12/Printf/3FQLY_xxrt3.ji" => 178 "/home/kc/.julia/juliaup/julia-nightly/share/julia/compiled/v1.12/Dates/p8See_xxrt3.ji" => 158 "/home/kc/.julia/juliaup/julia-nightly/share/julia/compiled/v1.12/Dates/p8See_zHycD.ji" => 158 "/home/kc/.julia/juliaup/julia-nightly/share/julia/compiled/v1.12/TOML/mjrwE_zHycD.ji" => 155 "/home/kc/.julia/juliaup/julia-nightly/share/julia/compiled/v1.12/TOML/mjrwE_xxrt3.ji" => 155 "/home/kc/.julia/compiled/v1.12/Preferences/pWSk8_4Qv86.ji" => 152 "/home/kc/.julia/compiled/v1.12/Preferences/pWSk8_juhqb.ji" => 152 "/home/kc/.julia/juliaup/julia-nightly/share/julia/compiled/v1.12/StyledStrings/UcVoM_zHycD.ji" => 144 "/home/kc/.julia/juliaup/julia-nightly/share/julia/compiled/v1.12/StyledStrings/UcVoM_xxrt3.ji" => 144 ``` After: ```julia julia> @time Pkg.precompile() 0.460077 seconds (877.59 k allocations: 108.075 MiB, 4.77% gc time) julia> isfile_calls[1:10] 10-element Vector{Pair{String, Int64}}: "/tmp/jl_a5xFWK/Project.toml" => 15 "/tmp/jl_a5xFWK/Manifest.toml" => 7 "/home/kc/.julia/registries/General.toml" => 6 "/home/kc/.julia/juliaup/julia-nightly/share/julia/stdlib/v1.12/Markdown/src/Markdown.jl" => 3 "/home/kc/.julia/juliaup/julia-nightly/share/julia/stdlib/v1.12/Serialization/src/Serialization.jl" => 3 "/home/kc/.julia/juliaup/julia-nightly/share/julia/stdlib/v1.12/Distributed/src/Distributed.jl" => 3 "/home/kc/.julia/juliaup/julia-nightly/share/julia/stdlib/v1.12/UUIDs/src/UUIDs.jl" => 3 "/home/kc/.julia/juliaup/julia-nightly/share/julia/stdlib/v1.12/LibCURL/src/LibCURL.jl" => 3 ``` Performance is improved and we are not calling `isfile` on a bunch of the same ji files hundreds times. Benchmark is made on a linux machine so performance diff should be a lot better on Windows where these `isfile_casesensitive` call is much more expensive. Fixes #56366 --------- Co-authored-by: KristofferC <[email protected]> Co-authored-by: Ian Butterworth <[email protected]> (cherry picked from commit 9850a38)
1 parent b3c3460 commit bea9c9c

File tree

2 files changed

+10
-6
lines changed

2 files changed

+10
-6
lines changed

base/loading.jl

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1770,7 +1770,8 @@ const StaleCacheKey = Tuple{Base.PkgId, UInt128, String, String}
17701770
function compilecache_path(pkg::PkgId;
17711771
ignore_loaded::Bool=false,
17721772
stale_cache::Dict{StaleCacheKey,Bool}=Dict{StaleCacheKey, Bool}(),
1773-
cachepaths::Vector{String}=Base.find_all_in_cache_path(pkg),
1773+
cachepath_cache::Dict{PkgId, Vector{String}}=Dict{PkgId, Vector{String}}(),
1774+
cachepaths::Vector{String}=get!(() -> find_all_in_cache_path(pkg), cachepath_cache, pkg),
17741775
sourcepath::Union{String,Nothing}=Base.locate_package(pkg),
17751776
flags::CacheFlags=CacheFlags())
17761777
path = nothing
@@ -1785,7 +1786,7 @@ function compilecache_path(pkg::PkgId;
17851786
for dep in staledeps
17861787
dep isa Module && continue
17871788
modpath, modkey, modbuild_id = dep::Tuple{String, PkgId, UInt128}
1788-
modpaths = find_all_in_cache_path(modkey)
1789+
modpaths = get!(() -> find_all_in_cache_path(modkey), cachepath_cache, modkey)
17891790
for modpath_to_try in modpaths::Vector{String}
17901791
stale_cache_key = (modkey, modbuild_id, modpath, modpath_to_try)::StaleCacheKey
17911792
if get!(() -> stale_cachefile(stale_cache_key...; ignore_loaded, requested_flags=flags) === true,
@@ -1827,10 +1828,11 @@ fresh julia session specify `ignore_loaded=true`.
18271828
function isprecompiled(pkg::PkgId;
18281829
ignore_loaded::Bool=false,
18291830
stale_cache::Dict{StaleCacheKey,Bool}=Dict{StaleCacheKey, Bool}(),
1830-
cachepaths::Vector{String}=Base.find_all_in_cache_path(pkg),
1831+
cachepath_cache::Dict{PkgId, Vector{String}}=Dict{PkgId, Vector{String}}(),
1832+
cachepaths::Vector{String}=get!(() -> find_all_in_cache_path(pkg), cachepath_cache, pkg),
18311833
sourcepath::Union{String,Nothing}=Base.locate_package(pkg),
18321834
flags::CacheFlags=CacheFlags())
1833-
path = compilecache_path(pkg; ignore_loaded, stale_cache, cachepaths, sourcepath, flags)
1835+
path = compilecache_path(pkg; ignore_loaded, stale_cache, cachepath_cache, cachepaths, sourcepath, flags)
18341836
return !isnothing(path)
18351837
end
18361838

base/precompilation.jl

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -400,6 +400,8 @@ function _precompilepkgs(pkgs::Vector{String},
400400
color_string(cstr::String, col::Union{Int64, Symbol}) = _color_string(cstr, col, hascolor)
401401

402402
stale_cache = Dict{StaleCacheKey, Bool}()
403+
cachepath_cache = Dict{PkgId, Vector{String}}()
404+
403405
exts = Dict{PkgId, String}() # ext -> parent
404406
# make a flat map of each dep and its direct deps
405407
depsmap = Dict{PkgId, Vector{PkgId}}()
@@ -773,7 +775,7 @@ function _precompilepkgs(pkgs::Vector{String},
773775
## precompilation loop
774776

775777
for (pkg, deps) in depsmap
776-
cachepaths = Base.find_all_in_cache_path(pkg)
778+
cachepaths = get!(() -> Base.find_all_in_cache_path(pkg), cachepath_cache, pkg)
777779
sourcepath = Base.locate_package(pkg)
778780
single_requested_pkg = length(pkgs) == 1 && only(pkgs) == pkg.name
779781
for config in configs
@@ -796,7 +798,7 @@ function _precompilepkgs(pkgs::Vector{String},
796798
wait(was_processed[(dep,config)])
797799
end
798800
circular = pkg in circular_deps
799-
is_stale = !Base.isprecompiled(pkg; ignore_loaded=true, stale_cache, cachepaths, sourcepath, flags=cacheflags)
801+
is_stale = !Base.isprecompiled(pkg; ignore_loaded=true, stale_cache, cachepath_cache, cachepaths, sourcepath, flags=cacheflags)
800802
if !circular && is_stale
801803
Base.acquire(parallel_limiter)
802804
is_direct_dep = pkg in direct_deps

0 commit comments

Comments
 (0)