From c7bf1da797b49a62aa9f1d64b3ea0c71fa485b85 Mon Sep 17 00:00:00 2001 From: William Casarin Date: Thu, 6 Apr 2023 10:26:51 -0700 Subject: [PATCH] perf: cache lnurl to avoid bech32 encoding on the main thread --- damus/Nostr/Nostr.swift | 18 +++++++++++++----- damus/Util/AccountDeletion.swift | 2 +- 2 files changed, 14 insertions(+), 6 deletions(-) diff --git a/damus/Nostr/Nostr.swift b/damus/Nostr/Nostr.swift index 5c7c457e..eacb695d 100644 --- a/damus/Nostr/Nostr.swift +++ b/damus/Nostr/Nostr.swift @@ -7,7 +7,7 @@ import Foundation -struct Profile: Codable { +class Profile: Codable { var value: [String: AnyCodable] init (name: String?, display_name: String?, about: String?, picture: String?, banner: String?, website: String?, lud06: String?, lud16: String?, nip05: String?) { @@ -39,7 +39,7 @@ struct Profile: Codable { return s } - private mutating func set_val(_ key: String, _ val: T?) { + private func set_val(_ key: String, _ val: T?) { if val == nil { self.value.removeValue(forKey: key) return @@ -48,7 +48,7 @@ struct Profile: Codable { self.value[key] = AnyCodable.init(val) } - private mutating func set_str(_ key: String, _ val: String?) { + private func set_str(_ key: String, _ val: String?) { set_val(key, val) } @@ -110,13 +110,21 @@ struct Profile: Codable { } } + private var _lnurl: String? = nil var lnurl: String? { + if let _lnurl { + return _lnurl + } + guard let addr = lud16 ?? lud06 else { return nil; } if addr.contains("@") { - return lnaddress_to_lnurl(addr); + // this is a heavy op and is used a lot in views, cache it! + let addr = lnaddress_to_lnurl(addr); + self._lnurl = addr + return addr } if !addr.lowercased().hasPrefix("lnurl") { @@ -139,7 +147,7 @@ struct Profile: Codable { self.value = [:] } - init(from decoder: Decoder) throws { + required init(from decoder: Decoder) throws { let container = try decoder.singleValueContainer() self.value = try container.decode([String: AnyCodable].self) } diff --git a/damus/Util/AccountDeletion.swift b/damus/Util/AccountDeletion.swift index d0c72f79..6fe24105 100644 --- a/damus/Util/AccountDeletion.swift +++ b/damus/Util/AccountDeletion.swift @@ -9,7 +9,7 @@ import Foundation func created_deleted_account_profile(keypair: FullKeypair) -> NostrEvent { - var profile = Profile() + let profile = Profile() profile.deleted = true profile.about = "account deleted" profile.name = "nobody"