Repository navigation
Defer ghc-pkg dump - #12594
Defer ghc-pkg dump#12594philderbeast wants to merge 13 commits into
Conversation
878bea6 to
d116291
Compare
d116291 to
458f92b
Compare
| = ProjectInstalledIndex | ||
| (IO InstalledPackageIndex) | ||
| -- ^ The bulk read of every package db the plan touches, run at most once. | ||
| (MVar (Maybe InstalledPackageIndex)) |
There was a problem hiding this comment.
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?
There was a problem hiding this comment.
I've switched to using an Either so that the IO action is unavailable after the read.
There was a problem hiding this comment.
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. |
There was a problem hiding this comment.
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
left a comment
There was a problem hiding this comment.
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.
| (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. |
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
I've done this and added a little more from my own understanding.
| Nothing -> do | ||
| ipi <- load | ||
| return (Just ipi, ipi) |
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
This is fixed by use of memoizeMVar, isn't it?
|
Motivated by the AI-assisted code review discussion needing examples, I reviewed this pull request with GlaHiCo: cabalism/cabal-reviews#1. |
c6c0c21 to
128b634
Compare
I've vendored and am using |
Great. Would you like me to take another look? |
Fixes #12592.
QA Notes
Use the same script from the issue to compare before and after the fix:
significance: significantin the changelog file.