Only allocate 100 brushes for the flamegraph.
authorMilian Wolff <mail@milianw.de>
Sun, 11 Oct 2015 16:19:05 +0000 (18:19 +0200)
committerMilian Wolff <mail@milianw.de>
Sun, 11 Oct 2015 21:40:22 +0000 (23:40 +0200)
More colors are probably not discernible anyways. This way,
we can save a couple of megabytes of memory for large input
files.

gui/flamegraph.cpp

index e715ec8e6e06d974381d2645bee58917fc14007b..5c91d8cc7ea52c25f0679117753a9442e62b64e6 100644 (file)
@@ -118,11 +118,19 @@ void FrameGraphicsItem::hoverLeaveEvent(QGraphicsSceneHoverEvent *event)
 namespace {
 
 /**
- * Generate a color from the "mem" color space used in upstream FlameGraph.pl
+ * Generate a brush from the "mem" color space used in upstream FlameGraph.pl
  */
-QColor color()
+QBrush brush()
 {
-    return QColor(0, 190 + 50 * qreal(rand()) / RAND_MAX, 210 * qreal(rand()) / RAND_MAX, 125);
+    // intern the brushes, to reuse them across items which can be thousands
+    // otherwise we'd end up with dozens of allocations and higher memory consumption
+    static QVector<QBrush> brushes;
+    if (brushes.isEmpty()) {
+        std::generate_n(std::back_inserter(brushes), 100, [] () {
+            return QColor(0, 190 + 50 * qreal(rand()) / RAND_MAX, 210 * qreal(rand()) / RAND_MAX, 125);
+        });
+    }
+    return brushes.at(rand() % brushes.size());
 }
 
 /**
@@ -169,7 +177,7 @@ void toGraphicsItems(const QVector<RowData>& data, FrameGraphicsItem *parent)
         if (!item) {
             item = new FrameGraphicsItem(row.allocations, row.location.function, parent);
             item->setPen(parent->pen());
-            item->setBrush(color());
+            item->setBrush(brush());
         } else {
             item->setCost(item->cost() + row.allocations);
         }