diff mbox series

[04/11] kernel-shark-qt: Optimize the search in the text data

Message ID 20181121151356.16901-6-ykaradzhov@vmware.com (mailing list archive)
State Accepted
Headers show
Series Small modifications and bug fixes toward KS 1.0 | expand

Commit Message

Yordan Karadzhov Nov. 21, 2018, 3:14 p.m. UTC
This patch aims to optimize the search by doing two small
modifications:

It changes the type of the matching function used when searching.
This will eliminate the call of the QString copy constructor.

It also adds an intermediate step in the conversion of the tracing
data into the format expected by the QTableView widget (QVariant).
By having this intermediate step we avoid the unnecessary conversion
QString -> QVariant -> QString when searching.

Signed-off-by: Yordan Karadzhov <ykaradzhov@vmware.com>
---
 kernel-shark-qt/src/KsModels.cpp      | 28 ++++++++++++++++-----------
 kernel-shark-qt/src/KsModels.hpp      |  4 +++-
 kernel-shark-qt/src/KsTraceViewer.cpp |  6 +++---
 kernel-shark-qt/src/KsTraceViewer.hpp |  3 ---
 4 files changed, 23 insertions(+), 18 deletions(-)
diff mbox series

Patch

diff --git a/kernel-shark-qt/src/KsModels.cpp b/kernel-shark-qt/src/KsModels.cpp
index 5eb1646..d67ee62 100644
--- a/kernel-shark-qt/src/KsModels.cpp
+++ b/kernel-shark-qt/src/KsModels.cpp
@@ -59,8 +59,7 @@  void KsFilterProxyModel::_search(int column,
 {
 	int row, nRows(last - first + 1);
 	int pbCount(1);
-	QVariant item;
-	QString text;
+	QString item;
 
 	_searchProgress = 0;
 
@@ -76,10 +75,9 @@  void KsFilterProxyModel::_search(int column,
 		 * of the row number in the base model.
 		 */
 		row = mapRowFromSource(r);
-		item = _source->getValue(column, row);
-		if (cond(searchText, item.toString())) {
+		item = _source->getValueStr(column, row);
+		if (cond(searchText, item))
 			matchList->append(row);
-		}
 
 		if (_searchStop) {
 			_searchStop = false;
@@ -156,7 +154,6 @@  QList<int> KsFilterProxyModel::searchMap(int column,
 					 bool notify)
 {
 	QList<int> matchList;
-	qInfo() << "searchMap" << first << last;
 	_search(column, searchText, cond, &matchList, first, last,
 		nullptr, nullptr, notify);
 
@@ -203,15 +200,17 @@  QVariant KsViewModel::data(const QModelIndex &index, int role) const
 	return {};
 }
 
-/** Get the data stored in a given cell of the table. */
-QVariant KsViewModel::getValue(int column, int row) const
+/** Get the string data stored in a given cell of the table. */
+QString KsViewModel::getValueStr(int column, int row) const
 {
+	int pid;
+
 	switch (column) {
 		case TRACE_VIEW_COL_INDEX :
-			return row;
+			return QString("%1").arg(row);
 
 		case TRACE_VIEW_COL_CPU:
-			return _data[row]->cpu;
+			return QString("%1").arg(_data[row]->cpu);
 
 		case TRACE_VIEW_COL_TS:
 			return KsUtils::Ts2String(_data[row]->ts, 6);
@@ -220,7 +219,8 @@  QVariant KsViewModel::getValue(int column, int row) const
 			return kshark_get_task_easy(_data[row]);
 
 		case TRACE_VIEW_COL_PID:
-			return kshark_get_pid_easy(_data[row]);
+			pid = kshark_get_pid_easy(_data[row]);
+			return QString("%1").arg(pid);
 
 		case TRACE_VIEW_COL_LAT:
 			return kshark_get_latency_easy(_data[row]);
@@ -236,6 +236,12 @@  QVariant KsViewModel::getValue(int column, int row) const
 	}
 }
 
+/** Get the data stored in a given cell of the table. */
+QVariant KsViewModel::getValue(int column, int row) const
+{
+	return getValueStr(column, row);
+}
+
 /**
  * Get the header of a column. This is an implementation of the pure virtual
  * method of the abstract model class.
diff --git a/kernel-shark-qt/src/KsModels.hpp b/kernel-shark-qt/src/KsModels.hpp
index 00b20b2..b66c259 100644
--- a/kernel-shark-qt/src/KsModels.hpp
+++ b/kernel-shark-qt/src/KsModels.hpp
@@ -28,7 +28,7 @@ 
 #include "libkshark-model.h"
 
 /** Matching condition function type. To be user for searching. */
-typedef bool (*condition_func)(QString, QString);
+typedef bool (*condition_func)(const QString &, const QString &);
 
 enum class DualMarkerState;
 
@@ -81,6 +81,8 @@  public:
 	/** Get the list of column's headers. */
 	QStringList header() const {return _header;}
 
+	QString getValueStr(int column, int row) const;
+
 	QVariant getValue(int column, int row) const;
 
 	size_t search(int column,
diff --git a/kernel-shark-qt/src/KsTraceViewer.cpp b/kernel-shark-qt/src/KsTraceViewer.cpp
index 7f12365..e92dd83 100644
--- a/kernel-shark-qt/src/KsTraceViewer.cpp
+++ b/kernel-shark-qt/src/KsTraceViewer.cpp
@@ -275,17 +275,17 @@  void KsTraceViewer::_graphFollowsChanged(int state)
 		emit select(*_it); // Send a signal to the Graph widget.
 }
 
-static bool notHaveCond(QString searchText, QString itemText)
+static bool notHaveCond(const QString &searchText, const QString &itemText)
 {
 	return !itemText.contains(searchText, Qt::CaseInsensitive);
 }
 
-static bool containsCond(QString searchText, QString itemText)
+static bool containsCond(const QString &searchText, const QString &itemText)
 {
 	return itemText.contains(searchText, Qt::CaseInsensitive);
 }
 
-static bool matchCond(QString searchText, QString itemText)
+static bool matchCond(const QString &searchText, const QString &itemText)
 {
 	return (itemText.compare(searchText, Qt::CaseInsensitive) == 0);
 }
diff --git a/kernel-shark-qt/src/KsTraceViewer.hpp b/kernel-shark-qt/src/KsTraceViewer.hpp
index 2a85e4f..de8af97 100644
--- a/kernel-shark-qt/src/KsTraceViewer.hpp
+++ b/kernel-shark-qt/src/KsTraceViewer.hpp
@@ -20,9 +20,6 @@ 
 #include "KsModels.hpp"
 #include "KsDualMarker.hpp"
 
-/** Matching condition function type. To be user for searchong. */
-typedef bool (*condition_func)(QString, QString);
-
 /**
  * The KsTraceViewer class provides a widget for browsing in the trace data
  * shown in a text form.