This commit is contained in:
Pranay Sashank 2024-08-05 20:24:42 +00:00 committed by GitHub
commit 3bb1443d51
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194

View File

@ -44,6 +44,7 @@ import GHC.Generics ( Generic )
import Prelude hiding ( span ) import Prelude hiding ( span )
-- containers -- containers
import Data.Containers.ListUtils ( nubOrd )
import Data.Map.Strict ( Map ) import Data.Map.Strict ( Map )
import qualified Data.Map.Strict as Map import qualified Data.Map.Strict as Map
import Data.Sequence ( Seq ) import Data.Sequence ( Seq )
@ -730,22 +731,30 @@ requestEvidence n d = do
} }
-- | Follow the given evidence uses back to their instance bindings, -- | Follow the given evidence use back to their instance bindings
-- and connect the declaration to those bindings. followEvidenceUses :: RefMap TypeIndex -> Name -> [Declaration]
followEvidenceUses :: RefMap TypeIndex -> Declaration -> Set Name -> Graph Declaration followEvidenceUses rf name =
followEvidenceUses refMap d names = let evidenceInfos = maybe [] (nubOrd . Tree.flatten) (getEvidenceTree rf name)
let getEvidenceTrees = mapMaybe (getEvidenceTree refMap) . Set.toList -- Often, we get duplicates in the flattened evidence trees. Sometimes, it's
evidenceInfos = concatMap Tree.flatten (getEvidenceTrees names) -- just one or two elements and other times there are 5x as many
instanceEvidenceInfos = evidenceInfos & filter \case instanceEvidenceInfos = evidenceInfos & filter \case
EvidenceInfo _ _ _ (Just (EvInstBind _ _, ModuleScope, _)) -> True EvidenceInfo _ _ _ (Just (EvInstBind _ _, ModuleScope, _)) -> True
_ -> False _ -> False
evBindSiteDecls = mapMaybe (nameToDeclaration . evidenceVar) instanceEvidenceInfos in mapMaybe (nameToDeclaration . evidenceVar) instanceEvidenceInfos
in star d evBindSiteDecls
-- | Follow evidence uses listed under 'requestedEvidence' back to their -- | Follow evidence uses listed under 'requestedEvidence' back to their
-- instance bindings, and connect their corresponding declaration to those bindings. -- instance bindings, and connect their corresponding declaration to those bindings.
analyseEvidenceUses :: RefMap TypeIndex -> Analysis -> Analysis analyseEvidenceUses :: RefMap TypeIndex -> Analysis -> Analysis
analyseEvidenceUses rf a@Analysis{ requestedEvidence, dependencyGraph } = analyseEvidenceUses rf a@Analysis{ requestedEvidence, dependencyGraph } = do
let graphs = map (uncurry (followEvidenceUses rf)) $ Map.toList requestedEvidence let combinedNames = mconcat (Map.elems requestedEvidence)
-- We combine all the names in all sets into one set, because the names
-- are duplicated a lot. In one example, the number of elements in the
-- combined sizes of all the sets are 16961625 as opposed to the
-- number of elements by combining all sets into one: 200330, that's an
-- 80x difference!
declMap = Map.fromSet (followEvidenceUses rf) combinedNames
-- Map.! is safe because declMap contains all elements of v by definition
graphs = map (\(d, v) -> star d ((nubOrd $ foldMap (declMap Map.!) v)))
(Map.toList requestedEvidence)
in a { dependencyGraph = overlays (dependencyGraph : graphs) } in a { dependencyGraph = overlays (dependencyGraph : graphs) }