From f0eedbce4418cfde0ee3bb18fcbd44fcb7f2da2d Mon Sep 17 00:00:00 2001 From: George Rimar Date: Tue, 28 Aug 2018 08:49:40 +0000 Subject: [PATCH] [LLD][ELF] - Simplify Call-Chain Clustering implementation a bit. Looking at the current implementation and algorithm description, it does not seem we need to keep vector with all edges for each cluster and can just remember the best one. This is NFC change. Differential revision: https://reviews.llvm.org/D50609 llvm-svn: 340806 --- lld/ELF/CallGraphSort.cpp | 26 ++++++++++---------------- 1 file changed, 10 insertions(+), 16 deletions(-) diff --git a/lld/ELF/CallGraphSort.cpp b/lld/ELF/CallGraphSort.cpp index 6a8da0f2dcde..113ae5736a26 100644 --- a/lld/ELF/CallGraphSort.cpp +++ b/lld/ELF/CallGraphSort.cpp @@ -72,7 +72,7 @@ struct Cluster { size_t Size = 0; uint64_t Weight = 0; uint64_t InitialWeight = 0; - std::vector Preds; + Edge BestPred = {-1, 0}; }; class CallGraphSort { @@ -136,8 +136,12 @@ CallGraphSort::CallGraphSort() { if (From == To) continue; - // Add an edge - Clusters[To].Preds.push_back({From, Weight}); + // Remember the best edge. + Cluster &ToC = Clusters[To]; + if (ToC.BestPred.From == -1 || ToC.BestPred.Weight < Weight) { + ToC.BestPred.From = From; + ToC.BestPred.Weight = Weight; + } } for (Cluster &C : Clusters) C.InitialWeight = C.Weight; @@ -181,21 +185,11 @@ void CallGraphSort::groupClusters() { // been merged into another cluster yet. Cluster &C = Clusters[SI]; - int BestPred = -1; - uint64_t BestWeight = 0; - - for (Edge &E : C.Preds) { - if (BestPred == -1 || E.Weight > BestWeight) { - BestPred = E.From; - BestWeight = E.Weight; - } - } - - // don't consider merging if the edge is unlikely. - if (BestWeight * 10 <= C.InitialWeight) + // Don't consider merging if the edge is unlikely. + if (C.BestPred.From == -1 || C.BestPred.Weight * 10 <= C.InitialWeight) continue; - Cluster *PredC = SecToCluster[BestPred]; + Cluster *PredC = SecToCluster[C.BestPred.From]; if (PredC == &C) continue;