From 8f62dd1776acfc4f3c1b680994ce263a74cb5e09 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 | 15 ++++++++++++++- 1 file changed, 14 insertions(+), 1 deletion(-) diff --git a/bitchat/ViewModels/ChatViewModel.swift b/bitchat/ViewModels/ChatViewModel.swift index 8af4e045..e153232c 100644 --- a/bitchat/ViewModels/ChatViewModel.swift +++ b/bitchat/ViewModels/ChatViewModel.swift @@ -363,6 +363,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 @@ -3763,7 +3764,19 @@ final class ChatViewModel: ObservableObject, BitchatDelegate { if let spid = message.senderPeerID?.id { // In geohash channels, compare against our per-geohash nostr short ID if case .location(let ch) = activeChannel, spid.hasPrefix("nostr:") { - if let myGeo = try? idBridge.deriveIdentity(forGeohash: ch.geohash) { + // Use cached identity to avoid crypto during rendering + let myGeo: NostrIdentity? = { + if let cached = cachedGeohashIdentity, cached.geohash == ch.geohash { + return cached.identity + } + // Fallback: derive and cache (should rarely happen) + if let identity = try? NostrIdentityBridge.deriveIdentity(forGeohash: ch.geohash) { + cachedGeohashIdentity = (ch.geohash, identity) + return identity + } + return nil + }() + if let myGeo = myGeo { return spid == "nostr:\(myGeo.publicKeyHex.prefix(TransportConfig.nostrShortKeyDisplayLength))" } }