quotient-im / libQuotient

A Qt library to write cross-platform clients for Matrix
https://quotient-im.github.io/libQuotient/
GNU Lesser General Public License v2.1
132 stars 56 forks source link

Room State Cache saved too often #771

Open uriesk opened 2 months ago

uriesk commented 2 months ago

The room state cache gets saved to disk on almost all Room::change events. That state can be multiple MB and is freshly written on every new message. Is this necessary or is it possible to limit this?

uriesk commented 2 months ago

JFYI i am in a room with hundreds of users and storing the cache takes >20ms producing a 4MB file.

diff --git a/Quotient/connection.cpp b/Quotient/connection.cpp
index 1474ddc3..78befbef 100644
--- a/Quotient/connection.cpp
+++ b/Quotient/connection.cpp
@@ -1422,6 +1422,9 @@ void Connection::saveRoomState(Room* r) const
     if (!d->cacheState)
         return;

+    QElapsedTimer et;
+    et.start();
+
     QFile outRoomFile { stateCacheDir().filePath(
         SyncData::fileNameForRoom(r->id())) };
     if (outRoomFile.open(QFile::WriteOnly)) {
@@ -1435,6 +1438,8 @@ void Connection::saveRoomState(Room* r) const
         qCWarning(MAIN) << "Error opening" << outRoomFile.fileName() << ":"
                         << outRoomFile.errorString();
     }
+
+    qCDebug(PROFILER) << "Room state cache for" << r->id() << "generated in" << et;
 }

 void Connection::saveState() const
KitsuneRal commented 2 months ago

This was made as low-maintenance way to store the room state back before we had to have a database. I think saving the most recent state is valuable to make sure it doesn't get lost in case of crashes but if we save it to the database instead of a CBOR file we could at least save it in a more fine-grained manner, into a file that is already open anyway.