From df832f4b418154a24cb0676d612cc15f72bb56b1 Mon Sep 17 00:00:00 2001 From: Brent Simmons Date: Mon, 4 Sep 2023 14:14:51 -0700 Subject: [PATCH] Override existingFeed(withURL url: String) from Container protocol for performance reasons. Use a dictionary to lookup a feed more quickly than by looping through the feeds. --- Account/Sources/Account/Account.swift | 18 ++++++++++++++++-- 1 file changed, 16 insertions(+), 2 deletions(-) diff --git a/Account/Sources/Account/Account.swift b/Account/Sources/Account/Account.swift index 5f3f91d62..8e9377346 100644 --- a/Account/Sources/Account/Account.swift +++ b/Account/Sources/Account/Account.swift @@ -174,7 +174,15 @@ public enum FetchType { } return _externalIDToFeedDictionary } - + + private var _urlToFeedDictionary = [String: Feed]() + var urlToFeedDictionary: [String: Feed] { + if feedDictionariesNeedUpdate { + rebuildFeedDictionaries() + } + return _urlToFeedDictionary + } + var flattenedFeedURLs: Set { return Set(flattenedFeeds().map({ $0.url })) } @@ -1341,16 +1349,19 @@ private extension Account { func rebuildFeedDictionaries() { var idDictionary = [String: Feed]() var externalIDDictionary = [String: Feed]() + var urlToFeedDictionary = [String: Feed]() for feed in flattenedFeeds() { idDictionary[feed.feedID] = feed if let externalID = feed.externalID { externalIDDictionary[externalID] = feed } + urlToFeedDictionary[feed.url] = feed } _idToFeedDictionary = idDictionary _externalIDToFeedDictionary = externalIDDictionary + _urlToFeedDictionary = urlToFeedDictionary feedDictionariesNeedUpdate = false } @@ -1510,7 +1521,10 @@ extension Account { public func existingFeed(withExternalID externalID: String) -> Feed? { return externalIDToFeedDictionary[externalID] } - + + public func existingFeed(withURL url: String) -> Feed? { + urlToFeedDictionary[url] + } } // MARK: - OPMLRepresentable