Skip to content

Commit 915075f

Browse files
nsajkoKristofferC
authored andcommitted
work around some closure capture boxing inference issues in loading (#59367)
The only method of the `__require_prelocked` function in `Base` gets compiled with bad inference for two variables, `path` and `reasons`, which get pessimistically boxed (and inferred as `Any`), even though both of them are only assigned to once across both the method and the closure within (neither is assigned to within the closure). My guess for what causes this would be either of these, or the combination: * (mis)use of GOTO * `try`-`catch` The least demanding way to work around the issue is adding a `let` around the closure, as advised in the Performance tips. That is what this change does. This change should fix several bad inference results within method instances such as `__require_prelocked(::PkgId, ::Nothing)` and `__require_prelocked(::PkgId, ::String)`. Consequently, the sysimage should become less vulnerable to invalidation happening when loading certain packages. (cherry picked from commit 547f858)
1 parent 2ad6627 commit 915075f

File tree

1 file changed

+14
-12
lines changed

1 file changed

+14
-12
lines changed

base/loading.jl

Lines changed: 14 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -2634,21 +2634,23 @@ function __require_prelocked(pkg::PkgId, env)
26342634
@goto load_from_cache
26352635
end
26362636
# spawn off a new incremental pre-compile task for recursive `require` calls
2637-
loaded = maybe_cachefile_lock(pkg, path) do
2638-
# double-check the search now that we have lock
2639-
m = _require_search_from_serialized(pkg, path, UInt128(0), true)
2640-
m isa Module && return m
2641-
triggers = get(EXT_PRIMED, pkg, nothing)
2642-
loadable_exts = nothing
2643-
if triggers !== nothing # extension
2644-
loadable_exts = PkgId[]
2645-
for (ext′, triggers′) in EXT_PRIMED
2646-
if triggers′ triggers
2647-
push!(loadable_exts, ext′)
2637+
loaded = let path = path, reasons = reasons
2638+
maybe_cachefile_lock(pkg, path) do
2639+
# double-check the search now that we have lock
2640+
m = _require_search_from_serialized(pkg, path, UInt128(0), true)
2641+
m isa Module && return m
2642+
triggers = get(EXT_PRIMED, pkg, nothing)
2643+
loadable_exts = nothing
2644+
if triggers !== nothing # extension
2645+
loadable_exts = PkgId[]
2646+
for (ext′, triggers′) in EXT_PRIMED
2647+
if triggers′ triggers
2648+
push!(loadable_exts, ext′)
2649+
end
26482650
end
26492651
end
2652+
return compilecache(pkg, path; reasons, loadable_exts)
26502653
end
2651-
return compilecache(pkg, path; reasons, loadable_exts)
26522654
end
26532655
loaded isa Module && return loaded
26542656
if isnothing(loaded) # maybe_cachefile_lock returns nothing if it had to wait for another process

0 commit comments

Comments
 (0)