Skip to content

Commit 1b0d252

Browse files
committed
Use Data.HashMap.lookupKey and Data.HashSet.lookupElement
1 parent 3ac41e3 commit 1b0d252

File tree

2 files changed

+12
-33
lines changed

2 files changed

+12
-33
lines changed

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

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -318,7 +318,7 @@ getLocatedImportsRule :: Recorder (WithPriority Log) -> Rules ()
318318
getLocatedImportsRule recorder =
319319
define (cmapWithPrio LogShake recorder) $ \GetLocatedImports file -> do
320320
ModSummaryResult{msrModSummary = ms} <- use_ GetModSummaryWithoutTimestamps file
321-
(KnownTargets targets targetsMap) <- useNoFile_ GetKnownTargets
321+
(KnownTargets targets) <- useNoFile_ GetKnownTargets
322322
#if MIN_VERSION_ghc(9,13,0)
323323
let imports = [(False, lvl, mbPkgName, modName) | (lvl, mbPkgName, modName) <- ms_textual_imps ms]
324324
++ [(True, NormalLevel, NoPkgQual, noLoc modName) | L _ modName <- ms_srcimps ms]
@@ -331,14 +331,13 @@ getLocatedImportsRule recorder =
331331
let dflags = hsc_dflags env
332332
opt <- getIdeOptions
333333
let getTargetFor modName nfp
334-
| Just (TargetFile nfp') <- HM.lookup (TargetFile nfp) targetsMap = do
334+
| Just (TargetFile nfp') <- HM.lookupKey (TargetFile nfp) targets = do
335335
-- reuse the existing NormalizedFilePath in order to maximize sharing
336336
itExists <- getFileExists nfp'
337337
return $ if itExists then Just nfp' else Nothing
338338
| Just tt <- HM.lookup (TargetModule modName) targets = do
339339
-- reuse the existing NormalizedFilePath in order to maximize sharing
340-
let ttmap = HM.mapWithKey const (HashSet.toMap tt)
341-
nfp' = HM.lookupDefault nfp nfp ttmap
340+
let nfp' = fromMaybe nfp $ HashSet.lookupElement nfp tt
342341
itExists <- getFileExists nfp'
343342
return $ if itExists then Just nfp' else Nothing
344343
| otherwise = do

ghcide/src/Development/IDE/Types/KnownTargets.hs

Lines changed: 9 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -19,49 +19,29 @@ import Development.IDE.Types.Location
1919
import GHC.Generics
2020

2121
-- | A mapping of module name to known files
22-
data KnownTargets = KnownTargets
23-
{ targetMap :: !(HashMap Target (HashSet NormalizedFilePath))
24-
-- | 'normalisingMap' is a cached copy of `HMap.mapKey const targetMap`
25-
--
26-
-- At startup 'GetLocatedImports' is called on all known files. Say you have 10000
27-
-- modules in your project then this leads to 10000 calls to 'GetLocatedImports'
28-
-- running concurrently.
29-
--
30-
-- In `GetLocatedImports` the known targets are consulted and the targetsMap
31-
-- is created by mapping the known targets. This map is used for introducing
32-
-- sharing amongst filepaths. This operation copies a local copy of the `target`
33-
-- map which is local to the rule.
34-
--
35-
-- @
36-
-- let targetsMap = HMap.mapWithKey const targets
37-
-- @
38-
--
39-
-- So now each rule has a 'HashMap' of size 10000 held locally to it and depending
40-
-- on how the threads are scheduled there will be 10000^2 elements in total
41-
-- allocated in 'HashMap's. This used a lot of memory.
42-
--
43-
-- Solution: Return the 'normalisingMap' in the result of the `GetKnownTargets` rule so it is shared across threads.
44-
, normalisingMap :: !(HashMap Target Target) } deriving Show
22+
newtype KnownTargets = KnownTargets
23+
{ targetMap :: (HashMap Target (HashSet NormalizedFilePath)) }
24+
deriving Show
4525

4626

4727
unionKnownTargets :: KnownTargets -> KnownTargets -> KnownTargets
48-
unionKnownTargets (KnownTargets tm nm) (KnownTargets tm' nm') =
49-
KnownTargets (HMap.unionWith (<>) tm tm') (HMap.union nm nm')
28+
unionKnownTargets (KnownTargets tm) (KnownTargets tm') =
29+
KnownTargets (HMap.unionWith (<>) tm tm')
5030

5131
mkKnownTargets :: [(Target, HashSet NormalizedFilePath)] -> KnownTargets
52-
mkKnownTargets vs = KnownTargets (HMap.fromList vs) (HMap.fromList [(k,k) | (k,_) <- vs ])
32+
mkKnownTargets vs = KnownTargets (HMap.fromList vs)
5333

5434
instance NFData KnownTargets where
55-
rnf (KnownTargets tm nm) = rnf tm `seq` rnf nm `seq` ()
35+
rnf (KnownTargets tm) = rnf tm `seq` ()
5636

5737
instance Eq KnownTargets where
5838
k1 == k2 = targetMap k1 == targetMap k2
5939

6040
instance Hashable KnownTargets where
61-
hashWithSalt s (KnownTargets hm _) = hashWithSalt s hm
41+
hashWithSalt s (KnownTargets hm) = hashWithSalt s hm
6242

6343
emptyKnownTargets :: KnownTargets
64-
emptyKnownTargets = KnownTargets HMap.empty HMap.empty
44+
emptyKnownTargets = KnownTargets HMap.empty
6545

6646
data Target = TargetModule ModuleName | TargetFile NormalizedFilePath
6747
deriving ( Eq, Ord, Generic, Show )

0 commit comments

Comments
 (0)