Wolfgang Jeltsch pushed to branch wip/jeltsch/file-and-dir-cache-with-fast-string-keys at Glasgow Haskell Compiler / GHC Commits: 64b2923f by Wolfgang Jeltsch at 2026-11-25T15:28:20+02:00 Switch fingerprint caches to `FastString` keys Resolves #28139. - - - - - 5 changed files: - + changelog.d/fingerprint-caches-with-fast-string-keys - compiler/GHC/HsToCore/Usage.hs - compiler/GHC/Iface/Recomp.hs - compiler/GHC/Unit/Finder.hs - compiler/GHC/Unit/Finder/Types.hs Changes: ===================================== changelog.d/fingerprint-caches-with-fast-string-keys ===================================== @@ -0,0 +1,9 @@ +section: compiler +synopsis: Switch fingerprint caches to ``FastString`` keys +issues: #28139 +mrs: !17042 +description: { + The caches of file and directory fingerprints now use ``FastString`` + keys instead of ``FilePath`` keys, to avoid certain allocations + within the compiler. +} ===================================== compiler/GHC/HsToCore/Usage.hs ===================================== @@ -205,7 +205,7 @@ mkObjectUsage plugins fc th_links_needed th_pkgs_needed = do , usg_bytecode_hash = bclu_hash } - fing mmsg fn = UsageFile (mkFastString fn) <$> lookupFileCache fc fn <*> pure mmsg + fing mmsg fn = let fs = mkFastString fn in UsageFile fs <$> lookupFileCache fc fs <*> pure mmsg librarySpecToUsage :: LibrarySpec -> IO [Usage] librarySpecToUsage (Objects os) = traverse (fing Nothing) os ===================================== compiler/GHC/Iface/Recomp.hs ===================================== @@ -833,7 +833,7 @@ checkModUsage fc UsageFile{ usg_file_path = file, usg_file_label = mlabel } = liftIO $ handleIO handler $ do - new_hash <- lookupFileCache fc $ unpackFS file + new_hash <- lookupFileCache fc file if (old_hash /= new_hash) then return recomp else return UpToDate @@ -849,7 +849,7 @@ checkModUsage fc UsageDirectory{ usg_dir_path = dir, usg_dir_label = mlabel } = liftIO $ handleIO handler $ do - new_hash <- lookupDirCache fc $ unpackFS dir + new_hash <- lookupDirCache fc dir if (old_hash /= new_hash) then return recomp else return UpToDate ===================================== compiler/GHC/Unit/Finder.hs ===================================== @@ -55,6 +55,8 @@ import GHC.Platform.Ways import GHC.Data.IOEnv (IOEnv, runIOEnv, getEnv) import GHC.Data.OsPath import qualified GHC.Data.ShortText as ST +import GHC.Data.FastString (FastString, unpackFS) +import GHC.Data.FastString.Env (emptyFsEnv, lookupFsEnv, extendFsEnv) import GHC.Driver.Env import GHC.Driver.Config.Finder @@ -94,7 +96,6 @@ import Data.IORef import qualified Data.List as L(sort) import Data.List.NonEmpty ( NonEmpty (..) ) import qualified Data.List.NonEmpty as NE -import qualified Data.Map as M import Data.Set (Set) import qualified Data.Set as Set (empty, intersection, difference, null, toList) import Data.Time @@ -122,30 +123,30 @@ type BaseName = OsPath -- Basename of file initFinderCache :: IO FinderCache initFinderCache = do home_cache <- newHomeFinderCache - file_cache <- newIORef M.empty - dir_cache <- newIORef M.empty + file_cache <- newIORef emptyFsEnv + dir_cache <- newIORef emptyFsEnv let flushFinderCaches :: IO () flushFinderCaches = do clearHomeFinderCache home_cache - atomicModifyIORef' file_cache $ \_ -> (M.empty, ()) - atomicModifyIORef' dir_cache $ \_ -> (M.empty, ()) + atomicModifyIORef' file_cache $ \_ -> (emptyFsEnv, ()) + atomicModifyIORef' dir_cache $ \_ -> (emptyFsEnv, ()) - lookupFileCache :: FilePath -> IO Fingerprint + lookupFileCache :: FastString -> IO Fingerprint lookupFileCache key = do c <- readIORef file_cache - case M.lookup key c of + case lookupFsEnv c key of Nothing -> do - hash <- getFileHash key - atomicModifyIORef' file_cache $ \c -> (M.insert key hash c, ()) + hash <- getFileHash (unpackFS key) + atomicModifyIORef' file_cache $ \c -> (extendFsEnv c key hash, ()) return hash Just fp -> return fp - lookupDirCache :: FilePath -> IO Fingerprint + lookupDirCache :: FastString -> IO Fingerprint lookupDirCache key = do c <- readIORef dir_cache - case M.lookup key c of + case lookupFsEnv c key of Nothing -> do - hash <- getDirHash key - atomicModifyIORef' dir_cache $ \c -> (M.insert key hash c, ()) + hash <- getDirHash (unpackFS key) + atomicModifyIORef' dir_cache $ \c -> (extendFsEnv c key hash, ()) return hash Just fp -> return fp return $ ===================================== compiler/GHC/Unit/Finder/Types.hs ===================================== @@ -48,10 +48,10 @@ data FinderCache = , flushFinderCaches :: IO () -- ^ Flush all home-module search caches, and also flush the file and -- directory hash caches. - , lookupFileCache :: FilePath -> IO Fingerprint + , lookupFileCache :: FastString -> IO Fingerprint -- ^ Look for the hash of a file in the cache. This should add it to the -- cache. If the file doesn't exist, raise an IOException. - , lookupDirCache :: FilePath -> IO Fingerprint + , lookupDirCache :: FastString -> IO Fingerprint -- ^ Like 'lookupFileCache', but for a directory. } View it on GitLab: https://gitlab.haskell.org/ghc/ghc/-/commit/64b2923f2fbc58d34ba2d5b79d12aba5... -- View it on GitLab: https://gitlab.haskell.org/ghc/ghc/-/commit/64b2923f2fbc58d34ba2d5b79d12aba5... You're receiving this email because of your account on gitlab.haskell.org. Manage all notifications: https://gitlab.haskell.org/-/profile/notifications | Help: https://gitlab.haskell.org/help
participants (1)
-
Wolfgang Jeltsch (@jeltsch)