Skip to content

Commit 3ca4005

Browse files
authored
Cache NormalizedFilePath in ArtifactLocation (#445)
Previously PathIdMap cached NormalizedFilePath values, but with the addition of module data those got dropped. It's probably a good idea to bring those back to avoid the risk of a perf regression
1 parent 48a7867 commit 3ca4005

File tree

3 files changed

+15
-18
lines changed

3 files changed

+15
-18
lines changed

src/Development/IDE/Core/Rules.hs

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -177,7 +177,8 @@ getLocatedImportsRule =
177177
-- imports recursively.
178178
rawDependencyInformation :: NormalizedFilePath -> Action RawDependencyInformation
179179
rawDependencyInformation f = do
180-
let (initialId, initialMap) = getPathId (ArtifactsLocation $ ModLocation (Just $ fromNormalizedFilePath f) "" "") emptyPathIdMap
180+
let initialArtifact = ArtifactsLocation f (ModLocation (Just $ fromNormalizedFilePath f) "" "")
181+
(initialId, initialMap) = getPathId initialArtifact emptyPathIdMap
181182
go (IntSet.singleton $ getFilePathId initialId)
182183
(RawDependencyInformation IntMap.empty initialMap)
183184
where
@@ -270,7 +271,8 @@ getSpanInfoRule =
270271
parsedDeps <- mapMaybe (fmap fst) <$> usesWithStale GetParsedModule (transitiveModuleDeps deps)
271272
(fileImports, _) <- use_ GetLocatedImports file
272273
packageState <- hscEnv <$> use_ GhcSession file
273-
x <- liftIO $ getSrcSpanInfos packageState (fmap (second (fmap modLocationToNormalizedFilePath)) fileImports) tc parsedDeps
274+
let imports = second (fmap artifactFilePath) <$> fileImports
275+
x <- liftIO $ getSrcSpanInfos packageState imports tc parsedDeps
274276
return ([], Just x)
275277

276278
-- Typechecks a module.

src/Development/IDE/Import/DependencyInformation.hs

Lines changed: 5 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,6 @@ module Development.IDE.Import.DependencyInformation
1818
, pathToId
1919
, idToPath
2020
, reachableModules
21-
, modLocationToNormalizedFilePath
2221
, processDependencyInformation
2322
, transitiveDeps
2423
) where
@@ -76,27 +75,20 @@ data PathIdMap = PathIdMap
7675

7776
instance NFData PathIdMap
7877

79-
modLocationToNormalizedFilePath :: ArtifactsLocation -> NormalizedFilePath
80-
modLocationToNormalizedFilePath (ArtifactsLocation loc) =
81-
case ml_hs_file loc of
82-
Just filePath -> toNormalizedFilePath filePath
83-
-- Since we craete all 'ModLocation' values via 'mkHomeModLocation'
84-
Nothing -> error "Has something changed in mkHomeModLocation?"
85-
8678
emptyPathIdMap :: PathIdMap
8779
emptyPathIdMap = PathIdMap IntMap.empty HMS.empty
8880

8981
getPathId :: ArtifactsLocation -> PathIdMap -> (FilePathId, PathIdMap)
9082
getPathId path m@PathIdMap{..} =
91-
case HMS.lookup (modLocationToNormalizedFilePath path) pathToIdMap of
83+
case HMS.lookup (artifactFilePath path) pathToIdMap of
9284
Nothing ->
9385
let !newId = FilePathId $ HMS.size pathToIdMap
9486
in (newId, insertPathId path newId m)
9587
Just id -> (id, m)
9688

9789
insertPathId :: ArtifactsLocation -> FilePathId -> PathIdMap -> PathIdMap
9890
insertPathId path id PathIdMap{..} =
99-
PathIdMap (IntMap.insert (getFilePathId id) path idToPathMap) (HMS.insert (modLocationToNormalizedFilePath path) id pathToIdMap)
91+
PathIdMap (IntMap.insert (getFilePathId id) path idToPathMap) (HMS.insert (artifactFilePath path) id pathToIdMap)
10092

10193
insertImport :: FilePathId -> Either ModuleParseError ModuleImports -> RawDependencyInformation -> RawDependencyInformation
10294
insertImport (FilePathId k) v rawDepInfo = rawDepInfo { rawImports = IntMap.insert k v (rawImports rawDepInfo) }
@@ -105,7 +97,7 @@ pathToId :: PathIdMap -> NormalizedFilePath -> FilePathId
10597
pathToId PathIdMap{pathToIdMap} path = pathToIdMap HMS.! path
10698

10799
idToPath :: PathIdMap -> FilePathId -> NormalizedFilePath
108-
idToPath pathIdMap filePathId = modLocationToNormalizedFilePath $ idToModLocation pathIdMap filePathId
100+
idToPath pathIdMap filePathId = artifactFilePath $ idToModLocation pathIdMap filePathId
109101

110102
idToModLocation :: PathIdMap -> FilePathId -> ArtifactsLocation
111103
idToModLocation PathIdMap{idToPathMap} (FilePathId id) = idToPathMap IntMap.! id
@@ -305,9 +297,9 @@ transitiveDeps DependencyInformation{..} file = do
305297
let transitiveModuleDeps =
306298
map (idToPath depPathIdMap . FilePathId) transitiveModuleDepIds
307299
let transitiveNamedModuleDeps =
308-
[ NamedModuleDep (idToPath depPathIdMap (FilePathId fid)) mn ml
300+
[ NamedModuleDep (idToPath depPathIdMap (FilePathId fid)) mn artifactModLocation
309301
| (fid, ShowableModuleName mn) <- IntMap.toList depModuleNames
310-
, let ArtifactsLocation ml = idToPathMap depPathIdMap IntMap.! fid
302+
, let ArtifactsLocation{artifactModLocation} = idToPathMap depPathIdMap IntMap.! fid
311303
]
312304
pure TransitiveDependencies {..}
313305
where

src/Development/IDE/Import/FindImports.hs

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -34,11 +34,14 @@ data Import
3434
| PackageImport !M.InstalledUnitId
3535
deriving (Show)
3636

37-
newtype ArtifactsLocation = ArtifactsLocation ModLocation
37+
data ArtifactsLocation = ArtifactsLocation
38+
{ artifactFilePath :: !NormalizedFilePath
39+
, artifactModLocation :: !ModLocation
40+
}
3841
deriving (Show)
3942

4043
instance NFData ArtifactsLocation where
41-
rnf = const ()
44+
rnf ArtifactsLocation{..} = rnf artifactFilePath `seq` rwhnf artifactModLocation
4245

4346
instance NFData Import where
4447
rnf (FileImport x) = rnf x
@@ -94,7 +97,7 @@ locateModule dflags exts doesExist modName mbPkgName isSource = do
9497
where
9598
toModLocation file = liftIO $ do
9699
loc <- mkHomeModLocation dflags (unLoc modName) (fromNormalizedFilePath file)
97-
return $ Right $ FileImport $ ArtifactsLocation loc
100+
return $ Right $ FileImport $ ArtifactsLocation file loc
98101

99102

100103
lookupInPackageDB dfs =

0 commit comments

Comments
 (0)