mirror of
https://github.com/permissionlesstech/bitchat.git
synced 2026-07-26 07:25:18 +00:00
Optimization 3: Add caching for expensive view computations
- Cache getRSSIColor results with key format: "\(rssi)_\(isDark)" - Cache getEncryptionStatus results with peerID as key - Add cache invalidation methods for both caches - Invalidate encryption cache when: - Encryption status is updated - Fingerprints are verified - Peer authentication callbacks trigger - Invalidate RSSI cache when peer list updates - Clear all caches in panicClearAllData This reduces redundant calculations during view updates and improves performance
This commit is contained in:
@@ -747,6 +747,10 @@ class ChatViewModel: ObservableObject {
|
|||||||
// Clear read receipt tracking
|
// Clear read receipt tracking
|
||||||
sentReadReceipts.removeAll()
|
sentReadReceipts.removeAll()
|
||||||
|
|
||||||
|
// Clear all caches
|
||||||
|
invalidateEncryptionCache()
|
||||||
|
invalidateRSSIColorCache()
|
||||||
|
|
||||||
// Disconnect from all peers and clear persistent identity
|
// Disconnect from all peers and clear persistent identity
|
||||||
// This will force creation of a new identity (new fingerprint) on next launch
|
// This will force creation of a new identity (new fingerprint) on next launch
|
||||||
meshService.emergencyDisconnectAll()
|
meshService.emergencyDisconnectAll()
|
||||||
@@ -1212,6 +1216,9 @@ class ChatViewModel: ObservableObject {
|
|||||||
peerEncryptionStatus[peerID] = Optional.none
|
peerEncryptionStatus[peerID] = Optional.none
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Invalidate cache when encryption status changes
|
||||||
|
invalidateEncryptionCache(for: peerID)
|
||||||
|
|
||||||
// Force UI update
|
// Force UI update
|
||||||
DispatchQueue.main.async { [weak self] in
|
DispatchQueue.main.async { [weak self] in
|
||||||
self?.objectWillChange.send()
|
self?.objectWillChange.send()
|
||||||
@@ -1299,6 +1306,9 @@ class ChatViewModel: ObservableObject {
|
|||||||
peerEncryptionStatus[peerID] = Optional.none
|
peerEncryptionStatus[peerID] = Optional.none
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Invalidate cache when encryption status changes
|
||||||
|
invalidateEncryptionCache(for: peerID)
|
||||||
|
|
||||||
// Trigger UI update
|
// Trigger UI update
|
||||||
DispatchQueue.main.async { [weak self] in
|
DispatchQueue.main.async { [weak self] in
|
||||||
self?.objectWillChange.send()
|
self?.objectWillChange.send()
|
||||||
@@ -1418,6 +1428,9 @@ class ChatViewModel: ObservableObject {
|
|||||||
SecureLogger.log("ChatViewModel: Setting encryption status to noiseSecured for \(peerID)", category: SecureLogger.security, level: .info)
|
SecureLogger.log("ChatViewModel: Setting encryption status to noiseSecured for \(peerID)", category: SecureLogger.security, level: .info)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Invalidate cache when encryption status changes
|
||||||
|
self.invalidateEncryptionCache(for: peerID)
|
||||||
|
|
||||||
// Force UI update
|
// Force UI update
|
||||||
self.objectWillChange.send()
|
self.objectWillChange.send()
|
||||||
}
|
}
|
||||||
@@ -1426,7 +1439,14 @@ class ChatViewModel: ObservableObject {
|
|||||||
// Set up handshake required callback
|
// Set up handshake required callback
|
||||||
noiseService.onHandshakeRequired = { [weak self] peerID in
|
noiseService.onHandshakeRequired = { [weak self] peerID in
|
||||||
DispatchQueue.main.async {
|
DispatchQueue.main.async {
|
||||||
self?.peerEncryptionStatus[peerID] = .noiseHandshaking
|
guard let self = self else { return }
|
||||||
|
self.peerEncryptionStatus[peerID] = .noiseHandshaking
|
||||||
|
|
||||||
|
// Invalidate cache when encryption status changes
|
||||||
|
self.invalidateEncryptionCache(for: peerID)
|
||||||
|
|
||||||
|
// Force UI update
|
||||||
|
self.objectWillChange.send()
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -2169,6 +2189,9 @@ extension ChatViewModel: BitchatDelegate {
|
|||||||
// Update encryption status for all peers
|
// Update encryption status for all peers
|
||||||
self.updateEncryptionStatusForPeers()
|
self.updateEncryptionStatusForPeers()
|
||||||
|
|
||||||
|
// Invalidate RSSI cache since peer data may have changed
|
||||||
|
self.invalidateRSSIColorCache()
|
||||||
|
|
||||||
// Explicitly notify SwiftUI that the object has changed.
|
// Explicitly notify SwiftUI that the object has changed.
|
||||||
self.objectWillChange.send()
|
self.objectWillChange.send()
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user