Skip to content

Commit ccd969b

Browse files
committed
Prevent extensions from blocking parallel pre-compilation
Previously our precompilation code was causing any dependencies of a package A to wait on all of A's weakdeps to finish pre-compiling, even if it can't actually load those weakdeps (or the extension itself) This would lead to a pre-compile ordering like: ``` A B \ / \ Ext AB \ | / C / \ / D ``` Here, extension `C` cannot pre-compile in parallel with `Ext {A,B}` and `B`, because it has to wait for `Ext {A,B}` to finish pre-compiling. That happens even though `C` has no way to load either of these. This change updates the pre-compile ordering to be more parallel, reflecting the true place where `Ext {A,B}` can be loaded: ``` A B / \ / \ C Ext AB | \ | / \-- D --/ ``` which allows `C` to compile in parallel with `B` and `Ext{A,B}`
1 parent 8a2abe1 commit ccd969b

File tree

1 file changed

+41
-7
lines changed

1 file changed

+41
-7
lines changed

base/precompilation.jl

Lines changed: 41 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -490,14 +490,48 @@ function _precompilepkgs(pkgs::Vector{String},
490490
end
491491
end
492492

493+
# A package depends on an extension if it (indirectly) depends on all extension triggers
494+
function expand_indirect_dependencies(direct_deps)
495+
function visit!(visited, node, all_deps)
496+
if node in visited
497+
return
498+
end
499+
push!(visited, node)
500+
for dep in get(Set{Base.PkgId}, direct_deps, node)
501+
if !(dep in all_deps)
502+
push!(all_deps, dep)
503+
visit!(visited, dep, all_deps)
504+
end
505+
end
506+
end
507+
508+
indirect_deps = Dict{Base.PkgId, Set{Base.PkgId}}()
509+
for package in keys(direct_deps)
510+
# Initialize a set to keep track of all dependencies for 'package'
511+
all_deps = Set{Base.PkgId}()
512+
visited = Set{Base.PkgId}()
513+
visit!(visited, package, all_deps)
514+
# Update direct_deps with the complete set of dependencies for 'package'
515+
indirect_deps[package] = all_deps
516+
end
517+
return indirect_deps
518+
end
519+
493520
# this loop must be run after the full direct_deps map has been populated
494-
for (pkg, pkg_exts) in parent_to_exts
495-
# find any packages that depend on the extension(s)'s deps and replace those deps in their deps list with the extension(s),
496-
# basically injecting the extension into the precompile order in the graph, to avoid race to precompile extensions
497-
for (_pkg, deps) in direct_deps # for each manifest dep
498-
if !in(_pkg, keys(ext_to_parent)) && pkg in deps # if not an extension and depends on pkg
499-
append!(deps, pkg_exts) # add the package extensions to deps
500-
filter!(!isequal(pkg), deps) # remove the pkg from deps
521+
indirect_deps = expand_indirect_dependencies(direct_deps)
522+
for ext in keys(ext_to_parent)
523+
ext_loadable_in_pkg = Dict{Base.PkgId,Bool}()
524+
for pkg in keys(direct_deps)
525+
is_trigger = in(pkg, direct_deps[ext])
526+
is_extension = in(pkg, keys(ext_to_parent))
527+
has_triggers = issubset(direct_deps[ext], indirect_deps[pkg])
528+
ext_loadable_in_pkg[pkg] = !is_extension && has_triggers && !is_trigger
529+
end
530+
for (pkg, ext_loadable) in ext_loadable_in_pkg
531+
if ext_loadable && !any((dep)->ext_loadable_in_pkg[dep], direct_deps[pkg])
532+
# add an edge if the extension is loadable by pkg, and was not loadable in any
533+
# of the pkg's dependencies
534+
push!(direct_deps[pkg], ext)
501535
end
502536
end
503537
end

0 commit comments

Comments
 (0)