Wolfgang Jeltsch pushed to branch wip/jeltsch/more-efficient-home-unit-imports-finding at Glasgow Haskell Compiler / GHC Commits: 8363b1a8 by Wolfgang Jeltsch at 2026-04-29T16:43:54+03:00 Polish code in the finder - - - - - 1 changed file: - compiler/GHC/Unit/Finder.hs Changes: ===================================== compiler/GHC/Unit/Finder.hs ===================================== @@ -77,7 +77,8 @@ import GHC.Driver.Config.Finder import GHC.Types.Unique.Set import qualified Data.List as L(sort) import Data.List.NonEmpty ( NonEmpty (..) ) -import qualified Data.Set as Set +import Data.Set (Set) +import qualified Data.Set as Set (empty, intersection, difference, null, toList) import qualified System.Directory as SD import qualified System.OsPath as OsPath import qualified Data.List.NonEmpty as NE @@ -214,74 +215,104 @@ findImportedModuleNoHsc fc fopts ue home_module_name_providers_map mb_home_unit | otherwise -> pprPanic "findImportModule" (ppr mod_name $$ ppr mb_pkg $$ ppr (homeUnitId <$> mb_home_unit) $$ ppr uid $$ ppr (map fst all_opts)) OtherPkg _ -> pkg_import where + + mb_home_unit_id :: Maybe UnitId mb_home_unit_id = homeUnitId <$> mb_home_unit + + all_opts :: [(UnitId, FinderOpts)] all_opts = case mb_home_unit_id of - Nothing -> other_fopts - Just home_unit_id -> (home_unit_id, fopts) : other_fopts + Nothing -> other_fopts + Just home_unit_id -> (home_unit_id, fopts) : other_fopts + home_import :: IO FindResult home_import = case mb_home_unit of - Just home_unit -> findHomeModule fc fopts home_unit mod_name - Nothing -> pure $ NoPackage (panic "findImportedModule: no home-unit") - - + Just home_unit -> findHomeModule fc fopts home_unit mod_name + Nothing -> pure $ + NoPackage (panic "findImportedModule: no home-unit") + + {- + If the module is reexported, then look for it as if it was from the + perspective of that package which reexports it. + -} + home_pkg_import :: (UnitId, FinderOpts) -> IO FindResult home_pkg_import (uid, opts) - -- If the module is reexported, then look for it as if it was from the perspective - -- of that package which reexports it. - | Just real_mod_name <- lookupUniqMap (finder_reexportedModules opts) mod_name = - findImportedModuleNoHsc fc opts ue home_module_name_providers_map (Just $ DefiniteHomeUnit uid Nothing) real_mod_name NoPkgQual - | elementOfUniqSet mod_name (finder_hiddenModules opts) = - return (mkHomeHidden uid) - | otherwise = - findHomePackageModule fc opts uid mod_name - - -- Do not be smart and change this to `foldr orIfNotFound home_import hs` as - -- that is not the same!! home_import is first because we need to look within ourselves - -- first before looking at the packages in order. - any_home_import = foldr1 orIfNotFound (home_import :| map home_pkg_import other_fopts) - - pkg_import = findExposedPackageModule fc fopts units mod_name mb_pkg - - unqual_import = any_home_import - `orIfNotFound` - findExposedPackageModule fc fopts units mod_name NoPkgQual - - units = case mb_home_unit_id of - Nothing -> ue_homeUnitState ue - Just home_unit_id -> HUG.homeUnitEnv_units $ ue_findHomeUnitEnv home_unit_id ue - - hpt_deps :: Set.Set UnitId - hpt_deps = homeUnitDepends units - - sorted_deps = rankedHomeUnits home_module_name_providers_map mod_name hpt_deps - - other_fopts = - [ (uid, initFinderOpts (homeUnitEnv_dflags (ue_findHomeUnitEnv uid ue))) - | uid <- sorted_deps - ] - -rankedHomeUnits :: HomeModuleNameProvidersMap - -> ModuleName - -> Set.Set UnitId - -> [UnitId] -rankedHomeUnits _ _ hpt_deps | Set.null hpt_deps + | Just real_mod_name + <- lookupUniqMap (finder_reexportedModules opts) mod_name + = findImportedModuleNoHsc fc opts ue home_module_name_providers_map + (Just $ DefiniteHomeUnit uid Nothing) + real_mod_name + NoPkgQual + | elementOfUniqSet mod_name (finder_hiddenModules opts) + = return (mkHomeHidden uid) + | otherwise + = findHomePackageModule fc opts uid mod_name + + any_home_import :: IO FindResult + any_home_import = foldr1 orIfNotFound $ + home_import :| map home_pkg_import other_fopts + {- + Do not try to be smart and change this to `foldr orIfNotFound + home_import (map home_pkg_import other_fopts)`, as that would not be the + same. `home_import` is first because we need to first look within the + current unit before looking at the other units in order. + -} + + pkg_import :: IO FindResult + pkg_import = findExposedPackageModule fc fopts unit_state mod_name mb_pkg + + unqual_import :: IO FindResult + unqual_import + = any_home_import + `orIfNotFound` + findExposedPackageModule fc fopts unit_state mod_name NoPkgQual + + unit_state :: UnitState + unit_state = case mb_home_unit_id of + Nothing -> ue_homeUnitState ue + Just home_unit_id -> HUG.homeUnitEnv_units $ + ue_findHomeUnitEnv home_unit_id ue + + home_unit_deps :: Set UnitId + home_unit_deps = homeUnitDepends unit_state + + ranked_home_unit_deps :: [UnitId] + ranked_home_unit_deps = rankedHomeUnitDeps home_module_name_providers_map + mod_name + home_unit_deps + + other_fopts :: [(UnitId, FinderOpts)] + other_fopts + = [ + (uid, opts) | + uid <- ranked_home_unit_deps, + let opts = initFinderOpts $ + homeUnitEnv_dflags (ue_findHomeUnitEnv uid ue) + ] + +rankedHomeUnitDeps :: HomeModuleNameProvidersMap + -> ModuleName + -> Set UnitId + -> [UnitId] +rankedHomeUnitDeps _ _ home_unit_deps | Set.null home_unit_deps = [] -rankedHomeUnits home_module_name_providers_map mod_name hpt_deps +rankedHomeUnitDeps home_module_name_providers_map mod_name home_unit_deps = Set.toList cached_deps ++ Set.toList uncached_deps where - cached_providers :: Set.Set UnitId + cached_providers :: Set UnitId cached_providers = lookupWithDefaultUniqMap home_module_name_providers_map Set.empty mod_name - cached_deps :: Set.Set UnitId - cached_deps = Set.intersection hpt_deps cached_providers + cached_deps :: Set UnitId + cached_deps = Set.intersection home_unit_deps cached_providers - uncached_deps :: Set.Set UnitId - uncached_deps = Set.difference hpt_deps cached_providers + uncached_deps :: Set UnitId + uncached_deps = Set.difference home_unit_deps cached_providers {- - The special handling of the case that the dependency set is empty does not - change the result, but it avoids triggering evaluation of the module graph. + The special handling of the situation where the dependency set is empty does + not change the result, but it avoids triggering evaluation of the module + graph. -} -- | Locate a plugin module requested by the user, for a compiler View it on GitLab: https://gitlab.haskell.org/ghc/ghc/-/commit/8363b1a8844ca5bdd07181af8042c4d3... -- View it on GitLab: https://gitlab.haskell.org/ghc/ghc/-/commit/8363b1a8844ca5bdd07181af8042c4d3... You're receiving this email because of your account on gitlab.haskell.org.
participants (1)
-
Wolfgang Jeltsch (@jeltsch)