Skip to content

Commit 779890e

Browse files
authored
Use Data.HashMap.lookupKey and Data.HashSet.lookupElement (#4802)
1 parent 024d7ba commit 779890e

2 files changed

Lines changed: 12 additions & 33 deletions

File tree

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

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -321,7 +321,7 @@ getLocatedImportsRule :: Recorder (WithPriority Log) -> Rules ()
321321
getLocatedImportsRule recorder =
322322
define (cmapWithPrio LogShake recorder) $ \GetLocatedImports file -> do
323323
ModSummaryResult{msrModSummary = ms} <- use_ GetModSummaryWithoutTimestamps file
324-
(KnownTargets targets targetsMap) <- useNoFile_ GetKnownTargets
324+
(KnownTargets targets) <- useNoFile_ GetKnownTargets
325325
#if MIN_VERSION_ghc(9,13,0)
326326
let imports = [(False, lvl, mbPkgName, modName) | (lvl, mbPkgName, modName) <- ms_textual_imps ms]
327327
++ [(True, NormalLevel, NoPkgQual, noLoc modName) | L _ modName <- ms_srcimps ms]
@@ -334,14 +334,13 @@ getLocatedImportsRule recorder =
334334
let dflags = hsc_dflags env
335335
opt <- getIdeOptions
336336
let getTargetFor modName nfp
337-
| Just (TargetFile nfp') <- HM.lookup (TargetFile nfp) targetsMap = do
337+
| Just (TargetFile nfp') <- HM.lookupKey (TargetFile nfp) targets = do
338338
-- reuse the existing NormalizedFilePath in order to maximize sharing
339339
itExists <- getFileExists nfp'
340340
return $ if itExists then Just nfp' else Nothing
341341
| Just tt <- HM.lookup (TargetModule modName) targets = do
342342
-- reuse the existing NormalizedFilePath in order to maximize sharing
343-
let ttmap = HM.mapWithKey const (HashSet.toMap tt)
344-
nfp' = HM.lookupDefault nfp nfp ttmap
343+
let nfp' = fromMaybe nfp $ HashSet.lookupElement nfp tt
345344
itExists <- getFileExists nfp'
346345
return $ if itExists then Just nfp' else Nothing
347346
| 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)