Skip to content

Commit

Permalink
load extensions with fewer triggers earlier (#49891)
Browse files Browse the repository at this point in the history
Aimed to support the use case in
#48734 (comment).

https://github.com/KristofferC/ExtSquared.jl is an example, see
specifically
https://github.com/KristofferC/ExtSquared.jl/blob/ded7c57d6f799674e3310b8174dfb07591bbe025/ext/BExt.jl#L4.

I think this makes sense, happy for a second pair of eyes though.

cc @termi-official

---------

Co-authored-by: KristofferC <[email protected]>
Co-authored-by: Cody Tapscott <[email protected]>
  • Loading branch information
3 people authored Oct 30, 2024
1 parent 599b7ec commit 717bf54
Showing 1 changed file with 14 additions and 16 deletions.
30 changes: 14 additions & 16 deletions base/loading.jl
Original file line number Diff line number Diff line change
Expand Up @@ -1459,6 +1459,7 @@ end
mutable struct ExtensionId
const id::PkgId
const parentid::PkgId # just need the name, for printing
const n_total_triggers::Int
ntriggers::Int # how many more packages must be defined until this is loaded
end

Expand Down Expand Up @@ -1554,7 +1555,7 @@ function _insert_extension_triggers(parent::PkgId, extensions::Dict{String, Any}
continue # extension is already primed or loaded, don't add it again
end
EXT_PRIMED[id] = parent
gid = ExtensionId(id, parent, 1 + length(triggers))
gid = ExtensionId(id, parent, 1 + length(triggers), 1 + length(triggers))
trigger1 = get!(Vector{ExtensionId}, EXT_DORMITORY, parent)
push!(trigger1, gid)
for trigger in triggers
Expand Down Expand Up @@ -1598,25 +1599,22 @@ function run_extension_callbacks(pkgid::PkgId)
# take ownership of extids that depend on this pkgid
extids = pop!(EXT_DORMITORY, pkgid, nothing)
extids === nothing && return
extids_to_load = Vector{ExtensionId}()
for extid in extids
if extid.ntriggers > 0
# indicate pkgid is loaded
extid.ntriggers -= 1
end
if extid.ntriggers < 0
# indicate pkgid is loaded
extid.ntriggers += 1
succeeded = false
else
succeeded = true
end
@assert extid.ntriggers > 0
extid.ntriggers -= 1
if extid.ntriggers == 0
# actually load extid, now that all dependencies are met,
# and record the result
succeeded = succeeded && run_extension_callbacks(extid)
succeeded || push!(EXT_DORMITORY_FAILED, extid)
push!(extids_to_load, extid)
end
end
# Load extensions with the fewest triggers first
sort!(extids_to_load, by=extid->extid.n_total_triggers)
for extid in extids_to_load
# actually load extid, now that all dependencies are met,
succeeded = run_extension_callbacks(extid)
succeeded || push!(EXT_DORMITORY_FAILED, extid)
end

return
end

Expand Down

0 comments on commit 717bf54

Please sign in to comment.