Performance: invoke UpdateTrustWorker only once per incremental sync.

This commit is contained in:
Benoit Marty 2022-08-22 17:44:07 +02:00 committed by Benoit Marty
parent 9b57630eae
commit 13f7a9fc10
4 changed files with 31 additions and 10 deletions

View file

@ -63,6 +63,7 @@ import org.matrix.android.sdk.internal.session.room.accountdata.RoomAccountDataD
import org.matrix.android.sdk.internal.session.room.membership.RoomDisplayNameResolver
import org.matrix.android.sdk.internal.session.room.membership.RoomMemberHelper
import org.matrix.android.sdk.internal.session.room.relationship.RoomChildRelationInfo
import org.matrix.android.sdk.internal.session.sync.SyncResponsePostTreatmentAggregator
import timber.log.Timber
import javax.inject.Inject
import kotlin.system.measureTimeMillis
@ -91,7 +92,8 @@ internal class RoomSummaryUpdater @Inject constructor(
roomSummary: RoomSyncSummary? = null,
unreadNotifications: RoomSyncUnreadNotifications? = null,
updateMembers: Boolean = false,
inviterId: String? = null
inviterId: String? = null,
aggregator: SyncResponsePostTreatmentAggregator? = null
) {
val roomSummaryEntity = RoomSummaryEntity.getOrCreate(realm, roomId)
if (roomSummary != null) {
@ -180,8 +182,14 @@ internal class RoomSummaryUpdater @Inject constructor(
roomSummaryEntity.otherMemberIds.clear()
roomSummaryEntity.otherMemberIds.addAll(otherRoomMembers)
if (roomSummaryEntity.isEncrypted && otherRoomMembers.isNotEmpty()) {
if (aggregator == null) {
// Do it now
// mmm maybe we could only refresh shield instead of checking trust also?
crossSigningService.onUsersDeviceUpdate(otherRoomMembers)
crossSigningService.onUsersDeviceUpdate(otherRoomMembers) // This is very long and could maybe be done once per sync response.
} else {
// Schedule it
aggregator.userIdsWithDeviceUpdate.addAll(otherRoomMembers)
}
}
}
}

View file

@ -25,4 +25,7 @@ internal class SyncResponsePostTreatmentAggregator {
// List of userIds to fetch and update at the end of incremental syncs
val userIdsToFetch = mutableListOf<String>()
// Set of users to call `crossSigningService.onUsersDeviceUpdate` once per sync
val userIdsWithDeviceUpdate = mutableSetOf<String>()
}

View file

@ -20,6 +20,7 @@ import com.zhuinden.monarchy.Monarchy
import org.matrix.android.sdk.api.MatrixPatterns
import org.matrix.android.sdk.api.extensions.tryOrNull
import org.matrix.android.sdk.api.session.user.model.User
import org.matrix.android.sdk.internal.crypto.crosssigning.DefaultCrossSigningService
import org.matrix.android.sdk.internal.di.SessionDatabase
import org.matrix.android.sdk.internal.session.profile.GetProfileInfoTask
import org.matrix.android.sdk.internal.session.sync.RoomSyncEphemeralTemporaryStore
@ -36,12 +37,14 @@ internal class SyncResponsePostTreatmentAggregatorHandler @Inject constructor(
private val ephemeralTemporaryStore: RoomSyncEphemeralTemporaryStore,
private val updateUserAccountDataTask: UpdateUserAccountDataTask,
private val getProfileInfoTask: GetProfileInfoTask,
private val crossSigningService: DefaultCrossSigningService,
@SessionDatabase private val monarchy: Monarchy,
) {
suspend fun handle(aggregator: SyncResponsePostTreatmentAggregator) {
cleanupEphemeralFiles(aggregator.ephemeralFilesToDelete)
updateDirectUserIds(aggregator.directChatsToCheck)
fetchAndUpdateUsers(aggregator.userIdsToFetch)
handleUserIdsWithDeviceUpdate(aggregator.userIdsWithDeviceUpdate)
}
private fun cleanupEphemeralFiles(ephemeralFilesToDelete: List<String>) {
@ -92,6 +95,10 @@ internal class SyncResponsePostTreatmentAggregatorHandler @Inject constructor(
}
}
private fun handleUserIdsWithDeviceUpdate(userIdsWithDeviceUpdate: Iterable<String>) {
crossSigningService.onUsersDeviceUpdate(userIdsWithDeviceUpdate.toList())
}
private suspend fun List<User>.saveLocally() {
val userEntities = map { user -> UserEntityFactory.create(user) }
monarchy.awaitTransaction {

View file

@ -154,12 +154,12 @@ internal class RoomSyncHandler @Inject constructor(
}
is HandlingStrategy.INVITED ->
handlingStrategy.data.mapWithProgress(reporter, InitialSyncStep.ImportingAccountInvitedRooms, 0.1f) {
handleInvitedRoom(realm, it.key, it.value, insertType, syncLocalTimeStampMillis)
handleInvitedRoom(realm, it.key, it.value, insertType, syncLocalTimeStampMillis, aggregator)
}
is HandlingStrategy.LEFT -> {
handlingStrategy.data.mapWithProgress(reporter, InitialSyncStep.ImportingAccountLeftRooms, 0.3f) {
handleLeftRoom(realm, it.key, it.value, insertType, syncLocalTimeStampMillis)
handleLeftRoom(realm, it.key, it.value, insertType, syncLocalTimeStampMillis, aggregator)
}
}
}
@ -285,7 +285,8 @@ internal class RoomSyncHandler @Inject constructor(
Membership.JOIN,
roomSync.summary,
roomSync.unreadNotifications,
updateMembers = hasRoomMember
updateMembers = hasRoomMember,
aggregator = aggregator
)
return roomEntity
}
@ -295,7 +296,8 @@ internal class RoomSyncHandler @Inject constructor(
roomId: String,
roomSync: InvitedRoomSync,
insertType: EventInsertType,
syncLocalTimestampMillis: Long
syncLocalTimestampMillis: Long,
aggregator: SyncResponsePostTreatmentAggregator
): RoomEntity {
Timber.v("Handle invited sync for room $roomId")
val isInitialSync = insertType == EventInsertType.INITIAL_SYNC
@ -319,7 +321,7 @@ internal class RoomSyncHandler @Inject constructor(
it.type == EventType.STATE_ROOM_MEMBER
}
roomChangeMembershipStateDataSource.setMembershipFromSync(roomId, Membership.INVITE)
roomSummaryUpdater.update(realm, roomId, Membership.INVITE, updateMembers = true, inviterId = inviterEvent?.senderId)
roomSummaryUpdater.update(realm, roomId, Membership.INVITE, updateMembers = true, inviterId = inviterEvent?.senderId, aggregator = aggregator)
return roomEntity
}
@ -328,7 +330,8 @@ internal class RoomSyncHandler @Inject constructor(
roomId: String,
roomSync: RoomSync,
insertType: EventInsertType,
syncLocalTimestampMillis: Long
syncLocalTimestampMillis: Long,
aggregator: SyncResponsePostTreatmentAggregator
): RoomEntity {
val isInitialSync = insertType == EventInsertType.INITIAL_SYNC
val roomEntity = RoomEntity.getOrCreate(realm, roomId)
@ -366,7 +369,7 @@ internal class RoomSyncHandler @Inject constructor(
roomEntity.chunks.clearWith { it.deleteOnCascade(deleteStateEvents = true, canDeleteRoot = true) }
roomTypingUsersHandler.handle(realm, roomId, null)
roomChangeMembershipStateDataSource.setMembershipFromSync(roomId, Membership.LEAVE)
roomSummaryUpdater.update(realm, roomId, membership, roomSync.summary, roomSync.unreadNotifications)
roomSummaryUpdater.update(realm, roomId, membership, roomSync.summary, roomSync.unreadNotifications, aggregator = aggregator)
return roomEntity
}