From 210474f6acf7642fbc6f5c0dcab665acc2d9d8a4 Mon Sep 17 00:00:00 2001
From: Brent Simmons
Date: Sat, 31 Oct 2026 11:56:03 -0700
Subject: [PATCH] Parse, save, post HTML thumbnail URLs in batches of 100.
---
.../Images/ArticleThumbnailFinder.swift | 25 +++++++++++--------
.../ArticleThumbnailFinderTests.swift | 25 +++++++++++++++++++
2 files changed, 39 insertions(+), 11 deletions(-)
diff --git a/Modules/Images/Sources/Images/ArticleThumbnailFinder.swift b/Modules/Images/Sources/Images/ArticleThumbnailFinder.swift
index 650778953..7f91d9522 100644
--- a/Modules/Images/Sources/Images/ArticleThumbnailFinder.swift
+++ b/Modules/Images/Sources/Images/ArticleThumbnailFinder.swift
@@ -87,6 +87,8 @@ extension Notification.Name {
#endif
/// HTML images saved per article, so one that fails or is too small falls through to the next.
nonisolated private static let maxHTMLThumbnailURLsPerArticle = 3
+ /// Articles parsed and saved at a time, which also limits how long a save holds the database queue.
+ static let parsingBatchSize = 100
/// Entries in htmlThumbnailURLs, a few hundred bytes each. Past this, it’s emptied, and the next discovery reloads its articles from the database.
private static let maxCachedArticleCount = 50_000
/// Entries per mode in chosenThumbnailURLs. Past this, that mode’s entries are emptied.
@@ -155,8 +157,9 @@ extension Notification.Name {
/// Looks for thumbnails in the HTML of articles that have no usable provided image.
/// Results saved in the database are ready when this returns, so that rows laid out next can use them.
/// This blocks until the database has answered.
- /// The rest are parsed off the main thread and saved, and `.articleThumbnailURLsDidBecomeAvailable` is posted
- /// for those that have thumbnails. The returned task finishes after that.
+ /// The rest are parsed off the main thread and saved in batches, in the order given, so the first rows don’t wait for the last.
+ /// `.articleThumbnailURLsDidBecomeAvailable` is posted after each batch for those that have thumbnails.
+ /// The returned task finishes after the last batch.
@discardableResult public func findThumbnailURLs(for articles: some Collection) -> Task {
if htmlThumbnailURLs.count > Self.maxCachedArticleCount {
htmlThumbnailURLs.removeAll()
@@ -175,16 +178,16 @@ extension Notification.Name {
defer {
articleIDsBeingDiscovered.subtract(articleIDs)
}
- guard !articleHTMLs.isEmpty else {
- return
- }
- let parsedRecords = await Self.parseThumbnailURLs(articleHTMLs)
- cacheRecords(parsedRecords)
- imageDatabase.saveArticleThumbnails(parsedRecords)
+ for batchStart in stride(from: 0, to: articleHTMLs.count, by: Self.parsingBatchSize) {
+ let batchEnd = min(batchStart + Self.parsingBatchSize, articleHTMLs.count)
+ let parsedRecords = await Self.parseThumbnailURLs(Array(articleHTMLs[batchStart..
"#) }
+ let recorder = ThumbnailURLNotificationRecorder(finder: finder)
+
+ await finder.findThumbnailURLs(for: articles).value
+
+ #expect(recorder.articleIDCounts == [ArticleThumbnailFinder.parsingBatchSize, 1])
+ }
+
@Test func thumbnailTooSmallIsRejected() async throws {
let smallImageData = try #require(jpegData(width: 48, height: 48))
@@ -179,6 +189,21 @@ private typealias ArticleAttachment = Articles.Attachment
// MARK: - Helpers
+@MainActor private final class ThumbnailURLNotificationRecorder {
+
+ private(set) var articleIDCounts = [Int]()
+
+ init(finder: ArticleThumbnailFinder) {
+ NotificationCenter.default.addObserver(self, selector: #selector(handleArticleThumbnailURLsDidBecomeAvailable(_:)), name: .articleThumbnailURLsDidBecomeAvailable, object: finder)
+ }
+
+ @objc func handleArticleThumbnailURLsDidBecomeAvailable(_ notification: Notification) {
+ if let articleIDs = notification.userInfo?[ArticleThumbnailFinder.articleIDsKey] as? Set {
+ articleIDCounts.append(articleIDs.count)
+ }
+ }
+}
+
private func makeArticle(articleID: String = "1", contentHTML: String? = nil, imageURL: String? = nil, thumbnailURL: String? = nil, attachments: [ArticleAttachment]? = nil) -> Article {
let status = ArticleStatus(articleID: articleID, read: false, starred: false, dateArrived: Date())
return Article(accountID: "account", articleID: articleID, feedID: "feed", uniqueID: articleID, title: nil, contentHTML: contentHTML, contentText: nil, markdown: nil, url: "https://example.com/posts/\(articleID)", externalURL: nil, summary: nil, imageURL: imageURL, thumbnailURL: thumbnailURL, datePublished: nil, dateModified: nil, authors: nil, attachments: attachments, status: status)