From eccd9d2d92af2e890d68ddcc177ee5903913d099 Mon Sep 17 00:00:00 2001 From: hama Date: Thu, 14 Apr 2016 00:03:26 +0200 Subject: [PATCH] optimization: avoiding unused entries in file list --- appl/rebackgui/mainwindow.cpp | 32 ++++++++-- gui/ReGuiQueue.cpp | 109 ++++++++++++++++++++-------------- gui/ReGuiQueue.hpp | 19 +++++- 3 files changed, 108 insertions(+), 52 deletions(-) diff --git a/appl/rebackgui/mainwindow.cpp b/appl/rebackgui/mainwindow.cpp index e15d172..8489f9c 100644 --- a/appl/rebackgui/mainwindow.cpp +++ b/appl/rebackgui/mainwindow.cpp @@ -115,15 +115,32 @@ void MainWindow::onGuiTimerUpdate() clock_t start = clock(); clock_t diff; while(count-- > 0 && (diff = clock() - start) < CLOCKS_PER_SEC * 7 / 8){ + int itemCount; m_mutexGuiQueue.lock(); ReGuiQueueItem item = m_guiQueue.popFront(); + itemCount = 1 + m_guiQueue.countOfType(item.m_type); m_mutexGuiQueue.unlock(); - if (item.m_type == ReGuiQueueItem::ListEnd - && (list = reinterpret_cast(item.m_widget))->count() >= m_maxListSize) - delete list->takeItem(0); + if (item.m_type == ReGuiQueueItem::ListEnd){ + if (itemCount <= m_maxListSize){ + if ( (list = reinterpret_cast(item.m_widget))->count() >= m_maxListSize) + delete list->takeItem(0); + item.apply(m_guiQueue, m_maxListSize); + item.m_type = ReGuiQueueItem::Undef; + } + } if (item.m_type == ReGuiQueueItem::Undef) break; - if (! item.apply()){ + int maxItems = 0; + switch(item.m_type){ + case ReGuiQueueItem::ListEnd: + case ReGuiQueueItem::ListAppendToCurrent: + maxItems = m_maxListSize * 2; + break; + default: + break; + } + if (item.m_type != ReGuiQueueItem::Undef + && ! item.apply(m_guiQueue, maxItems)){ switch (item.m_type){ case ReGuiQueueItem::ListEnd: ui->listWidgetLog->addItem(item.m_value); @@ -153,6 +170,13 @@ void MainWindow::onGuiTimerUpdate() } } } + ReGuiQueueItem& item = m_guiQueue.lastOfType(ReGuiQueueItem::ListEnd); + if (item.m_type == ReGuiQueueItem::ListEnd){ + if ( (list = reinterpret_cast(item.m_widget))->count() >= m_maxListSize) + delete list->takeItem(0); + item.apply(m_guiQueue, 0); + item.m_type = ReGuiQueueItem::Undef; + } if (!statusMessage.isEmpty()) setStatusMessage(LOG_INFO, statusMessage); diff --git a/gui/ReGuiQueue.cpp b/gui/ReGuiQueue.cpp index 2a1a345..a9de75e 100644 --- a/gui/ReGuiQueue.cpp +++ b/gui/ReGuiQueue.cpp @@ -16,9 +16,12 @@ * Constructor. */ ReGuiQueue::ReGuiQueue() : - QList(), - m_locker() + QList(), + m_locker(), + m_countOfType(), + m_lastOfType() { + memset(&m_countOfType, 0, sizeof m_countOfType); } /** @@ -32,6 +35,7 @@ void ReGuiQueue::pushBack(const ReGuiQueueItem& item) { m_locker.lock(); append(item); + ++m_countOfType[item.m_type]; m_locker.unlock(); } @@ -59,6 +63,8 @@ ReGuiQueueItem ReGuiQueue::popFront() { m_locker.lock(); ReGuiQueueItem rc = takeFirst(); + m_lastOfType[rc.m_type] = rc; + --m_countOfType[rc.m_type]; m_locker.unlock(); return rc; } @@ -68,57 +74,68 @@ ReGuiQueueItem ReGuiQueue::popFront() * * This method should only used by the master thread. * + * @param + * @param maxItems 0 or the maximal count of relevant items: items will be + * inserted only if the count of items in the queue + * is lower than this limit. This is a widget type specific + * value * @return true: the info could be put into the widget
* false: nothing is done */ -bool ReGuiQueueItem::apply() const +bool ReGuiQueueItem::apply(ReGuiQueue &queue, int maxItems) const { bool rc = m_widget != NULL; if (rc){ - switch(m_type){ - case LabelText: - reinterpret_cast(m_widget)->setText(m_value); - break; - case NewTableRow: - { - QChar separator = m_value.at(0); - QStringList list = m_value.mid(1).split(separator); - QTableWidget* table = reinterpret_cast(m_widget); - int rowCount = table->rowCount(); - table->setRowCount(rowCount + 1); - int cols = min(list.size(), table->columnCount()); - for (int ix = 0; ix < cols; ix++){ - table->setItem(rowCount, ix, new QTableWidgetItem(list.at(ix))); + if (maxItems > 0 && queue.m_countOfType[m_type] > maxItems){ + // nothing to do! + } else { + switch(m_type){ + case LabelText: + reinterpret_cast(m_widget)->setText(m_value); + break; + case NewTableRow: + { + QChar separator = m_value.at(0); + QStringList list = m_value.mid(1).split(separator); + QTableWidget* table = reinterpret_cast(m_widget); + int rowCount = table->rowCount(); + table->setRowCount(rowCount + 1); + int cols = min(list.size(), table->columnCount()); + for (int ix = 0; ix < cols; ix++){ + table->setItem(rowCount, ix, new QTableWidgetItem(list.at(ix))); + } + break; + } + case ListEnd: + { + QListWidget* list = reinterpret_cast(m_widget); + list->addItem(m_value); + int count = list->count(); + list->setCurrentRow(count - 1); + break; + } + case ListAppendToCurrent: + { + QListWidget* list = reinterpret_cast(m_widget); + int count = list->count(); + if (count > 0){ + QListWidgetItem* item = list->item(count - 1); + item->setText(item->text() + " " + m_value); + } + break; + } + case ListReplaceCurrent: + { + QListWidget* list = reinterpret_cast(m_widget); + int count = list->count(); + QListWidgetItem* item = list->item(count - 1); + item->setText(m_value); + break; + } + default: + rc = false; + break; } - break; - } - case ListEnd: - { - QListWidget* list = reinterpret_cast(m_widget); - list->addItem(m_value); - int count = list->count(); - list->setCurrentRow(count - 1); - break; - } - case ListAppendToCurrent: - { - QListWidget* list = reinterpret_cast(m_widget); - int count = list->count(); - QListWidgetItem* item = list->item(count - 1); - item->setText(item->text() + " " + m_value); - break; - } - case ListReplaceCurrent: - { - QListWidget* list = reinterpret_cast(m_widget); - int count = list->count(); - QListWidgetItem* item = list->item(count - 1); - item->setText(m_value); - break; - } - default: - rc = false; - break; } } return rc; diff --git a/gui/ReGuiQueue.hpp b/gui/ReGuiQueue.hpp index 33613d7..4af30aa 100644 --- a/gui/ReGuiQueue.hpp +++ b/gui/ReGuiQueue.hpp @@ -12,13 +12,16 @@ #ifndef REGUIQUEUE_HPP #define REGUIQUEUE_HPP +class ReGuiQueue; class ReGuiQueueItem { public: enum WidgetType { Undef, LabelText, NewTableRow, ListEnd, ListAppendToCurrent, ListReplaceCurrent, LogMessage, LogError, ReadyMessage, StatusLine, - UserDefined1, UserDefined2 + UserDefined1, UserDefined2, + // must be the last! + TypeCount }; public: @@ -59,7 +62,7 @@ public: return *this; } public: - bool apply() const; + bool apply(ReGuiQueue &queue, int maxItems = 0) const; public: WidgetType m_type; QWidget* m_widget; @@ -80,8 +83,20 @@ public: int count() const; ReGuiQueueItem popFront(); void pushBack(const ReGuiQueueItem& item); + inline int countOfType(ReGuiQueueItem::WidgetType type) const { + return m_countOfType[type]; + } + inline ReGuiQueueItem& lastOfType(ReGuiQueueItem::WidgetType type){ + return m_lastOfType[type]; + } + protected: QMutex m_locker; + friend class ReGuiQueueItem; + // count of items for each widget type: + int m_countOfType[ReGuiQueueItem::TypeCount]; + // the last item of each widget type which is removed from the queue: + ReGuiQueueItem m_lastOfType[ReGuiQueueItem::TypeCount]; }; #endif // REGUIQUEUE_HPP -- 2.39.5