Skip to content

Commit 1d4ea5f

Browse files
committed
Goes back to using ghc-pkg to bulk unregister packages
This shows a speedup from ~2m40s to ~1m20s for doing `stack build --only-dependencies` for a large (~370) transitive dependency set when no compilation of dependencies is actually required.
1 parent 23ee719 commit 1d4ea5f

1 file changed

Lines changed: 15 additions & 32 deletions

File tree

src/Stack/GhcPkg.hs

Lines changed: 15 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,6 @@ import qualified Data.ByteString.Lazy as BL
2525
import qualified Data.List as L
2626
import qualified Data.Text as T
2727
import qualified Data.Text.Encoding as T
28-
import GHC.Utils.GhcPkg.Main.Compat ( ghcPkgUnregisterForce )
2928
import Path ( (</>), parent )
3029
import Path.Extra ( toFilePathNoTrailingSep )
3130
import Path.IO
@@ -34,11 +33,7 @@ import RIO.Process ( HasProcessContext, proc, readProcess_ )
3433
import Stack.Constants ( relFilePackageCache )
3534
import Stack.Prelude
3635
import Stack.Types.Compiler ( WhichCompiler (..) )
37-
import Stack.Types.CompilerPaths
38-
( CompilerPaths (..), GhcPkgExe (..), HasCompiler
39-
, compilerPathsL
40-
)
41-
import Stack.Types.GhcPkgExe ( GhcPkgPrettyException (..) )
36+
import Stack.Types.CompilerPaths ( GhcPkgExe (..) )
4237
import Stack.Types.GhcPkgId ( GhcPkgId, ghcPkgIdString )
4338
import System.FilePath ( searchPathSeparator )
4439

@@ -154,50 +149,38 @@ findGhcPkgField pkgexe pkgDbs name field =
154149
-- using GHC package id where available (from GHC 7.9)
155150
--
156151
-- The version of the ghc-pkg executable supplied with GHCs published before
157-
-- 28 August 2023 does not efficiently bulk unregister. Until an \'efficient\'
158-
-- ghc-pkg is available, this function no longer uses:
159-
--
160-
-- > eres <- ghcPkg pkgexe [pkgDb] args
161-
-- > where
162-
-- > args = "unregister" : "--user" : "--force" :
163-
-- > map packageIdentifierString idents ++
164-
-- > if null gids then [] else "--ipid" : map ghcPkgIdString gids
165-
--
166-
-- but uses:
167-
--
168-
-- > globalDb <- view $ compilerPathsL.to cpGlobalDB
169-
-- > eres <- tryAny $ liftIO $
170-
-- > ghcPkgUnregisterUserForce globalDb pkgDb hasIpid pkgarg_strs
152+
-- 28 August 2023 does not efficiently bulk unregister. This function delegates
153+
-- the bulk unregister directly to the ghc-pkg executable, which is efficient
154+
-- with the versions of GHC that Stack now supports.
171155
--
172156
unregisterGhcPkgIds ::
173-
(HasCompiler env, HasProcessContext env, HasTerm env)
157+
(HasProcessContext env, HasTerm env)
174158
=> Bool
175159
-- ^ Report pretty exceptions as warnings?
176160
-> GhcPkgExe
177161
-> Path Abs Dir -- ^ package database
178162
-> NonEmpty (Either PackageIdentifier GhcPkgId)
179163
-> RIO env ()
180164
unregisterGhcPkgIds isWarn pkgexe pkgDb epgids = do
181-
globalDb <- view $ compilerPathsL . to (.globalDB)
182-
try (ghcPkgUnregisterForce globalDb pkgDb hasIpid pkgarg_strs) >>= \case
183-
Left (PrettyException e) -> when isWarn $
165+
-- The ghcPkg function supplies initial arguments
166+
-- --no-user-package-db --package-db=<db1> ... --package-db=<dbn>. The ghc-pkg
167+
-- executable bulk unregisters in a single invocation and recaches the package
168+
-- database itself, so no separate 'ghc-pkg recache' is required.
169+
ghcPkg pkgexe [pkgDb] args >>= \case
170+
Left e -> when isWarn $
184171
prettyWarn $
185172
"[S-8729]"
186173
<> line
187174
<> flow "While unregistering packages, Stack encountered the following \
188175
\error:"
189176
<> blankLine
190-
<> pretty e
191-
Right _ -> pure ()
192-
-- ghcPkgUnregisterForce does not perform an effective 'ghc-pkg recache', as
193-
-- that depends on a specific version of the Cabal package.
194-
ghcPkg pkgexe [pkgDb] ["recache"] >>= \case
195-
Left err -> prettyThrowM $ CannotRecacheAfterUnregister pkgDb err
177+
<> string (displayException e)
196178
Right _ -> pure ()
197179
where
198180
(idents, gids) = partitionEithers $ toList epgids
199-
hasIpid = not (null gids)
200-
pkgarg_strs = map packageIdentifierString idents <> map ghcPkgIdString gids
181+
args = "unregister" : "--force" :
182+
map packageIdentifierString idents <>
183+
if null gids then [] else "--ipid" : map ghcPkgIdString gids
201184

202185
-- | Get the value for GHC_PACKAGE_PATH
203186
mkGhcPackagePath :: Bool -> Path Abs Dir -> Path Abs Dir -> [Path Abs Dir] -> Path Abs Dir -> Text

0 commit comments

Comments
 (0)