Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Cache inbound group sessions when decrypting #1566

Merged
merged 5 commits into from
Aug 31, 2022
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
64 changes: 60 additions & 4 deletions MatrixSDK/Crypto/MXOlmDevice.m
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,8 @@
#import "MXKeyProvider.h"
#import "MXRawDataKey.h"

NSInteger const kMXInboundGroupSessionCacheSize = 100;

@interface MXOlmDevice () <OLMKitPickleKeyDelegate>
{
// The OLMKit utility instance.
Expand All @@ -53,8 +55,10 @@ @interface MXOlmDevice () <OLMKitPickleKeyDelegate>
// The store where crypto data is saved.
@property (nonatomic, readonly) id<MXCryptoStore> store;

@end
// Cache to avoid refetching unchanged sessions from the crypto store
@property (nonatomic, strong) MXLRUCache *inboundGroupSessionCache;

@end

@implementation MXOlmDevice
@synthesize store;
Expand Down Expand Up @@ -98,6 +102,8 @@ - (instancetype)initWithStore:(id<MXCryptoStore>)theStore

_deviceCurve25519Key = olmAccount.identityKeys[@"curve25519"];
_deviceEd25519Key = olmAccount.identityKeys[@"ed25519"];

_inboundGroupSessionCache = [[MXLRUCache alloc] initWithCapacity:kMXInboundGroupSessionCacheSize];
}
return self;
}
Expand Down Expand Up @@ -394,7 +400,7 @@ - (BOOL)addInboundGroupSession:(NSString*)sessionId
session.sharedHistory = sharedHistory;
}

[store storeInboundGroupSessions:@[session]];
[self storeInboundGroupSessions:@[session]];

return YES;
}
Expand Down Expand Up @@ -441,7 +447,7 @@ - (BOOL)addInboundGroupSession:(NSString*)sessionId
[sessions addObject:session];
}

[store storeInboundGroupSessions:sessions];
[self storeInboundGroupSessions:sessions];

return sessions;
}
Expand All @@ -462,7 +468,7 @@ - (MXDecryptionResult *)decryptGroupMessage:(NSString *)body

MXDecryptionResult *result;

[store performSessionOperationWithGroupSessionWithId:sessionId senderKey:senderKey block:^(MXOlmInboundGroupSession *session) {
[self performGroupSessionOperationWithSessionId:sessionId senderKey:senderKey block:^(MXOlmInboundGroupSession *session) {

*error = [self checkInboundGroupSession:session roomId:roomId];
if (*error)
Expand Down Expand Up @@ -521,6 +527,41 @@ - (MXDecryptionResult *)decryptGroupMessage:(NSString *)body
return result;
}

- (void)performGroupSessionOperationWithSessionId:(NSString*)sessionId senderKey:(NSString*)senderKey block:(void (^)(MXOlmInboundGroupSession *inboundGroupSession))block
{
// Based on a feature flag megolm decryption will either fetch a group session from the store on every decryption,
// or (if the flag is enabled) it will use LRU cache to avoid refetching unchanged sessions.
//
// Additionally the duration of each variant is tracked in analytics (if configured and enabled by the user)
// to allow performance comparison
//
// LRU cache variant will eventually become the default implementation if proved stable.

BOOL enableCache = MXSDKOptions.sharedInstance.enableGroupSessionCache;
NSString *operation = enableCache ? @"megolm.decrypt.cache" : @"megolm.decrypt.store";
StopDurationTracking stopTracking = [MXSDKOptions.sharedInstance.analyticsDelegate startDurationTrackingForName:@"MXOlmDevice" operation:operation];

if (enableCache)
{
__block MXOlmInboundGroupSession *session;
@synchronized (self.inboundGroupSessionCache)
{
session = (MXOlmInboundGroupSession *)[self.inboundGroupSessionCache get:sessionId];
if (!session)
{
session = [store inboundGroupSessionWithId:sessionId andSenderKey:senderKey];
[self.inboundGroupSessionCache put:sessionId object:session];
manuroe marked this conversation as resolved.
Show resolved Hide resolved
}
}
block(session);
}
else
{
[store performSessionOperationWithGroupSessionWithId:sessionId senderKey:senderKey block:block];
}
stopTracking();
}

- (void)resetReplayAttackCheckInTimeline:(NSString*)timeline
{
[inboundGroupSessionMessageIndexes removeObjectForKey:timeline];
Expand Down Expand Up @@ -622,6 +663,21 @@ - (NSDictionary*)getInboundGroupSessionKey:(NSString*)roomId senderKey:(NSString
return inboundGroupSessionKey;
}

- (void)storeInboundGroupSessions:(NSArray <MXOlmInboundGroupSession *>*)sessions
{
[store storeInboundGroupSessions:sessions];
if (MXSDKOptions.sharedInstance.enableGroupSessionCache)
{
@synchronized (self.inboundGroupSessionCache)
{
for (MXOlmInboundGroupSession *session in sessions)
{
[self.inboundGroupSessionCache put:session.session.sessionIdentifier object:session];
}
}
}
}


#pragma mark - OLMKitPickleKeyDelegate

Expand Down
8 changes: 8 additions & 0 deletions MatrixSDK/MXSDKOptions.h
Original file line number Diff line number Diff line change
Expand Up @@ -214,6 +214,14 @@ NS_ASSUME_NONNULL_BEGIN
*/
@property (nonatomic) BOOL enableCryptoV2;

/**
Enable performance optimization where inbound group sessions are cached between decryption of events
rather than fetched from the store every time.

@remark The value is set randomly between YES / NO to perform a very basic A/B test
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
@remark The value is set randomly between YES / NO to perform a very basic A/B test
@remark By default, the value is set randomly between YES / NO to perform a very basic A/B test

*/
@property (nonatomic) BOOL enableGroupSessionCache;

#endif

@end
Expand Down
4 changes: 4 additions & 0 deletions MatrixSDK/MXSDKOptions.m
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,10 @@ - (instancetype)init
#if DEBUG
_enableCryptoV2 = NO;
#endif

// The value is set randomly between YES / NO to perform a very basic A/B test
// measured by `analytics` (if set and enabled)
_enableGroupSessionCache = arc4random_uniform(2) == 1;
}

return self;
Expand Down
11 changes: 11 additions & 0 deletions MatrixSDK/MXSession.m
Original file line number Diff line number Diff line change
Expand Up @@ -1406,6 +1406,7 @@ - (void)serverSyncWithServerTimeout:(NSUInteger)serverTimeout
dispatch_group_t initialSyncDispatchGroup = dispatch_group_create();

__block MXTaskProfile *syncTaskProfile;
__block StopDurationTracking stopDurationTracking;
__block MXSyncResponse *syncResponse;
__block BOOL useLiveResponse = YES;

Expand Down Expand Up @@ -1437,6 +1438,13 @@ - (void)serverSyncWithServerTimeout:(NSUInteger)serverTimeout
BOOL isInitialSync = !self.isEventStreamInitialised;
MXTaskProfileName taskName = isInitialSync ? MXTaskProfileNameStartupInitialSync : MXTaskProfileNameStartupIncrementalSync;
syncTaskProfile = [MXSDKOptions.sharedInstance.profiler startMeasuringTaskWithName:taskName];
if (isInitialSync) {
// Temporarily tracking performance both by `MXSDKOptions.sharedInstance.profiler` (manually measuring time)
// and `MXSDKOptions.sharedInstance.analyticsDelegate` (delegating to performance monitoring tool).
// This ambiguity will be resolved in the future
NSString *operation = MXSDKOptions.sharedInstance.enableGroupSessionCache ? @"initialSync.enableGroupSessionCache" : @"initialSync.diableGroupSessionCache";
stopDurationTracking = [MXSDKOptions.sharedInstance.analyticsDelegate startDurationTrackingForName:@"MXSession" operation:operation];
}
}

NSString * streamToken = self.store.eventStreamToken;
Expand Down Expand Up @@ -1479,6 +1487,9 @@ - (void)serverSyncWithServerTimeout:(NSUInteger)serverTimeout
syncTaskProfile.units = syncResponse.rooms.join.count;

[MXSDKOptions.sharedInstance.profiler stopMeasuringTaskWithProfile:syncTaskProfile];
if (stopDurationTracking) {
stopDurationTracking();
}
}

BOOL isInitialSync = !self.isEventStreamInitialised;
Expand Down
21 changes: 21 additions & 0 deletions MatrixSDK/Utils/MXAnalyticsDelegate.h
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,12 @@
#import "MXCallHangupEventContent.h"
#import "MXTaskProfile.h"

/**
Callback function to cancel ongoing duration tracking
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
Callback function to cancel ongoing duration tracking
Callback function to finish ongoing duration tracking

started by `[MXAnalyticsDelegate startDurationTracking]`
*/
typedef void (^StopDurationTracking)(void);

NS_ASSUME_NONNULL_BEGIN

/**
Expand All @@ -46,6 +52,21 @@ NS_ASSUME_NONNULL_BEGIN
*/
- (void)trackDuration:(NSInteger)milliseconds name:(MXTaskProfileName)name units:(NSUInteger)units;

/**
Start tracking the duration of a task and manually cancel when finished using the return handle
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
Start tracking the duration of a task and manually cancel when finished using the return handle
Start tracking the duration of a task and manually stop when finished using the return handle


@note This method is similar to `trackDuration`, but instead of passing the measured duraction
as a parameter, it relies on the implementation of `MXAnalyticsDelegate` to perform the
measurements.

@param name Name of the entity being measured (e.g. `RoomsViewController` or `Crypto`)
@param operation Short code identifying the type of operation measured (e.g. `viewDidLoad` or `decrypt`)


@return Handle that can be used to stop the performance tracking
*/
- (StopDurationTracking)startDurationTrackingForName:(NSString *)name operation:(NSString *)operation;

/**
Report that a call has started.

Expand Down
1 change: 1 addition & 0 deletions changelog.d/pr-1566.change
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Crypto: Cache inbound group sessions when decrypting