Wolfgang Jeltsch pushed to branch wip/jeltsch/file-and-dir-cache-with-fast-string-keys at Glasgow Haskell Compiler / GHC

Commits:

5 changed files:

Changes:

  • changelog.d/fingerprint-caches-with-fast-string-keys
    1
    +section: compiler
    
    2
    +synopsis: Switch fingerprint caches to ``FastString`` keys
    
    3
    +issues: #28139
    
    4
    +mrs: !17042
    
    5
    +description: {
    
    6
    +    The caches of file and directory fingerprints now use ``FastString``
    
    7
    +    keys instead of ``FilePath`` keys, to avoid certain allocations
    
    8
    +    within the compiler.
    
    9
    +}

  • compiler/GHC/HsToCore/Usage.hs
    ... ... @@ -205,7 +205,7 @@ mkObjectUsage plugins fc th_links_needed th_pkgs_needed = do
    205 205
                   , usg_bytecode_hash = bclu_hash
    
    206 206
                   }
    
    207 207
     
    
    208
    -    fing mmsg fn = UsageFile (mkFastString fn) <$> lookupFileCache fc fn <*> pure mmsg
    
    208
    +    fing mmsg fn = let fs = mkFastString fn in UsageFile fs <$> lookupFileCache fc fs <*> pure mmsg
    
    209 209
     
    
    210 210
         librarySpecToUsage :: LibrarySpec -> IO [Usage]
    
    211 211
         librarySpecToUsage (Objects os) = traverse (fing Nothing) os
    

  • compiler/GHC/Iface/Recomp.hs
    ... ... @@ -833,7 +833,7 @@ checkModUsage fc UsageFile{ usg_file_path = file,
    833 833
                                 usg_file_label = mlabel } =
    
    834 834
       liftIO $
    
    835 835
         handleIO handler $ do
    
    836
    -      new_hash <- lookupFileCache fc $ unpackFS file
    
    836
    +      new_hash <- lookupFileCache fc file
    
    837 837
           if (old_hash /= new_hash)
    
    838 838
              then return recomp
    
    839 839
              else return UpToDate
    
    ... ... @@ -849,7 +849,7 @@ checkModUsage fc UsageDirectory{ usg_dir_path = dir,
    849 849
                                      usg_dir_label = mlabel } =
    
    850 850
       liftIO $
    
    851 851
         handleIO handler $ do
    
    852
    -      new_hash <- lookupDirCache fc $ unpackFS dir
    
    852
    +      new_hash <- lookupDirCache fc dir
    
    853 853
           if (old_hash /= new_hash)
    
    854 854
              then return recomp
    
    855 855
              else return UpToDate
    

  • compiler/GHC/Unit/Finder.hs
    ... ... @@ -55,6 +55,8 @@ import GHC.Platform.Ways
    55 55
     import GHC.Data.IOEnv (IOEnv, runIOEnv, getEnv)
    
    56 56
     import GHC.Data.OsPath
    
    57 57
     import qualified GHC.Data.ShortText as ST
    
    58
    +import GHC.Data.FastString (FastString, unpackFS)
    
    59
    +import GHC.Data.FastString.Env (emptyFsEnv, lookupFsEnv, extendFsEnv)
    
    58 60
     
    
    59 61
     import GHC.Driver.Env
    
    60 62
     import GHC.Driver.Config.Finder
    
    ... ... @@ -94,7 +96,6 @@ import Data.IORef
    94 96
     import qualified Data.List as L(sort)
    
    95 97
     import Data.List.NonEmpty ( NonEmpty (..) )
    
    96 98
     import qualified Data.List.NonEmpty as NE
    
    97
    -import qualified Data.Map as M
    
    98 99
     import Data.Set (Set)
    
    99 100
     import qualified Data.Set as Set (empty, intersection, difference, null, toList)
    
    100 101
     import Data.Time
    
    ... ... @@ -122,30 +123,30 @@ type BaseName = OsPath -- Basename of file
    122 123
     initFinderCache :: IO FinderCache
    
    123 124
     initFinderCache = do
    
    124 125
       home_cache <- newHomeFinderCache
    
    125
    -  file_cache <- newIORef M.empty
    
    126
    -  dir_cache <- newIORef M.empty
    
    126
    +  file_cache <- newIORef emptyFsEnv
    
    127
    +  dir_cache <- newIORef emptyFsEnv
    
    127 128
       let flushFinderCaches :: IO ()
    
    128 129
           flushFinderCaches = do
    
    129 130
             clearHomeFinderCache home_cache
    
    130
    -        atomicModifyIORef' file_cache $ \_ -> (M.empty, ())
    
    131
    -        atomicModifyIORef' dir_cache  $ \_ -> (M.empty, ())
    
    131
    +        atomicModifyIORef' file_cache $ \_ -> (emptyFsEnv, ())
    
    132
    +        atomicModifyIORef' dir_cache  $ \_ -> (emptyFsEnv, ())
    
    132 133
     
    
    133
    -      lookupFileCache :: FilePath -> IO Fingerprint
    
    134
    +      lookupFileCache :: FastString -> IO Fingerprint
    
    134 135
           lookupFileCache key = do
    
    135 136
              c <- readIORef file_cache
    
    136
    -         case M.lookup key c of
    
    137
    +         case lookupFsEnv c key of
    
    137 138
                Nothing -> do
    
    138
    -             hash <- getFileHash key
    
    139
    -             atomicModifyIORef' file_cache $ \c -> (M.insert key hash c, ())
    
    139
    +             hash <- getFileHash (unpackFS key)
    
    140
    +             atomicModifyIORef' file_cache $ \c -> (extendFsEnv c key hash, ())
    
    140 141
                  return hash
    
    141 142
                Just fp -> return fp
    
    142
    -      lookupDirCache :: FilePath -> IO Fingerprint
    
    143
    +      lookupDirCache :: FastString -> IO Fingerprint
    
    143 144
           lookupDirCache key = do
    
    144 145
              c <- readIORef dir_cache
    
    145
    -         case M.lookup key c of
    
    146
    +         case lookupFsEnv c key of
    
    146 147
                Nothing -> do
    
    147
    -             hash <- getDirHash key
    
    148
    -             atomicModifyIORef' dir_cache $ \c -> (M.insert key hash c, ())
    
    148
    +             hash <- getDirHash (unpackFS key)
    
    149
    +             atomicModifyIORef' dir_cache $ \c -> (extendFsEnv c key hash, ())
    
    149 150
                  return hash
    
    150 151
                Just fp -> return fp
    
    151 152
       return $
    

  • compiler/GHC/Unit/Finder/Types.hs
    ... ... @@ -48,10 +48,10 @@ data FinderCache =
    48 48
       , flushFinderCaches :: IO ()
    
    49 49
         -- ^ Flush all home-module search caches, and also flush the file and
    
    50 50
         -- directory hash caches.
    
    51
    -  , lookupFileCache   :: FilePath -> IO Fingerprint
    
    51
    +  , lookupFileCache   :: FastString -> IO Fingerprint
    
    52 52
         -- ^ Look for the hash of a file in the cache. This should add it to the
    
    53 53
         -- cache. If the file doesn't exist, raise an IOException.
    
    54
    -  , lookupDirCache    :: FilePath -> IO Fingerprint
    
    54
    +  , lookupDirCache    :: FastString -> IO Fingerprint
    
    55 55
         -- ^ Like 'lookupFileCache', but for a directory.
    
    56 56
       }
    
    57 57