Optimize: Replace QHash with std::array.
authorMilian Wolff <milian.wolff@kdab.com>
Fri, 23 Oct 2015 08:49:47 +0000 (10:49 +0200)
committerMilian Wolff <milian.wolff@kdab.com>
Fri, 23 Oct 2015 08:49:47 +0000 (10:49 +0200)
We now know that chart cost data is monotonously indexed and can
thus use a plain array over a QHash. This is much quicker to index
into in Parser::handleTimeStamp, and removes a hotspot found with
perf when analyzing a big data file.

gui/chartmodel.cpp
gui/chartmodel.h
gui/parser.cpp

index 6cae774..0925995 100644 (file)
@@ -114,8 +114,9 @@ QVariant ChartModel::data(const QModelIndex& index, int role) const
         return data.timeStamp;
     }
     column = column / 2;
+    Q_ASSERT(column < ChartRows::MAX_NUM_COST);
 
-    const auto cost = data.cost.value(column);
+    const auto cost = data.cost[column];
     if (role == Qt::ToolTipRole) {
         const QString time = QString::number(double(data.timeStamp) / 1000, 'g', 3) + QLatin1Char('s');
         const auto label = m_data.labels.value(column);
index c5627c1..77404da 100644 (file)
 
 #include <QAbstractTableModel>
 #include <QVector>
+#include <array>
 
 struct ChartRows
 {
+    ChartRows()
+    {
+        cost.fill(0);
+    }
+    enum {
+        MAX_NUM_COST = 20
+    };
     quint64 timeStamp = 0;
-    QHash<int, quint64> cost;
+    std::array<quint64, MAX_NUM_COST> cost;
 };
 Q_DECLARE_TYPEINFO(ChartRows, Q_MOVABLE_TYPE);
 
index a380949..a674d75 100644 (file)
@@ -151,8 +151,7 @@ struct ParserData final : public AccumulatedTraceData
             sort(merged.begin(), merged.end(), [=] (const ChartMergeData& left, const ChartMergeData& right) {
                 return left.*member > right.*member;
             });
-            const size_t MAX_CHART_FUNCTIONS = 20;
-            for (size_t i = 0; i < min(size_t(MAX_CHART_FUNCTIONS), merged.size()); ++i) {
+            for (size_t i = 0; i < min(size_t(ChartRows::MAX_NUM_COST), merged.size()); ++i) {
                 const auto& alloc = merged[i];
                 if (!(alloc.*member)) {
                     break;
@@ -187,7 +186,7 @@ struct ParserData final : public AccumulatedTraceData
         auto createRow = [] (uint64_t timeStamp, uint64_t totalCost) {
             ChartRows row;
             row.timeStamp = timeStamp;
-            row.cost.insert(0, totalCost);
+            row.cost[0] = totalCost;
             return row;
         };
         auto consumed = createRow(newStamp, nowConsumed);