[KPhotoAlbum] Caching last date/time string on loading

Robert Krawitz rlk at alum.mit.edu
Sat Oct 3 17:36:51 BST 2020


I'm getting maybe 8% improvement on startup by caching the most recent date/time string when loading
image, and returning the same date rather than re-parsing it.  It's taking about 8.1 seconds vs. 8.8
to start up now.

This is going to vary a lot depending upon the composition of someone's database.  If someone has a
database with a lot of older images without EXIF data where someone might have manually assigned a
fixed date, or uses continuous bursts frequently with a fast camera, or shoots in RAW+JPEG mode, the
benefit could be considerable (as in my case).  If not, there will be a small overhead (I'd expect
it to be very small, since it's just one uncontended mutex and one string comparison).  I have a
total of 386253 image files with 172370 unique dates, so I'm saving more than half the conversions
by doing this.


diff --git a/XMLDB/Database.cpp b/XMLDB/Database.cpp
index dc7c1577..2a0ddca7 100644
--- a/XMLDB/Database.cpp
+++ b/XMLDB/Database.cpp
@@ -40,6 +40,7 @@
 #include <QElapsedTimer>
 #include <QExplicitlySharedDataPointer>
 #include <QFileInfo>
+#include <QMutexLocker>

 using Utilities::StringSet;

@@ -605,15 +606,22 @@ int XMLDB::Database::fileVersion()
 // During profiling of loading, I found that a significant amount of time was spent in
Utilities::FastDateTime::fromString.
 // Reviewing the code, I fount that it did a lot of extra checks we don't need (like checking if
the string have
 // timezone information (which they won't in KPA), this function is a replacement that is faster
than the original.
-Utilities::FastDateTime dateTimeFromString(const QString &str)
-{
-    static QChar T = QChar::fromLatin1('T');
-
-    if (str[10] == T)
-        return Utilities::FastDateTime(QDate::fromString(str.left(10), Qt::ISODate),
QTime::fromString(str.mid(11), Qt::ISODate));
-
-    else
-        return Utilities::FastDateTime::fromString(str, Qt::ISODate);
+static Utilities::FastDateTime dateTimeFromString(const QString& str) {
+    // Caching the last used date/time string will help for photographers
+    // who frequently take bursts.
+    static QString s_lastDateTimeString;
+    static Utilities::FastDateTime s_lastDateTime;
+    static QMutex s_lastDateTimeLocker;
+    QMutexLocker dummy(&s_lastDateTimeLocker);
+    static const QChar T = QChar::fromLatin1('T');
+    if (str != s_lastDateTimeString) {
+        if ( str[10] == T)
+            s_lastDateTime = QDateTime(QDate::fromString(str.left(10),
Qt::ISODate),QTime::fromString(str.mid(11),Qt::ISODate));
+        else
+            s_lastDateTime = QDateTime::fromString(str,Qt::ISODate);
+        s_lastDateTimeString = str;
+    }
+    return s_lastDateTime;
 }

 DB::ImageInfoPtr XMLDB::Database::createImageInfo(const DB::FileName &fileName, ReaderPtr reader,
Database *db, const QMap<QString, QString> *newToOldCategory)



More information about the Kphotoalbum mailing list