perf: move heavy work off main actor, throttle progress updates, add signposts
BackupEngine: - fetchAssets() runs in Task.detached — prevents 100-500ms UI freeze during PHFetchResult enumeration on large libraries - Manifest JSON decode + ManifestIndex Set construction run in Task.detached - pendingAssets filter runs in Task.detached — O(N) array filter off main thread - Progress updates throttled to 8 Hz via ProgressThrottle — eliminates the continuous SwiftUI full-tree redraws caused by per-byte upload callbacks - bytesSoFar captured by value in upload closure — avoids mutable var capture - reserveCapacity on manifestEntries array - os_signpost intervals on FetchAssets, NASConnect, ManifestLoad, BuildPendingQueue, UploadLoop, UploadFile for Instruments profiling BackupStatusService: - countSafe() marked nonisolated — runs in Task.detached off main actor, keeping touch/animation responsive during PHFetchResult batch enumeration - ManifestIndex JSON decode + Set construction run in Task.detached - Bootstrap ManifestIndex(nasListing:) construction off main actor - writeManifest JSON decode/encode/merge off main actor - os_signpost intervals on Reconcile, ManifestLoad, CountSafe Model types: - ManifestIndex, ManifestEntry, PhotoAsset, BackupFilter marked Sendable — safe to pass across task boundaries without warnings Co-Authored-By: Kutesir <kutesir@provoc.ug> Co-Authored-By: Sentry <sentry@provoc.ug>
This commit is contained in:
@@ -4,6 +4,7 @@ import UIKit
|
||||
import os.log
|
||||
|
||||
private let log = Logger(subsystem: "com.albert.nasbackup", category: "BackupStatus")
|
||||
private let signposter = OSSignposter(subsystem: "com.albert.nasbackup", category: "BackupStatus")
|
||||
|
||||
@MainActor
|
||||
final class BackupStatusService: NSObject, ObservableObject {
|
||||
@@ -16,14 +17,13 @@ final class BackupStatusService: NSObject, ObservableObject {
|
||||
private let photoService = PhotoLibraryService()
|
||||
private let cacheKey = "backupStatusSnapshot_v2"
|
||||
|
||||
// One active reconciliation task at a time — cancelled before starting a new one
|
||||
// One active reconciliation task at a time — cancelled before starting a new one.
|
||||
private var refreshTask: Task<Void, Never>?
|
||||
// Separate debounce task for photo library change events
|
||||
private var debounceTask: Task<Void, Never>?
|
||||
|
||||
private var lastFullRefresh: Date?
|
||||
private static let debounceInterval: TimeInterval = 4 // seconds before photo-change refresh runs
|
||||
private static let minRefreshInterval: TimeInterval = 30 // minimum between full NAS reconciliations
|
||||
private static let debounceInterval: TimeInterval = 4
|
||||
private static let minRefreshInterval: TimeInterval = 30
|
||||
|
||||
// MARK: — Init
|
||||
|
||||
@@ -52,7 +52,6 @@ final class BackupStatusService: NSObject, ObservableObject {
|
||||
|
||||
// MARK: — Public API
|
||||
|
||||
/// Start a reconciliation. `force: true` bypasses the minimum refresh interval.
|
||||
func refresh(force: Bool = false) {
|
||||
cancelAll()
|
||||
refreshTask = Task { [weak self] in
|
||||
@@ -60,21 +59,17 @@ final class BackupStatusService: NSObject, ObservableObject {
|
||||
}
|
||||
}
|
||||
|
||||
/// Awaitable reconciliation — used by pull-to-refresh and post-failure resolution.
|
||||
func refreshAndWait(force: Bool = true) async {
|
||||
cancelAll()
|
||||
await performRefresh(force: force)
|
||||
}
|
||||
|
||||
/// Called by BackupEngine after a completed run with newly uploaded entries.
|
||||
/// Applies an optimistic count update, writes the manifest in background, then reconciles.
|
||||
func refreshAfterBackup(entries: [ManifestEntry], connection: NASConnection) {
|
||||
let uploaded = entries.count
|
||||
snapshot.alreadySafe = min(snapshot.phoneTotal, snapshot.alreadySafe + uploaded)
|
||||
snapshot.nasArchiveTotal += uploaded
|
||||
saveSnapshot()
|
||||
log.info("Post-backup optimistic update: +\(uploaded) safe")
|
||||
// Write manifest then force reconcile (entries captured only briefly)
|
||||
Task {
|
||||
await writeManifest(entries: entries, connection: connection)
|
||||
refresh(force: true)
|
||||
@@ -96,7 +91,6 @@ final class BackupStatusService: NSObject, ObservableObject {
|
||||
return
|
||||
}
|
||||
|
||||
// Minimum interval guard (skip NAS round-trip if recently refreshed)
|
||||
if !force, let last = lastFullRefresh,
|
||||
Date().timeIntervalSince(last) < Self.minRefreshInterval {
|
||||
log.debug("Refresh debounced — updating phone count only")
|
||||
@@ -128,10 +122,14 @@ final class BackupStatusService: NSObject, ObservableObject {
|
||||
// MARK: — Reconciliation
|
||||
|
||||
private func reconcile() async throws {
|
||||
let spReconcile = signposter.beginInterval("Reconcile")
|
||||
defer { signposter.endInterval("Reconcile", spReconcile) }
|
||||
|
||||
let store = ConnectionStore.shared
|
||||
guard let conn = store.savedConnection else { return }
|
||||
|
||||
// ── Step 1: Phone count — metadata only, no image data, no thumbnails ──
|
||||
// ── Step 1: Phone count ───────────────────────────────────────────
|
||||
// PHFetchResult.count is O(1) after the initial index build — fast on main actor.
|
||||
let filter = store.backupFilter
|
||||
let fetchResult = photoService.fetchResultForReconciliation(filter: filter)
|
||||
let phoneTotal = fetchResult.count
|
||||
@@ -139,7 +137,7 @@ final class BackupStatusService: NSObject, ObservableObject {
|
||||
|
||||
try Task.checkCancellation()
|
||||
|
||||
// ── Step 2: NAS connection ──
|
||||
// ── Step 2: NAS connection ────────────────────────────────────────
|
||||
let transfer: any NASTransferProtocol = conn.nasProtocol == .smb ? SMBService() : SFTPService()
|
||||
do {
|
||||
try await transfer.connect(
|
||||
@@ -147,7 +145,6 @@ final class BackupStatusService: NSObject, ObservableObject {
|
||||
username: conn.username, password: conn.password
|
||||
)
|
||||
} catch {
|
||||
// NAS offline — keep cached safe count, enforce invariant
|
||||
snapshot.phoneTotal = phoneTotal
|
||||
snapshot.alreadySafe = min(snapshot.alreadySafe, phoneTotal)
|
||||
snapshot.connectionState = .offline
|
||||
@@ -157,32 +154,48 @@ final class BackupStatusService: NSObject, ObservableObject {
|
||||
return
|
||||
}
|
||||
defer { transfer.disconnect() }
|
||||
|
||||
try Task.checkCancellation()
|
||||
|
||||
// ── Step 3: Load ManifestIndex — Data + BackupManifest released after this call ──
|
||||
// ── Step 3: Load ManifestIndex ────────────────────────────────────
|
||||
// Download suspends main actor (good). JSON decode + Set<String> construction
|
||||
// is CPU-bound O(N) — runs in a background task so touch/animation remain smooth.
|
||||
let spManifest = signposter.beginInterval("ManifestLoad")
|
||||
let manifestPath = "\(conn.remotePath)/\(BackupManifest.remoteFilename)"
|
||||
let index = await buildManifestIndex(transfer: transfer, path: manifestPath, conn: conn)
|
||||
signposter.endInterval("ManifestLoad", spManifest,
|
||||
"\(index.totalCount) entries, filename-only=\(index.isFilenameOnly)")
|
||||
log.info("Manifest index built — \(index.totalCount) NAS entries, filename-only=\(index.isFilenameOnly)")
|
||||
|
||||
try Task.checkCancellation()
|
||||
|
||||
// ── Step 4: Count safe assets — PHFetchResult enumerated in batches, no [PhotoAsset] kept ──
|
||||
let safe = try await countSafe(in: fetchResult, against: index)
|
||||
// ── Step 4: Count safe assets — off main actor ────────────────────
|
||||
// Batch enumeration of PHFetchResult is CPU-bound (up to N/150 batches).
|
||||
// Running it in a detached task keeps the main thread free for the UI
|
||||
// while the count is in progress. Task.yield() inside countSafe lets other
|
||||
// async work interleave between batches.
|
||||
let spCount = signposter.beginInterval("CountSafe")
|
||||
let safe = try await Task.detached(priority: .userInitiated) { [weak self, fetchResult, index] in
|
||||
guard let self else { throw CancellationError() }
|
||||
return try await self.countSafe(in: fetchResult, against: index)
|
||||
}.value
|
||||
signposter.endInterval("CountSafe", spCount, "\(safe)/\(phoneTotal) safe")
|
||||
log.info("Comparison complete — \(safe)/\(phoneTotal) already safe")
|
||||
|
||||
// ── Step 5: Commit — enforce invariant (alreadySafe ≤ phoneTotal) ──
|
||||
// ── Step 5: Commit ────────────────────────────────────────────────
|
||||
var updated = BackupStatusSnapshot()
|
||||
updated.phoneTotal = phoneTotal
|
||||
updated.alreadySafe = min(safe, phoneTotal)
|
||||
updated.phoneTotal = phoneTotal
|
||||
updated.alreadySafe = min(safe, phoneTotal)
|
||||
updated.nasArchiveTotal = index.totalCount
|
||||
updated.connectionState = .connected
|
||||
updated.lastCheckedAt = Date()
|
||||
updated.lastCheckedAt = Date()
|
||||
snapshot = updated
|
||||
saveSnapshot()
|
||||
}
|
||||
|
||||
/// Builds a ManifestIndex from NAS. Data and BackupManifest are scoped inside
|
||||
/// this function — they are released before the caller's comparison step begins.
|
||||
/// Builds a ManifestIndex from NAS.
|
||||
/// - The download suspends the main actor.
|
||||
/// - JSON decode and Set construction run in a background task.
|
||||
private func buildManifestIndex(
|
||||
transfer: any NASTransferProtocol,
|
||||
path: String,
|
||||
@@ -190,22 +203,29 @@ final class BackupStatusService: NSObject, ObservableObject {
|
||||
) async -> ManifestIndex {
|
||||
do {
|
||||
let data = try await transfer.downloadData(at: path)
|
||||
if let manifest = try? JSONDecoder().decode(BackupManifest.self, from: data) {
|
||||
// `data` and `manifest` released when this scope exits
|
||||
// Decode and build index off main actor — JSONDecoder is not cheap for large manifests.
|
||||
let index: ManifestIndex? = await Task.detached(priority: .utility) {
|
||||
guard let manifest = try? JSONDecoder().decode(BackupManifest.self, from: data)
|
||||
else { return nil }
|
||||
return ManifestIndex(manifest: manifest)
|
||||
}
|
||||
}.value
|
||||
if let index { return index }
|
||||
} catch {
|
||||
// File not found — fall through to directory listing bootstrap
|
||||
}
|
||||
// Bootstrap from directory listing
|
||||
|
||||
// Bootstrap: list NAS directory and build filename-only index in background.
|
||||
let items = (try? await transfer.listDirectory(at: conn.remotePath)) ?? []
|
||||
log.info("Manifest not found — bootstrapping from \(items.count) NAS items")
|
||||
return ManifestIndex(nasListing: items)
|
||||
return await Task.detached(priority: .utility) {
|
||||
ManifestIndex(nasListing: items)
|
||||
}.value
|
||||
}
|
||||
|
||||
/// Enumerates a PHFetchResult in batches using autoreleasepool.
|
||||
/// Never builds a full [PHAsset] or [PhotoAsset] array.
|
||||
private func countSafe(
|
||||
/// Marked nonisolated so it can be called from a background Task without
|
||||
/// requiring main-actor scheduling — it accesses no @MainActor state.
|
||||
private nonisolated func countSafe(
|
||||
in result: PHFetchResult<PHAsset>,
|
||||
against index: ManifestIndex,
|
||||
batchSize: Int = 150
|
||||
@@ -220,18 +240,14 @@ final class BackupStatusService: NSObject, ObservableObject {
|
||||
let batchEnd = min(processed + batchSize, total)
|
||||
|
||||
// autoreleasepool releases PHAsset objects and PHAssetResource arrays
|
||||
// created during this batch before moving to the next
|
||||
// created during this batch before moving to the next.
|
||||
let batchSafe: Int = autoreleasepool {
|
||||
var count = 0
|
||||
for i in processed..<batchEnd {
|
||||
let asset = result.object(at: i)
|
||||
|
||||
if index.matches(localIdentifier: asset.localIdentifier) {
|
||||
// Fast path — O(1) Set lookup, no resource inspection
|
||||
count += 1
|
||||
} else if index.isFilenameOnly {
|
||||
// Bootstrap path — manifest was built from NAS directory listing,
|
||||
// no localIdentifiers available, must look up filename
|
||||
let resources = PHAssetResource.assetResources(for: asset)
|
||||
if let name = resources.first?.originalFilename,
|
||||
index.matches(filename: name) {
|
||||
@@ -244,7 +260,7 @@ final class BackupStatusService: NSObject, ObservableObject {
|
||||
|
||||
safe += batchSafe
|
||||
processed = batchEnd
|
||||
await Task.yield() // yield between batches to keep main thread responsive
|
||||
await Task.yield()
|
||||
}
|
||||
|
||||
return safe
|
||||
@@ -253,7 +269,6 @@ final class BackupStatusService: NSObject, ObservableObject {
|
||||
// MARK: — Phone-only lightweight update (no NAS connection)
|
||||
|
||||
private func updatePhoneCountOnly() {
|
||||
// Returns PHFetchResult — no array allocation
|
||||
let result = photoService.fetchResultForReconciliation(
|
||||
filter: ConnectionStore.shared.backupFilter
|
||||
)
|
||||
@@ -279,23 +294,27 @@ final class BackupStatusService: NSObject, ObservableObject {
|
||||
|
||||
let manifestPath = "\(connection.remotePath)/\(BackupManifest.remoteFilename)"
|
||||
|
||||
// Load existing, merge new entries, write back
|
||||
var manifest: BackupManifest
|
||||
do {
|
||||
let data = try await transfer.downloadData(at: manifestPath)
|
||||
manifest = (try? JSONDecoder().decode(BackupManifest.self, from: data)) ?? BackupManifest()
|
||||
} catch {
|
||||
manifest = BackupManifest()
|
||||
}
|
||||
// Download existing manifest (async, main actor suspended).
|
||||
let existingData = try? await transfer.downloadData(at: manifestPath)
|
||||
|
||||
manifest.merge(entries: entries)
|
||||
// Merge + JSON encode off main actor — can be slow for large manifests.
|
||||
let encoded: Data? = await Task.detached(priority: .utility) {
|
||||
var manifest: BackupManifest
|
||||
if let data = existingData,
|
||||
let existing = try? JSONDecoder().decode(BackupManifest.self, from: data) {
|
||||
manifest = existing
|
||||
} else {
|
||||
manifest = BackupManifest()
|
||||
}
|
||||
manifest.merge(entries: entries)
|
||||
let encoder = JSONEncoder()
|
||||
encoder.dateEncodingStrategy = .iso8601
|
||||
return try? encoder.encode(manifest)
|
||||
}.value
|
||||
guard let encoded else { return }
|
||||
|
||||
let encoder = JSONEncoder()
|
||||
encoder.dateEncodingStrategy = .iso8601
|
||||
let data = try encoder.encode(manifest)
|
||||
// manifest released here before write
|
||||
try await transfer.writeData(data, to: manifestPath)
|
||||
log.info("Manifest written — \(manifest.entries.count) entries")
|
||||
try await transfer.writeData(encoded, to: manifestPath)
|
||||
log.info("Manifest written — \(entries.count) new entries")
|
||||
} catch {
|
||||
log.error("Manifest write failed (non-fatal): \(error.localizedDescription)")
|
||||
}
|
||||
@@ -322,17 +341,14 @@ final class BackupStatusService: NSObject, ObservableObject {
|
||||
// MARK: — PHPhotoLibraryChangeObserver
|
||||
|
||||
extension BackupStatusService: PHPhotoLibraryChangeObserver {
|
||||
/// Called on an arbitrary background thread by Photos.
|
||||
/// Debounced: schedules a refresh after a short delay, cancels any pending debounce.
|
||||
nonisolated func photoLibraryDidChange(_ changeInstance: PHChange) {
|
||||
Task { @MainActor [weak self] in
|
||||
guard let self else { return }
|
||||
// Cancel pending debounce and start a new one
|
||||
self.debounceTask?.cancel()
|
||||
self.debounceTask = Task {
|
||||
do {
|
||||
try await Task.sleep(nanoseconds: UInt64(BackupStatusService.debounceInterval * 1_000_000_000))
|
||||
self.refresh() // non-forced: respects minRefreshInterval
|
||||
self.refresh()
|
||||
} catch {
|
||||
// Cancelled — a newer change event took over
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user