From 46caded0999ec84a39f9c56fa28343cee38037df Mon Sep 17 00:00:00 2001 From: jack Date: Tue, 30 Sep 2025 14:46:48 +0200 Subject: [PATCH] Cache geohash identity in ChatViewModel to prevent crypto during rendering Additional optimization for location channels (voice notes are mesh-only, but this helps with text message rendering in geohash channels): - Add cachedGeohashIdentity to avoid deriveIdentity calls during rendering - Check cache before falling back to crypto derivation - Reduces main thread crypto work in location channels --- bitchat/ViewModels/ChatViewModel.swift | 13 ++++++++++++- 1 file changed, 12 insertions(+), 1 deletion(-) diff --git a/bitchat/ViewModels/ChatViewModel.swift b/bitchat/ViewModels/ChatViewModel.swift index 8f710d28..8a7fce64 100644 --- a/bitchat/ViewModels/ChatViewModel.swift +++ b/bitchat/ViewModels/ChatViewModel.swift @@ -358,6 +358,7 @@ final class ChatViewModel: ObservableObject, BitchatDelegate { private var geoSubscriptionID: String? = nil private var geoDmSubscriptionID: String? = nil private var currentGeohash: String? = nil + private var cachedGeohashIdentity: (geohash: String, identity: NostrIdentity)? = nil // Cache current geohash identity private var geoNicknames: [String: String] = [:] // pubkeyHex(lowercased) -> nickname // Show Tor status once per app launch private var torStatusAnnounced = false @@ -3741,7 +3742,17 @@ final class ChatViewModel: ObservableObject, BitchatDelegate { if let spid = message.senderPeerID { // In geohash channels, compare against our per-geohash nostr short ID if case .location(let ch) = activeChannel, spid.isGeoChat { - if let myGeo = try? idBridge.deriveIdentity(forGeohash: ch.geohash) { + let myGeo: NostrIdentity? = { + if let cached = cachedGeohashIdentity, cached.geohash == ch.geohash { + return cached.identity + } + if let identity = try? idBridge.deriveIdentity(forGeohash: ch.geohash) { + cachedGeohashIdentity = (ch.geohash, identity) + return identity + } + return nil + }() + if let myGeo { return spid == PeerID(nostr: myGeo.publicKeyHex) } }