Skip to content

Build the installed package closure from the component's dependencies - #12641

Open
Lotion-3 wants to merge 3 commits into
haskell:masterfrom
Lotion-3:perf/installed-closure
Open

Lotion-3 wants to merge 3 commits into
haskell:masterfrom
Lotion-3:perf/installed-closure

Conversation

@Lotion-3

Copy link
Copy Markdown
Contributor

Addresses part of #12546.

toComponentLocalBuildInfos built a Graph of every installed package, unioned it with the component graph and took the closure. Only installed packages reachable from the components can be in that closure, so this looks them up by UnitId and builds the graph from those. Installed packages with the same UnitId as a component are skipped, since unionRight replaced them, and missing dependencies are still reported by Graph.broken.

The closure is the same, including its order, which PackageIndex.fromList depends on for instances of the same package. I compared the old and new code on 3,600 random installed-package graphs, with shared package names, shadowed keys and missing dependencies: the closure, the PackageIndex built from it and Graph.broken were identical in every case.

Time for this step, per component (synthetic package index, GHC 9.10.3, Windows 11):

Installed packages Closure size Before After
1,000 154 0.45 ms 0.10 ms
5,000 533 3.9 ms 0.75 ms
20,000 1,230 37 ms 3.4 ms

This doesn't touch the other costs in #12546: PackageIndex.fromList and dependencyInconsistencies still run on the closure for each component.

The Backpack, BuildDeps, InternalLibraries and ConfigureComponent tests pass, except Backpack/Includes2 and InternalLibraries/setup-gen-script, which fail the same way on master on my Windows machine.


Template B: This PR does not modify behaviour or interface

toComponentLocalBuildInfos built a graph of every installed package, unioned
it with the component graph and took the closure. Only installed packages
reachable from the components can be in that closure, so look those up by
UnitId and build the graph from them. The closure is unchanged.

Addresses haskell#12546.
@Lotion-3
Lotion-3 force-pushed the perf/installed-closure branch from a8bdcf2 to 1a3cba1 Compare November 21, 2026 06:15

@sheaf sheaf left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This looks great, just a few small suggestions. Thanks for doing this!

combined_graph = Graph.unionRight external_graph internal_graph
local_graph =
fromMaybe (error "toComponentLocalBuildInfos: closure returned Nothing") $
Graph.closure combined_graph (map nodeKey graph)

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why do we still need to compute a closure? Didn't the DFS above already compute it?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The DFS only decides which installed packages go into the graph. I kept Graph.closure so that local_graph comes out in the same order as before, since PackageIndex.fromList keeps that order for instances of the same package. Happy to build it directly if the order doesn't matter there.

Comment on lines +236 to +243
reachable_installed = go Map.empty (concatMap nodeNeighbors graph)
where
go seen [] = Map.elems seen
go seen (uid : uids)
| uid `Set.member` internal_keys || uid `Map.member` seen = go seen uids
| Just pkg <- PackageIndex.lookupUnitId installedPackageSet uid =
go (Map.insert uid pkg seen) (nodeNeighbors pkg ++ uids)
| otherwise = go seen uids

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Could we refactor Distribution.Simple.PackageIndex.dependencyClosure to avoid re-implementing that logic here, e.g. by having a new function that returns the two lists ("completed", "failed") separately? We'd use that new function and dependencyClosure just wraps it in Either as it does now.

This would allow there to be a single place in the code where a dependency closure is computed.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done in ae70a07: dependencyClosureAndMissing returns the closure and the missing UnitIds separately, dependencyClosure wraps it, and this calls it on the index without the components' UnitIds. The results are the same on the 3,600 random cases; it takes 3.8 ms rather than 2.8 ms per component at 20,000 installed packages (36 ms on master).

Add dependencyClosureAndMissing, which returns the closure and the missing UnitIds separately, define dependencyClosure with it, and use it in toComponentLocalBuildInfos instead of a separate traversal.
@sheaf

sheaf commented Nov 23, 2026

Copy link
Copy Markdown
Collaborator

I had to manually turn off assertions to get similar behaviour to the numbers you reported.

dependencyClosure (now dependencyClosureAndMissing) calls Distribution.Simple.PackageIndex.insert which calls Distribution.Simple.PackageIndex.mkPackageIndex which has an assertion that is not compiled away because Cabal.cabal contains -fno-ignore-asserts. I think that assertion should be removed: it only checks internals of Distribution.Simple.PackageIndex.insert because PackageIndex is opaque, so the proper thing to do would be to test those internals instead of using an assertion. As it is, the assertion causes dependencyClosure to be quadratic in the size of the closure due to the check running on each insertion.

insert checks the PackageIndex invariant, which is not compiled out (-fno-ignore-asserts), so building the closure with one insert per package was quadratic. Collect the packages and use fromList once, which gives the same index. Also drop the deleteUnitId calls: an installed package shadowed by a component is replaced by Graph.unionRight, so it does not change the closure.
@Lotion-3

Copy link
Copy Markdown
Contributor Author

Thanks, I missed that: my benchmark linked GHC's installed Cabal. In b1970ce, dependencyClosureAndMissing builds the index once with fromList instead of inserting each package (same index, including the order of instances), and the deleteUnitId calls are gone: an installed package shadowed by a component is replaced by unionRight, so it doesn't change the closure. With the in-tree Cabal and assertions on, at 20,000 installed packages this step takes 38.6 ms on master and 5.8 ms now, and dependencyClosure on a 1,230-package closure goes from 241 ms to 4 ms. I left the assertion itself alone; happy to remove it here or in a separate PR.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants