]> gitweb.hamatoma.de Git - reqt/commitdiff
optimization: avoiding unused entries in file list
authorhama <hama@siduction.net>
Wed, 13 Apr 2016 22:03:26 +0000 (00:03 +0200)
committerhama <hama@siduction.net>
Wed, 13 Apr 2016 22:03:26 +0000 (00:03 +0200)
appl/rebackgui/mainwindow.cpp
gui/ReGuiQueue.cpp
gui/ReGuiQueue.hpp

index e15d172ca2f7d3483ac0efd92fabee68ab1418d4..8489f9ce46fb8cadc77451d656dc9fd43e3f3c96 100644 (file)
@@ -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<QListWidget*>(item.m_widget))->count() >= m_maxListSize)
-                       delete list->takeItem(0);
+               if (item.m_type == ReGuiQueueItem::ListEnd){
+                       if (itemCount <= m_maxListSize){
+                               if ( (list = reinterpret_cast<QListWidget*>(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<QListWidget*>(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);
 
index 2a1a345855fca20b1cd5fbdd0966a766217cd0fb..a9de75e32795f821a66c19701514b54a61719cc4 100644 (file)
  * Constructor.
  */
 ReGuiQueue::ReGuiQueue() :
-    QList<ReGuiQueueItem>(),
-       m_locker()
+       QList<ReGuiQueueItem>(),
+       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     <code>true</code>: the info could be put into the widget<br>
  *                     <code>false</code>: 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<QLabel*>(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<QTableWidget*>(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<QLabel*>(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<QTableWidget*>(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<QListWidget*>(m_widget);
+                               list->addItem(m_value);
+                               int count = list->count();
+                               list->setCurrentRow(count - 1);
+                               break;
+                       }
+                       case ListAppendToCurrent:
+                       {
+                               QListWidget* list = reinterpret_cast<QListWidget*>(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<QListWidget*>(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<QListWidget*>(m_widget);
-                       list->addItem(m_value);
-            int count = list->count();
-            list->setCurrentRow(count - 1);
-                       break;
-               }
-               case ListAppendToCurrent:
-               {
-                       QListWidget* list = reinterpret_cast<QListWidget*>(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<QListWidget*>(m_widget);
-                       int count = list->count();
-                       QListWidgetItem* item = list->item(count - 1);
-                       item->setText(m_value);
-                       break;
-               }
-               default:
-                       rc = false;
-                       break;
                }
        }
        return rc;
index 33613d7da6160b8b2ff3d891c169053831f0feb5..4af30aad6942836ea52c9f7e7fde713c89cf79af 100644 (file)
 #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