Optimize TreeModel: cache KFormat to prevent repeated allocations.
authorMilian Wolff <milian.wolff@kdab.com>
Fri, 22 Jan 2016 12:26:12 +0000 (13:26 +0100)
committerMilian Wolff <milian.wolff@kdab.com>
Mon, 8 Feb 2016 16:31:08 +0000 (17:31 +0100)
gui/treemodel.cpp
gui/treemodel.h

index 2aa992d..24fcede 100644 (file)
@@ -42,6 +42,12 @@ const RowData* rowAt(const TreeData& rows, int row)
     return rows.data() + row;
 }
 
+/// @return the parent row containing @p index
+const RowData* toParentRow(const QModelIndex& index)
+{
+    return static_cast<const RowData*>(index.internalPointer());
+}
+
 }
 
 TreeModel::TreeModel(QObject* parent)
@@ -133,13 +139,12 @@ QVariant TreeModel::data(const QModelIndex& index, int role) const
     }
     const auto row = toRow(index);
     if (role == Qt::DisplayRole || role == SortRole) {
-        KFormat format;
         switch (static_cast<Columns>(index.column())) {
         case AllocatedColumn:
             if (role == SortRole) {
                 return row->allocated;
             } else {
-                return format.formatByteSize(row->allocated);
+                return m_format.formatByteSize(row->allocated);
             }
         case AllocationsColumn:
             return row->allocations;
@@ -149,13 +154,13 @@ QVariant TreeModel::data(const QModelIndex& index, int role) const
             if (role == SortRole) {
                 return row->peak;
             } else {
-                return format.formatByteSize(row->peak);
+                return m_format.formatByteSize(row->peak);
             }
         case LeakedColumn:
             if (role == SortRole) {
                 return row->leaked;
             } else {
-                return format.formatByteSize(row->leaked);
+                return m_format.formatByteSize(row->leaked);
             }
         case FunctionColumn:
             return row->location->function;
@@ -267,12 +272,6 @@ const RowData* TreeModel::toRow(const QModelIndex& index) const
     }
 }
 
-const RowData* TreeModel::toParentRow(const QModelIndex& index) const
-{
-    Q_ASSERT(index.isValid());
-    return static_cast<const RowData*>(index.internalPointer());
-}
-
 int TreeModel::rowOf(const RowData* row) const
 {
     if (auto parent = row->parent) {
index 5bad4e6..220346d 100644 (file)
@@ -23,6 +23,8 @@
 #include <QAbstractItemModel>
 #include <QVector>
 
+#include <KFormat>
+
 #include <boost/functional/hash.hpp>
 
 #include <memory>
@@ -122,12 +124,12 @@ public slots:
 private:
     /// @return the row resembled by @p index
     const RowData* toRow(const QModelIndex& index) const;
-    /// @return the parent row containing @p index
-    const RowData* toParentRow(const QModelIndex& index) const;
     /// @return the row number of @p row in its parent
     int rowOf(const RowData* row) const;
 
     TreeData m_data;
+    // TODO: update via global event filter when the locale changes (changeEvent)
+    KFormat m_format;
 };
 
 #endif // TREEMODEL_H