]> granicus.if.org Git - llvm/commitdiff
Time profiler: optimize json output time
authorAnton Afanasyev <anton.a.afanasyev@gmail.com>
Tue, 16 Apr 2019 20:36:56 +0000 (20:36 +0000)
committerAnton Afanasyev <anton.a.afanasyev@gmail.com>
Tue, 16 Apr 2019 20:36:56 +0000 (20:36 +0000)
Summary:
Use llvm::json::Array.reserve() to optimize json output time. Here is motivation:
https://reviews.llvm.org/D60609#1468941. In short: for the json array
with ~32K entries, pushing back each entry takes ~4% of whole time compared
to the method of preliminary memory reservation: (3995-3845)/3995 = 3.75%.

Reviewers: lebedev.ri

Subscribers: hiraditya, llvm-commits

Tags: #llvm

Differential Revision: https://reviews.llvm.org/D60792

git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@358522 91177308-0d34-0410-b5e6-96231b3b80d8

lib/Support/TimeProfiler.cpp

index 6c7e8def7076cfe9c603a3b8aac9c5ed9bbc808e..447ddf5ab3c9acee8460820d42028d8dca07d176 100644 (file)
@@ -89,6 +89,9 @@ struct TimeTraceProfiler {
            "All profiler sections should be ended when calling Write");
 
     json::Array Events;
+    const size_t ExpectedEntryCount =
+        Entries.size() + CountAndTotalPerName.size() + 1;
+    Events.reserve(ExpectedEntryCount);
 
     // Emit all events for the main flame graph.
     for (const auto &E : Entries) {
@@ -149,6 +152,8 @@ struct TimeTraceProfiler {
         {"args", json::Object{{"name", "clang"}}},
     });
 
+    assert(Events.size() == ExpectedEntryCount && "Size prediction failed!");
+
     OS << formatv("{0:2}", json::Value(json::Object(
                                {{"traceEvents", std::move(Events)}})));
   }