Skip to content

Commit e3043a8

Browse files
authored
delay loading of extensions as much as possible (#48674)
1 parent c37fc27 commit e3043a8

File tree

1 file changed

+47
-40
lines changed

1 file changed

+47
-40
lines changed

base/loading.jl

Lines changed: 47 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -1080,7 +1080,6 @@ function register_restored_modules(sv::SimpleVector, pkg::PkgId, path::String)
10801080
end
10811081

10821082
function run_package_callbacks(modkey::PkgId)
1083-
run_extension_callbacks(modkey)
10841083
assert_havelock(require_lock)
10851084
unlock(require_lock)
10861085
try
@@ -1204,53 +1203,57 @@ function run_extension_callbacks(extid::ExtensionId)
12041203
return succeeded
12051204
end
12061205

1207-
function run_extension_callbacks(pkgid::PkgId)
1206+
function run_extension_callbacks()
12081207
assert_havelock(require_lock)
1209-
# take ownership of extids that depend on this pkgid
1210-
extids = pop!(EXT_DORMITORY, pkgid, nothing)
1211-
extids === nothing && return
1212-
for extid in extids
1213-
if extid.ntriggers > 0
1214-
# It is possible that pkgid was loaded in an environment
1215-
# below the one of the parent. This will cause a load failure when the
1216-
# pkg ext tries to load the triggers. Therefore, check this first
1217-
# before loading the pkg ext.
1218-
pkgenv = Base.identify_package_env(extid.id, pkgid.name)
1219-
ext_not_allowed_load = false
1220-
if pkgenv === nothing
1221-
ext_not_allowed_load = true
1222-
else
1223-
pkg, env = pkgenv
1224-
path = Base.locate_package(pkg, env)
1225-
if path === nothing
1208+
loaded_triggers = collect(intersect(keys(Base.loaded_modules), keys(Base.EXT_DORMITORY)))
1209+
sort!(loaded_triggers; by=x->x.uuid)
1210+
for pkgid in loaded_triggers
1211+
# take ownership of extids that depend on this pkgid
1212+
extids = pop!(EXT_DORMITORY, pkgid, nothing)
1213+
extids === nothing && continue
1214+
for extid in extids
1215+
if extid.ntriggers > 0
1216+
# It is possible that pkgid was loaded in an environment
1217+
# below the one of the parent. This will cause a load failure when the
1218+
# pkg ext tries to load the triggers. Therefore, check this first
1219+
# before loading the pkg ext.
1220+
pkgenv = Base.identify_package_env(extid.id, pkgid.name)
1221+
ext_not_allowed_load = false
1222+
if pkgenv === nothing
12261223
ext_not_allowed_load = true
1224+
else
1225+
pkg, env = pkgenv
1226+
path = Base.locate_package(pkg, env)
1227+
if path === nothing
1228+
ext_not_allowed_load = true
1229+
end
1230+
end
1231+
if ext_not_allowed_load
1232+
@debug "Extension $(extid.id.name) of $(extid.parentid.name) will not be loaded \
1233+
since $(pkgid.name) loaded in environment lower in load path"
1234+
# indicate extid is expected to fail
1235+
extid.ntriggers *= -1
1236+
else
1237+
# indicate pkgid is loaded
1238+
extid.ntriggers -= 1
12271239
end
12281240
end
1229-
if ext_not_allowed_load
1230-
@debug "Extension $(extid.id.name) of $(extid.parentid.name) will not be loaded \
1231-
since $(pkgid.name) loaded in environment lower in load path"
1232-
# indicate extid is expected to fail
1233-
extid.ntriggers *= -1
1234-
else
1241+
if extid.ntriggers < 0
12351242
# indicate pkgid is loaded
1236-
extid.ntriggers -= 1
1243+
extid.ntriggers += 1
1244+
succeeded = false
1245+
else
1246+
succeeded = true
1247+
end
1248+
if extid.ntriggers == 0
1249+
# actually load extid, now that all dependencies are met,
1250+
# and record the result
1251+
succeeded = succeeded && run_extension_callbacks(extid)
1252+
succeeded || push!(EXT_DORMITORY_FAILED, extid)
12371253
end
1238-
end
1239-
if extid.ntriggers < 0
1240-
# indicate pkgid is loaded
1241-
extid.ntriggers += 1
1242-
succeeded = false
1243-
else
1244-
succeeded = true
1245-
end
1246-
if extid.ntriggers == 0
1247-
# actually load extid, now that all dependencies are met,
1248-
# and record the result
1249-
succeeded = succeeded && run_extension_callbacks(extid)
1250-
succeeded || push!(EXT_DORMITORY_FAILED, extid)
12511254
end
12521255
end
1253-
nothing
1256+
return
12541257
end
12551258

12561259
"""
@@ -1665,6 +1668,10 @@ function _require_prelocked(uuidkey::PkgId, env=nothing)
16651668
else
16661669
newm = root_module(uuidkey)
16671670
end
1671+
# Load extensions when not precompiling and not in a nested package load
1672+
if JLOptions().incremental == 0 && isempty(package_locks)
1673+
run_extension_callbacks()
1674+
end
16681675
return newm
16691676
end
16701677

0 commit comments

Comments
 (0)