Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix space leak where EPS retained HPTs from old HscEnv #2553

Merged
merged 2 commits into from
Dec 30, 2021
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 13 additions & 6 deletions ghcide/src/Development/IDE/Core/Compile.hs
Original file line number Diff line number Diff line change
Expand Up @@ -56,9 +56,9 @@ import HieDb
import Language.LSP.Types (DiagnosticTag (..))

#if MIN_VERSION_ghc(8,10,0)
import Control.DeepSeq (force, rnf)
import Control.DeepSeq (force, rnf, liftRnf, rwhnf)
#else
import Control.DeepSeq (rnf)
import Control.DeepSeq (rnf, liftRnf, rwhnf)
import ErrUtils
#endif

Expand Down Expand Up @@ -691,7 +691,8 @@ loadModulesHome
-> HscEnv
-> HscEnv
loadModulesHome mod_infos e =
e { hsc_HPT = addListToHpt (hsc_HPT e) [(mod_name x, x) | x <- mod_infos]
let !new_modules = addListToHpt (hsc_HPT e) [(mod_name x, x) | x <- mod_infos]
in e { hsc_HPT = new_modules
, hsc_type_env_var = Nothing }
where
mod_name = moduleName . mi_module . hm_iface
Expand All @@ -702,15 +703,21 @@ mergeEnvs env extraModSummaries extraMods envs = do
prevFinderCache <- concatFC <$> mapM (readIORef . hsc_FC) envs
let ims = map (Compat.installedModule (homeUnitId_ $ hsc_dflags env) . moduleName . ms_mod) extraModSummaries
ifrs = zipWith (\ms -> InstalledFound (ms_location ms)) extraModSummaries ims
-- Very important to force this as otherwise the hsc_mod_graph field is not
-- forced and ends up retaining a reference to all the old hsc_envs we have merged to get
-- this new one, which in turn leads to the EPS referencing the HPT.
module_graph_nodes =
extraModSummaries ++ nubOrdOn ms_mod (concatMap (mgModSummaries . hsc_mod_graph) envs)

newFinderCache <- newIORef $
foldl'
(\fc (im, ifr) -> Compat.extendInstalledModuleEnv fc im ifr) prevFinderCache
$ zip ims ifrs
return $ loadModulesHome extraMods $ env{
liftRnf rwhnf module_graph_nodes `seq` (return $ loadModulesHome extraMods $ env{
hsc_HPT = foldMapBy mergeUDFM emptyUDFM hsc_HPT envs,
hsc_FC = newFinderCache,
hsc_mod_graph = mkModuleGraph $ extraModSummaries ++ nubOrdOn ms_mod (concatMap (mgModSummaries . hsc_mod_graph) envs)
}
hsc_mod_graph = mkModuleGraph module_graph_nodes
})
where
mergeUDFM = plusUDFM_C combineModules
combineModules a b
Expand Down