Skip to content

Defer ghc-pkg dump - #12594

Open
philderbeast wants to merge 13 commits into
haskell:masterfrom
cabalism:fix/defer-ghc-pkg-dump
Open

philderbeast wants to merge 13 commits into
haskell:masterfrom
cabalism:fix/defer-ghc-pkg-dump

Conversation

@philderbeast

@philderbeast philderbeast commented Nov 14, 2026 •

Copy link
Copy Markdown
Collaborator

Fixes #12592.

QA Notes

Use the same script from the issue to compare before and after the fix:

  • Before the fix:
$ ./repl-dump-timing.sh cabal Cabal-syntax 3 --builddir=dist-cabal-before
cabal                        dumps  dump span  before GHCi  whole run
3.19.0.0 run 1                   4     4.96 s      5.54 s   10.98 s
3.19.0.0 run 2                   3     3.45 s      3.94 s    9.46 s
3.19.0.0 run 3                   3     3.42 s      3.90 s    9.50 s
  • After the fix:
$ ./repl-dump-timing.sh cabal Cabal-syntax 3 --builddir=dist-cabal-after
cabal                        dumps  dump span  before GHCi  whole run
3.19.0.0 run 1                   4     5.07 s      5.71 s   11.22 s
3.19.0.0 run 2                   0          -      0.44 s    5.98 s
3.19.0.0 run 3                   0          -      0.45 s    6.08 s

@philderbeast
philderbeast force-pushed the fix/defer-ghc-pkg-dump branch 2 times, most recently from 878bea6 to d116291 Compare November 14, 2026 17:59
= ProjectInstalledIndex
(IO InstalledPackageIndex)
-- ^ The bulk read of every package db the plan touches, run at most once.
(MVar (Maybe InstalledPackageIndex))

@Bodigrim Bodigrim Nov 15, 2026 •

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.

It's not like we are doing any takeMVar / putMVar, so can we use IORef instead of MVar?

Also, could it be

newtype ProjectInstalledIndex = ProjectInstalledIndex 
  (IORef (Either (IO InstalledPackageIndex) InstalledPackageIndex))

so that inconsistent states cannot be represented?

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

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

I've switched to using an Either so that the IO action is unavailable after the read.

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.

Awesome.

Any time I see MVar in the code I must read it very carefully to check for any takeMVar / putMVar interaction, which can block my program indefinitely. MVar is a low-level building block, and unless we are doing something very peculiar with racing threads other instruments such as IORef and atomicModifyIORef' are usually preferred.


-- | Record a library that was just registered. Before the first read this is
-- a no-op: the registration went to one of the package dbs that the read
-- covers, so the index will pick it up then.

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.

The explanation seems to imply some specific constraints on how insertProjectInstalledIndex must be used, which breaks the abstraction. I don't see a strong reason why we cannot just insert InstalledPackageInfo whatever the state of InstalledPackageInfo is by using bimap instead of fmap.

@sheaf
sheaf self-requested a review November 16, 2026 09:32

@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.

I think this is absolutely the correct thing to do, however I think we should use memoizeMVar to implement the ProjectInstalledIndex.

I suggest vendoring memoizeMVar just like we vendor parts of async. Then the code for ProjectInstalledIndex could be something like:

data ProjectInstalledIndex
  = ProjectInstalledIndex
      (Memoized InstalledPackageIndex)
      -- ^ (ProjIPI1): bulk read of package DBs
      (IORef InstalledPackageIndex)
      -- ^ (ProjIPI2): libraries registered during this run 

newProjectInstalledIndex :: IO InstalledPackageIndex -> IO ProjectInstalledIndex
newProjectInstalledIndex readPackageDbs =
  ProjectInstalledIndex <$> memoizeMVar readPackageDbs <*> newIORef mempty

readProjectInstalledIndex :: ProjectInstalledIndex -> IO InstalledPackageIndex
readProjectInstalledIndex (ProjectInstalledIndex packageDbs registered) =
  PackageIndex.merge <$> runMemoized packageDbs <*> readIORef registered

insertProjectInstalledIndex :: InstalledPackageInfo -> ProjectInstalledIndex -> IO ()
insertProjectInstalledIndex ipkg (ProjectInstalledIndex _ registered) =
  atomicModifyIORef' registered $ \idx -> (PackageIndex.insert ipkg idx, ())

This ensures that if ghc-pkg dump throws an exception, we store the exception instead of continually re-running; so we memoise whether the computation succeeded or failed, instead of only memoising on success.

There is still the question of reporting the same error for each package we configure, but that can be addressed separately, I don't think we need to figure it out right now.

Comment on lines +497 to +503
(ProjIPI2)
During execution, each time we register a library, we insert its
InstalledPackageInfo into the index, so that subsequent packages that
depend on it have it available, without needing to re-query @ghc-pkg@.

Before the first read the insert is a no-op: the library was registered
into one of the DBs the read covers, so the read will include it.

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.

I think we should rephrase this in terms of the invariants the code relies on rather than an overview of the implementation.

  (ProjIPI2)
    During execution, each time we register a library, we insert its
    InstalledPackageInfo into the index, so that subsequent packages that
    depend on it have it available, without needing to re-query ghc-pkg.

    Crucially, we ensure that this insertion does not depend on whether the bulk
    read of (ProjIPI1) has run yet: the index always consists of the bulk read
    together with every library registered so far in this run, regardless of the
    order in which these were done.

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

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

I've done this and added a little more from my own understanding.

Comment on lines +227 to +229
Nothing -> do
ipi <- load
return (Just ipi, ipi)

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.

If load fails (e.g. ghc-pkg fails) then the original contents of the MVar are preserved. If that happens, it means that every single time we try to read the index we will re-fail, over and over.

So I think ProjectInstalledIndex is set up slightly wrong.

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

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

This is fixed by use of memoizeMVar, isn't it?

@philderbeast

Copy link
Copy Markdown
Collaborator Author

Motivated by the AI-assisted code review discussion needing examples, I reviewed this pull request with GlaHiCo: cabalism/cabal-reviews#1.

@philderbeast
philderbeast force-pushed the fix/defer-ghc-pkg-dump branch from c6c0c21 to 128b634 Compare December 3, 2026 20:02
@philderbeast

Copy link
Copy Markdown
Collaborator Author

I think we should use memoizeMVar to implement the ProjectInstalledIndex

I've vendored and am using memoizeMVar. I brought over its tests too.

@sheaf

sheaf commented Dec 3, 2026

Copy link
Copy Markdown
Collaborator

I've vendored and am using memoizeMVar. I brought over its tests too.

Great. Would you like me to take another look?

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

Projects

None yet

Development

Successfully merging this pull request may close these issues.

cabal repl dumps all package dbs

4 participants