From 50a4d2ba0aef3d89f2442cf4a5a6c91447fe484d Mon Sep 17 00:00:00 2001 From: Valere Date: Fri, 29 Apr 2022 10:54:09 +0200 Subject: [PATCH] olm try to decrypt with recent session first --- changelog.d/5872.misc | 1 + .../org/matrix/android/sdk/internal/crypto/OlmSessionStore.kt | 3 ++- .../android/sdk/internal/crypto/store/db/RealmCryptoStore.kt | 1 + 3 files changed, 4 insertions(+), 1 deletion(-) create mode 100644 changelog.d/5872.misc diff --git a/changelog.d/5872.misc b/changelog.d/5872.misc new file mode 100644 index 0000000000..1e15229800 --- /dev/null +++ b/changelog.d/5872.misc @@ -0,0 +1 @@ +Faster Olm decrypt when there is a lot of existing sessions diff --git a/matrix-sdk-android/src/main/java/org/matrix/android/sdk/internal/crypto/OlmSessionStore.kt b/matrix-sdk-android/src/main/java/org/matrix/android/sdk/internal/crypto/OlmSessionStore.kt index f4fbca6a0f..aac6f67aea 100644 --- a/matrix-sdk-android/src/main/java/org/matrix/android/sdk/internal/crypto/OlmSessionStore.kt +++ b/matrix-sdk-android/src/main/java/org/matrix/android/sdk/internal/crypto/OlmSessionStore.kt @@ -66,7 +66,8 @@ internal class OlmSessionStore @Inject constructor(private val store: IMXCryptoS olmSessions.getOrPut(deviceKey) { mutableListOf() }.forEach { cached -> getSafeSessionIdentifier(cached.olmSession)?.let { cachedSessionId -> if (!persistedKnownSessions.contains(cachedSessionId)) { - persistedKnownSessions.add(cachedSessionId) + // as it's in cache put in on top + persistedKnownSessions.add(0, cachedSessionId) } } } diff --git a/matrix-sdk-android/src/main/java/org/matrix/android/sdk/internal/crypto/store/db/RealmCryptoStore.kt b/matrix-sdk-android/src/main/java/org/matrix/android/sdk/internal/crypto/store/db/RealmCryptoStore.kt index 99adbbfbae..fad901f95d 100644 --- a/matrix-sdk-android/src/main/java/org/matrix/android/sdk/internal/crypto/store/db/RealmCryptoStore.kt +++ b/matrix-sdk-android/src/main/java/org/matrix/android/sdk/internal/crypto/store/db/RealmCryptoStore.kt @@ -715,6 +715,7 @@ internal class RealmCryptoStore @Inject constructor( return doWithRealm(realmConfiguration) { it.where() .equalTo(OlmSessionEntityFields.DEVICE_KEY, deviceKey) + .sort(OlmSessionEntityFields.LAST_RECEIVED_MESSAGE_TS, Sort.DESCENDING) .findAll() .mapNotNull { sessionEntity -> sessionEntity.sessionId