[mythtv] PATCH: Intelligent shuffle locks mythmusic for a long time

Joseph A. Caputo jcaputo1 at comcast.net
Sun Mar 9 23:35:41 EST 2003


I finally figured out that mythmusic wasn't crashing when I toggled the
shuffle mode.  It's just that when it cycled to "Intelligent" shuffle
mode, it was taking 5 minutes to recompute the playlist.  I made a few
simple changes to make the playlist traversal more efficient; now it
takes ~1 second :-)

- Extracted "weight" computation into its own function (yes, it
introduces a little function call overhead, but the cost is negligible
and you never know when you might need to use it elsewhere)
- Changed playlistorder and playlistweight to use QValueVector instead
of QValueList.  QValueList is not optimized for random access --
internally it's a linked list, so random access costs O(n).  This was
really killing us when sorting or looping through a large playlist.
QValueVector uses an internal array, so random access is done in
constant time.

If it looks okay Isaac, I would suggest this be committed 0.8.

-Joe C.




-------------- next part --------------
? mythmusic.diff
Index: mythmusic/playbackbox.cpp
===================================================================
RCS file: /var/lib/cvs/mythmusic/mythmusic/playbackbox.cpp,v
retrieving revision 1.29
diff -u -d -r1.29 playbackbox.cpp
--- mythmusic/playbackbox.cpp	5 Mar 2003 21:57:06 -0000	1.29
+++ mythmusic/playbackbox.cpp	10 Mar 2003 04:22:27 -0000
@@ -511,6 +511,28 @@
     }
 }
 
+double PlaybackBox::computeIntelligentWeight(Metadata & mdata, double currentDateTime)
+{
+        int rating;
+        int playcount;
+        double lastplay;
+        double ratingValue = 0;
+        double playcountValue = 0;
+        double lastplayValue = 0;
+
+	rating = mdata.Rating();
+	playcount = mdata.PlayCount();
+	lastplay = mdata.LastPlay();
+	ratingValue = (double)rating / 10;
+	playcountValue = (double)playcount / 50;
+	lastplayValue = (currentDateTime - lastplay) / 
+	                 currentDateTime * 2000;
+	return (35 * ratingValue - 25 * playcountValue + 
+		25 * lastplayValue + 
+		15 * (double)rand() / (RAND_MAX + 1.0));
+}
+
+
 void PlaybackBox::setupPlaylist(void)
 {
     if (playlistorder.size() > 0)
@@ -529,6 +551,10 @@
         return;
     }
 
+    // Reserve QValueVector memory up front
+    // to decrease memory allocation overhead
+    playlistorder.reserve(plist->size());
+
     if (shufflemode == 0)
     {
         for (int i = 0; i < (int)plist->size(); i++)
@@ -578,36 +604,17 @@
 
         int i, j, temp;
         double tempd;
-        int rating;
-        int playcount;
-        double lastplay;
         QDateTime cTime = QDateTime::currentDateTime();
         double currentDateTime = cTime.toString("yyyyMMddhhmmss").toDouble();
-        double ratingValue = 0;
-        double playcountValue = 0;
-        double lastplayValue = 0;
         double weight;
 
-        QValueList<double> playlistweight;
-
-        for (i = 0; i < max; i++)
-            playlistweight.push_back(0);
+        QValueVector<double> playlistweight;
+	playlistweight.reserve(max);
 
         for (i = 0; i < max; i++)
         {
-            curMeta = (*plist)[i];
-            rating = curMeta.Rating();
-            playcount = curMeta.PlayCount();
-            lastplay = curMeta.LastPlay();
-            ratingValue = (double)rating / 10;
-            playcountValue = (double)playcount / 50;
-            lastplayValue = (currentDateTime - lastplay) / 
-                            currentDateTime * 2000;
-            weight = (35 * ratingValue - 25 * playcountValue + 
-                      25 * lastplayValue + 
-                      15 * (double)rand() / (RAND_MAX + 1.0));
-
-            playlistweight[i] = weight;
+	    weight = computeIntelligentWeight((*plist)[i], currentDateTime);
+            playlistweight.push_back(weight);
             playlistorder.push_back(i);
         }
 
Index: mythmusic/playbackbox.h
===================================================================
RCS file: /var/lib/cvs/mythmusic/mythmusic/playbackbox.h,v
retrieving revision 1.17
diff -u -d -r1.17 playbackbox.h
--- mythmusic/playbackbox.h	4 Mar 2003 17:39:38 -0000	1.17
+++ mythmusic/playbackbox.h	10 Mar 2003 04:22:28 -0000
@@ -8,6 +8,7 @@
 #include <qtoolbutton.h>
 #include <qtimer.h>
 #include <qmutex.h>
+#include <qvaluevector.h>
 
 #include <mythtv/mythwidgets.h>
 
@@ -70,7 +71,7 @@
 
   private:
     void setupListView(void);
-
+    double computeIntelligentWeight(Metadata &mdata, double currentDateTime);
     void setupPlaylist(void);
 
     QPixmap scalePixmap(const char **xpmdata);
@@ -89,7 +90,7 @@
     QSqlDatabase *db;
 
     QValueList<Metadata> *plist;
-    QValueList<int> playlistorder;
+    QValueVector<int> playlistorder;
     QMutex listlock;
 
     int playlistindex;


More information about the mythtv-dev mailing list