Fix a performance bug when fetching all unread articles. When validating unread counts, it was looping through the entire set of articles once per feed. Now it loops through articles just once, for a major performance boost with lots of feeds and/or lots of unread articles.

This commit is contained in:
Brent Simmons 2018-09-10 22:08:38 -07:00
parent 2c2c005798
commit 25ff76e1e5
1 changed files with 16 additions and 1 deletions

View File

@ -338,7 +338,22 @@ public final class Account: DisplayNameProvider, UnreadCountProvider, Container,
let feeds = container.flattenedFeeds()
let articles = database.fetchUnreadArticles(for: feeds.feedIDs())
feeds.forEach { validateUnreadCount($0, articles) }
// Validate unread counts. This was the site of a performance slowdown:
// it was calling going through the entire list of articles once per feed:
// feeds.forEach { validateUnreadCount($0, articles) }
// Now we loop through articles exactly once. This makes a huge difference.
var unreadCountStorage = [String: Int]() // [FeedID: Int]
articles.forEach { (article) in
precondition(!article.status.read)
unreadCountStorage[article.feedID, default: 0] += 1
}
feeds.forEach { (feed) in
let unreadCount = unreadCountStorage[feed.feedID, default: 0]
feed.unreadCount = unreadCount
}
return articles
}