Wolfgang Jeltsch pushed to branch wip/jeltsch/file-and-dir-cache-with-fast-string-keys at Glasgow Haskell Compiler / GHC Commits: cd118727 by Wolfgang Jeltsch at 2026-11-23T18:32:09+02:00 Switch file and directory cache to `FastString` keys Resolves #28139. - - - - - 4 changed files: - compiler/GHC/HsToCore/Usage.hs - compiler/GHC/Iface/Recomp.hs - compiler/GHC/Unit/Finder.hs - compiler/GHC/Unit/Finder/Types.hs Changes: ===================================== 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 ===================================== @@ -86,6 +86,8 @@ import GHC.Types.Unique.Map import GHC.Types.Unique.Set import GHC.Types.UnresolvedImport +import GHC.Data.FastString (FastString, unpackFS) + import Control.Applicative ((<|>)) import Control.Exception (throwIO) import Control.Monad @@ -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 emptyUniqMap + dir_cache <- newIORef emptyUniqMap let flushFinderCaches :: IO () flushFinderCaches = do clearHomeFinderCache home_cache - atomicModifyIORef' file_cache $ \_ -> (M.empty, ()) - atomicModifyIORef' dir_cache $ \_ -> (M.empty, ()) + atomicModifyIORef' file_cache $ \_ -> (emptyUniqMap, ()) + atomicModifyIORef' dir_cache $ \_ -> (emptyUniqMap, ()) - lookupFileCache :: FilePath -> IO Fingerprint + lookupFileCache :: FastString -> IO Fingerprint lookupFileCache key = do c <- readIORef file_cache - case M.lookup key c of + case lookupUniqMap 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 -> (addToUniqMap 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 lookupUniqMap 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 -> (addToUniqMap 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/cd118727b224adc454734a0e66cae7a9... -- View it on GitLab: https://gitlab.haskell.org/ghc/ghc/-/commit/cd118727b224adc454734a0e66cae7a9... 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