[llvm] [BOLT][NFC] Eliminate uses of throwing std::map::at (PR #92950)

shaw young via llvm-commits llvm-commits at lists.llvm.org
Wed May 22 09:16:58 PDT 2024


================
@@ -1553,36 +1553,28 @@ Error PrintProgramStats::runOnFunctions(BinaryContext &BC) {
     const bool Ascending =
         opts::DynoStatsSortOrderOpt == opts::DynoStatsSortOrder::Ascending;
 
-    if (SortAll) {
-      llvm::stable_sort(Functions,
-                        [Ascending, &Stats](const BinaryFunction *A,
-                                            const BinaryFunction *B) {
-                          auto StatsItr = Stats.find(A);
-                          assert(StatsItr != Stats.end());
-                          const DynoStats &StatsA = StatsItr->second;
-
-                          StatsItr = Stats.find(B);
-                          assert(StatsItr != Stats.end());
-                          const DynoStats &StatsB = StatsItr->second;
-
-                          return Ascending ? StatsA < StatsB : StatsB < StatsA;
-                        });
-    } else {
-      llvm::stable_sort(
-          Functions, [Ascending, &Stats](const BinaryFunction *A,
-                                         const BinaryFunction *B) {
-            auto StatsItr = Stats.find(A);
-            assert(StatsItr != Stats.end());
-            const DynoStats &StatsA = StatsItr->second;
-
-            StatsItr = Stats.find(B);
-            assert(StatsItr != Stats.end());
-            const DynoStats &StatsB = StatsItr->second;
-
-            return Ascending ? StatsA.lessThan(StatsB, opts::PrintSortedBy)
-                             : StatsB.lessThan(StatsA, opts::PrintSortedBy);
-          });
-    }
+    std::function<bool(const DynoStats &, const DynoStats &)>
+        DynoStatsComparator =
+            SortAll ? [](const DynoStats &StatsA,
----------------
shawbyoung wrote:

I'm interpreting sinking SortAll as also sinking the ternary operator into the return statement such that we return either StatsA < StatsB or StatsA.lessThan(StatsB, opts::PrintSortedBy) conditioned on SortAll. However, my thinking is that with my current implementation we'll only have to check SortAll once, instead of at every comparison, which would be marginally more efficient. Did you mean something different by sinking the SortAll or was my thinking correct and your reasoning more for code brevity?

https://github.com/llvm/llvm-project/pull/92950


More information about the llvm-commits mailing list