Skip to content

Commit

Permalink
Fix backup performance
Browse files Browse the repository at this point in the history
  • Loading branch information
Anderas committed Nov 22, 2022
1 parent 5131019 commit cdc4f81
Show file tree
Hide file tree
Showing 5 changed files with 41 additions and 12 deletions.
35 changes: 30 additions & 5 deletions MatrixSDK/Crypto/CryptoMachine/MXCryptoMachine.swift
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,12 @@ class MXCryptoMachine {
private let syncQueue = MXTaskQueue()
private var roomQueues = RoomQueues()

// Temporary properties to help with the performance of backup keys checks
// until the performance is improved in the rust-sdk
private var cachedRoomKeyCounts: RoomKeyCounts?
private var isComputingRoomKeyCounts = false
private let processingQueue = DispatchQueue(label: "org.matrix.sdk.MXCryptoMachine.processingQueue")

private let log = MXNamedLog(name: "MXCryptoMachine")

init(userId: String, deviceId: String, restClient: MXRestClient, getRoomAction: @escaping GetRoomAction) throws {
Expand Down Expand Up @@ -700,12 +706,14 @@ extension MXCryptoMachine: MXCryptoBackup {
}

var roomKeyCounts: RoomKeyCounts? {
do {
return try machine.roomKeyCounts()
} catch {
log.error("Cannot get room key counts", context: error)
return nil
// Checking the number of backed-up keys is currently very compute-heavy
// and blocks the main thread for large accounts. A light-weight `hasKeysToBackup`
// method will be added into rust-sdk and for the time-being we return cached counts
// on the main thread and compute new value on separate queue
processingQueue.async { [weak self] in
self?.updateRoomKeyCountsIfNeeded()
}
return cachedRoomKeyCounts
}

func enableBackup(key: MegolmV1BackupKey, version: String) throws {
Expand Down Expand Up @@ -777,6 +785,23 @@ extension MXCryptoMachine: MXCryptoBackup {
}
return try machine.importRoomKeys(keys: string, passphrase: passphrase, progressListener: progressListener)
}

// MARK: - Private

private func updateRoomKeyCountsIfNeeded() {
guard !isComputingRoomKeyCounts else {
return
}

isComputingRoomKeyCounts = true
do {
cachedRoomKeyCounts = try machine.roomKeyCounts()
} catch {
log.error("Cannot get room key counts", context: error)
cachedRoomKeyCounts = nil
}
isComputingRoomKeyCounts = false
}
}

extension MXCryptoMachine: Logger {
Expand Down
6 changes: 6 additions & 0 deletions MatrixSDK/Data/MXRoom.h
Original file line number Diff line number Diff line change
Expand Up @@ -176,12 +176,18 @@ FOUNDATION_EXPORT NSInteger const kMXRoomInvalidInviteSenderErrorCode;

/**
Indicate if the room is tagged as a direct chat.
@warning: This is an O(n) computed property that iterates through all
direct rooms to determine whether any particular room is direct.
*/
@property (nonatomic, readonly) BOOL isDirect;

/**
The user identifier for whom this room is tagged as direct (if any).
nil if the room is not a direct chat.
@warning: This is an O(n) computed property that iterates through all
direct rooms to determine given direct user id.
*/
@property (nonatomic, readonly) NSString *directUserId;

Expand Down
2 changes: 2 additions & 0 deletions MatrixSDK/Data/MXRoom.m
Original file line number Diff line number Diff line change
Expand Up @@ -3537,12 +3537,14 @@ - (void)setReadMarker:(NSString*)eventId withReadReceipt:(NSString*)receiptEvent
- (BOOL)isDirect
{
// Check whether this room is tagged as direct for one of the room members.
// This is an O(n) operation.
return (self.directUserId != nil);
}

- (NSString *)directUserId
{
// Get the information from the user account data that is managed by MXSession
// This is an O(n) operation.
return [self.mxSession directUserIdInRoom:_roomId];
}

Expand Down
9 changes: 2 additions & 7 deletions MatrixSDK/Space/MXSpaceService.swift
Original file line number Diff line number Diff line change
Expand Up @@ -498,13 +498,8 @@ public class MXSpaceService: NSObject {
let startDate = Date()
MXLog.debug("[MXSpaceService] buildGraph: started")

var directRoomIds = Set<String>()
let roomIds: [String] = self.session.rooms.compactMap { room in
if room.isDirect {
directRoomIds.insert(room.roomId)
}
return room.roomId
}
var directRoomIds = Set(session.directRooms?.flatMap(\.value) ?? [])
let roomIds = session.rooms.compactMap(\.roomId)

let output = PrepareDataResult()
MXLog.debug("[MXSpaceService] buildGraph: preparing data for \(roomIds.count) rooms")
Expand Down
1 change: 1 addition & 0 deletions changelog.d/pr-1641.change
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
CryptoV2: Fix backup performance

0 comments on commit cdc4f81

Please sign in to comment.