Optimisation: Add a cache SyncJournalDb::mapChecksumType
authorOlivier Goffart <ogoffart@woboq.com>
Tue, 6 Nov 2018 09:45:22 +0000 (10:45 +0100)
committerKevin Ottens <kevin.ottens@nextcloud.com>
Tue, 15 Dec 2020 09:58:19 +0000 (10:58 +0100)
No need to do two sql query for something that's always the same and
there are very few checksum types

src/common/syncjournaldb.cpp
src/common/syncjournaldb.h

index 3234b00673bcdb51bb05bc375235856d11b280c4..2d900f8ae11fb06088bcf2fddf8b032380b944de 100644 (file)
@@ -1901,6 +1901,10 @@ int SyncJournalDb::mapChecksumType(const QByteArray &checksumType)
         return 0;
     }
 
+    auto it =  _checksymTypeCache.find(checksumType);
+    if (it != _checksymTypeCache.end())
+        return *it;
+
     // Ensure the checksum type is in the db
     if (!_insertChecksumTypeQuery.initOrReset(QByteArrayLiteral("INSERT OR IGNORE INTO checksumtype (name) VALUES (?1)"), _db))
         return 0;
@@ -1921,7 +1925,9 @@ int SyncJournalDb::mapChecksumType(const QByteArray &checksumType)
         qCWarning(lcDb) << "No checksum type mapping found for" << checksumType;
         return 0;
     }
-    return _getChecksumTypeIdQuery.intValue(0);
+    auto value = _getChecksumTypeIdQuery.intValue(0);
+    _checksymTypeCache[checksumType] = value;
+    return value;
 }
 
 QByteArray SyncJournalDb::dataFingerprint()
index 12d2871cad4c34a87e15fb9596810b5d42701fcc..01e66734b6a723cf5a0b7a4d630965ea06b6680d 100644 (file)
@@ -272,6 +272,7 @@ private:
     SqlDatabase _db;
     QString _dbFile;
     QMutex _mutex; // Public functions are protected with the mutex.
+    QMap<QByteArray, int> _checksymTypeCache;
     int _transaction;
     bool _metadataTableIsEmpty;