Skip to content

Commit 2749371

Browse files
pepeiborraAilrun
andauthored
Minor performance optimizations (#1432)
* Use a newtype * Avoid O(n) operation in loop HMS.size is O(n) and getPathId gets called in the rawDependencyInformation loop Co-authored-by: Junyoung/Clare Jang <[email protected]>
1 parent 152e57f commit 2749371

File tree

2 files changed

+11
-7
lines changed

2 files changed

+11
-7
lines changed

ghcide/src/Development/IDE/Core/RuleTypes.hs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -250,7 +250,7 @@ type instance RuleResult GetModIfaceWithoutLinkable = HiFileResult
250250
type instance RuleResult GetFileContents = (FileVersion, Maybe Text)
251251

252252
-- The Shake key type for getModificationTime queries
253-
data GetModificationTime = GetModificationTime_
253+
newtype GetModificationTime = GetModificationTime_
254254
{ missingFileDiagnostics :: Bool
255255
-- ^ If false, missing file diagnostics are not reported
256256
}

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)