Skip to content

Commit a4a2a19

Browse files
committed
Avoid O(n) operation in loop
HMS.size is O(n) and getPathId gets called in the rawDependencyInformation loop
1 parent 3582203 commit a4a2a19

File tree

1 file changed

+10
-6
lines changed

1 file changed

+10
-6
lines changed

ghcide/src/Development/IDE/Import/DependencyInformation.hs

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -76,25 +76,29 @@ type FilePathIdSet = IntSet
7676
data PathIdMap = PathIdMap
7777
{ idToPathMap :: !(FilePathIdMap ArtifactsLocation)
7878
, pathToIdMap :: !(HashMap NormalizedFilePath FilePathId)
79+
, nextFreshId :: !Int
7980
}
8081
deriving (Show, Generic)
8182

8283
instance NFData PathIdMap
8384

8485
emptyPathIdMap :: PathIdMap
85-
emptyPathIdMap = PathIdMap IntMap.empty HMS.empty
86+
emptyPathIdMap = PathIdMap IntMap.empty HMS.empty 0
8687

8788
getPathId :: ArtifactsLocation -> PathIdMap -> (FilePathId, PathIdMap)
8889
getPathId path m@PathIdMap{..} =
8990
case HMS.lookup (artifactFilePath path) pathToIdMap of
9091
Nothing ->
91-
let !newId = FilePathId $ HMS.size pathToIdMap
92+
let !newId = FilePathId nextFreshId
9293
in (newId, insertPathId path newId m)
9394
Just id -> (id, m)
94-
95-
insertPathId :: ArtifactsLocation -> FilePathId -> PathIdMap -> PathIdMap
96-
insertPathId path id PathIdMap{..} =
97-
PathIdMap (IntMap.insert (getFilePathId id) path idToPathMap) (HMS.insert (artifactFilePath path) id pathToIdMap)
95+
where
96+
insertPathId :: ArtifactsLocation -> FilePathId -> PathIdMap -> PathIdMap
97+
insertPathId path id PathIdMap{..} =
98+
PathIdMap
99+
(IntMap.insert (getFilePathId id) path idToPathMap)
100+
(HMS.insert (artifactFilePath path) id pathToIdMap)
101+
(succ nextFreshId)
98102

99103
insertImport :: FilePathId -> Either ModuleParseError ModuleImports -> RawDependencyInformation -> RawDependencyInformation
100104
insertImport (FilePathId k) v rawDepInfo = rawDepInfo { rawImports = IntMap.insert k v (rawImports rawDepInfo) }

0 commit comments

Comments
 (0)