[PATCH] D60404: Improve hashing for time profiler

Anton Afanasyev via Phabricator via llvm-commits llvm-commits at lists.llvm.org
Mon Apr 8 06:02:10 PDT 2019


anton-afanasyev updated this revision to Diff 194124.
anton-afanasyev added a comment.

Update: lint fix


Repository:
  rG LLVM Github Monorepo

CHANGES SINCE LAST ACTION
  https://reviews.llvm.org/D60404/new/

https://reviews.llvm.org/D60404

Files:
  llvm/lib/Support/TimeProfiler.cpp


Index: llvm/lib/Support/TimeProfiler.cpp
===================================================================
--- llvm/lib/Support/TimeProfiler.cpp
+++ llvm/lib/Support/TimeProfiler.cpp
@@ -51,7 +51,9 @@
 }
 
 typedef duration<steady_clock::rep, steady_clock::period> DurationType;
-typedef std::pair<std::string, DurationType> NameAndDuration;
+typedef std::pair<size_t, DurationType> CountAndDurationType;
+typedef std::pair<std::string, CountAndDurationType>
+    NameAndCountAndDurationType;
 
 struct Entry {
   time_point<steady_clock> Start;
@@ -89,8 +91,9 @@
     if (std::find_if(++Stack.rbegin(), Stack.rend(), [&](const Entry &Val) {
           return Val.Name == E.Name;
         }) == Stack.rend()) {
-      TotalPerName[E.Name] += E.Duration;
-      CountPerName[E.Name]++;
+      auto &CountAndTotal = CountAndTotalPerName[E.Name];
+      CountAndTotal.first++;
+      CountAndTotal.second += E.Duration;
     }
 
     Stack.pop_back();
@@ -115,23 +118,25 @@
     // Emit totals by section name as additional "thread" events, sorted from
     // longest one.
     int Tid = 1;
-    std::vector<NameAndDuration> SortedTotals;
-    SortedTotals.reserve(TotalPerName.size());
-    for (const auto &E : TotalPerName) {
-      SortedTotals.push_back(E);
+    std::vector<NameAndCountAndDurationType> SortedTotals;
+    SortedTotals.reserve(CountAndTotalPerName.size());
+    for (const auto &E : CountAndTotalPerName) {
+      SortedTotals.push_back(std::pair<std::string, CountAndDurationType>(
+          E.getKey(), E.getValue()));
     }
     std::sort(SortedTotals.begin(), SortedTotals.end(),
-              [](const NameAndDuration &A, const NameAndDuration &B) {
-                return A.second > B.second;
+              [](const NameAndCountAndDurationType &A,
+                 const NameAndCountAndDurationType &B) {
+                return A.second.second > B.second.second;
               });
     for (const auto &E : SortedTotals) {
-      auto DurUs = duration_cast<microseconds>(E.second).count();
+      auto DurUs = duration_cast<microseconds>(E.second.second).count();
       *OS << "{ \"pid\":1, \"tid\":" << Tid << ", \"ph\":\"X\", \"ts\":" << 0
           << ", \"dur\":" << DurUs << ", \"name\":\"Total "
           << escapeString(E.first)
-          << "\", \"args\":{ \"count\":" << CountPerName[E.first]
-          << ", \"avg ms\":" << (DurUs / CountPerName[E.first] / 1000)
-          << "} },\n";
+          << "\", \"args\":{ \"count\":" << CountAndTotalPerName[E.first].first
+          << ", \"avg ms\":"
+          << (DurUs / CountAndTotalPerName[E.first].first / 1000) << "} },\n";
       ++Tid;
     }
 
@@ -143,8 +148,7 @@
 
   std::vector<Entry> Stack;
   std::vector<Entry> Entries;
-  std::unordered_map<std::string, DurationType> TotalPerName;
-  std::unordered_map<std::string, size_t> CountPerName;
+  StringMap<CountAndDurationType> CountAndTotalPerName;
   time_point<steady_clock> StartTime;
 };
 


-------------- next part --------------
A non-text attachment was scrubbed...
Name: D60404.194124.patch
Type: text/x-patch
Size: 2957 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/llvm-commits/attachments/20190408/8da04d33/attachment.bin>


More information about the llvm-commits mailing list