From 904bb36d7f55bab90179a27a8b51cb0a9b6bff7d Mon Sep 17 00:00:00 2001 From: Jordan Rose Date: Wed, 19 Jan 2022 17:08:17 -0800 Subject: [PATCH] Turn group data caches back on in the NSE Without the GroupV2Params caches, processing multiple large group updates is extremely wasteful, decrypting the same profile keys and UUIDs over and over again. The size of these caches has also been *increased* to match the group size limit, or else there's no benefit for large groups. Other LRUCache instances are still off by default in the NSE by virtue of the "NSE max" defaulting to 0. --- SignalMessaging/groups/GroupV2Params.swift | 10 ++++++++-- SignalServiceKit/src/Util/LRUCache.swift | 15 +++------------ 2 files changed, 11 insertions(+), 14 deletions(-) diff --git a/SignalMessaging/groups/GroupV2Params.swift b/SignalMessaging/groups/GroupV2Params.swift index b631eab8ce..400834ddc8 100644 --- a/SignalMessaging/groups/GroupV2Params.swift +++ b/SignalMessaging/groups/GroupV2Params.swift @@ -91,7 +91,12 @@ public extension GroupV2Params { return try uuid(forUuidCiphertext: uuidCiphertext) } - private static let decryptedUuidCache = LRUCache(maxSize: 256) + private static var maxGroupSize: Int { + return Int(RemoteConfig.groupsV2MaxGroupSizeHardLimit) + } + + private static let decryptedUuidCache = LRUCache(maxSize: Self.maxGroupSize, + nseMaxSize: Self.maxGroupSize) func uuid(forUuidCiphertext uuidCiphertext: UuidCiphertext) throws -> UUID { let cacheKey = (uuidCiphertext.serialize().asData + groupSecretParamsData) @@ -117,7 +122,8 @@ public extension GroupV2Params { return userId } - private static let decryptedProfileKeyCache = LRUCache(maxSize: 256) + private static let decryptedProfileKeyCache = LRUCache(maxSize: Self.maxGroupSize, + nseMaxSize: Self.maxGroupSize) func profileKey(forProfileKeyCiphertext profileKeyCiphertext: ProfileKeyCiphertext, uuid: UUID) throws -> Data { diff --git a/SignalServiceKit/src/Util/LRUCache.swift b/SignalServiceKit/src/Util/LRUCache.swift index 307a585e1b..c7e2313554 100644 --- a/SignalServiceKit/src/Util/LRUCache.swift +++ b/SignalServiceKit/src/Util/LRUCache.swift @@ -1,5 +1,5 @@ // -// Copyright (c) 2021 Open Whisper Systems. All rights reserved. +// Copyright (c) 2022 Open Whisper Systems. All rights reserved. // // An @objc wrapper around LRUCache. @@ -64,7 +64,6 @@ public class AnyLRUCache: NSObject { public class LRUCache { private let cache = NSCache() - private let maxSize: Int private let _resetCount = AtomicUInt(0) public var resetCount: UInt { _resetCount.get() @@ -73,15 +72,7 @@ public class LRUCache { public init(maxSize: Int, nseMaxSize: Int = 0, shouldEvacuateInBackground: Bool = false) { - self.maxSize = { - if CurrentAppContext().isNSE { - let disableCacheInNSE = true - return disableCacheInNSE ? 0 : nseMaxSize - } else { - return maxSize - } - }() - self.cache.countLimit = maxSize + self.cache.countLimit = CurrentAppContext().isNSE ? nseMaxSize : maxSize if CurrentAppContext().isMainApp, shouldEvacuateInBackground { @@ -116,7 +107,7 @@ public class LRUCache { remove(key: key) return } - guard maxSize > 0 else { + guard cache.countLimit > 0 else { Logger.verbose("Using disabled cache.") return }