Uninstall Mac apps with every file they leave behind, and see why each belongs. Free up space, fine-tune macOS, and set up Terminal.
Swift cache-cleanercleanerclideveloper-toolsdisk-cleanupdisk-spaceduplicate-finderhomebrewliquid-glassmac-cleanermacosmacos-appmacos-golden-gatemacos-tahoemacos-tweaksswiftswiftuiterminal-themesuninstalluninstaller
A search measures a few folders of one place at a time
Țugui Dragoș902656d8dc1d
diff --git a/Packages/PeelCore/Sources/PeelCore/Leftovers/LeftoverScanner.swift b/Packages/PeelCore/Sources/PeelCore/Leftovers/LeftoverScanner.swiftindex 70daad0a..1653ee2c 100644--- a/Packages/PeelCore/Sources/PeelCore/Leftovers/LeftoverScanner.swift+++ b/Packages/PeelCore/Sources/PeelCore/Leftovers/LeftoverScanner.swift@@ -164,7 +164,7 @@ public struct LeftoverScanner: Sendable {matcher: LeftoverMatcher,home: String,bundle: String,- measure: Measure,+ measure: @escaping Measure,refuses: Refuses,nestedFolderLimit: Int) async -> LocationResult {@@ -181,7 +181,7 @@ public struct LeftoverScanner: Sendable {}let parent = ParentAccess(location.url)- var leftovers: [Leftover] = []+ var toMeasure: [Found] = []var nobodysFolders: [(url: URL, isAnotherApps: Bool)] = []// The guard reads every spelling of a path from the disk, so it is asked only about what the scan would@@ -193,13 +193,14 @@ public struct LeftoverScanner: Sendable {let path = url.path(percentEncoded: false)if let match = claim(name, at: url, kind: location.kind, matcher: matcher, bundle: bundle), !isSharedWithTheWholeMac(url, home: home) {guard !refuses(path, home) else { continue }- leftovers.append(await leftover(at: url, kind: location.kind, match: match, parent: parent, home: home, measure: measure))+ toMeasure.append(Found(url: url, match: match, parent: parent, isInsideAnotherAppsFolder: false))} else if nestedKinds.contains(location.kind), url.isRealFolder, !refuses(path, home) {// Not this app's. Whether it is somebody else's decides what a name inside it is worth.nobodysFolders.append((url, isSomebodyElses(name, kind: location.kind, matcher: matcher)))}}+ let leftovers = await measured(toMeasure, kind: location.kind, home: home, measure: measure)let inside = await nested(in: nobodysFolders, kind: location.kind, matcher: matcher, home: home, measure: measure, refuses: refuses,limit: nestedFolderLimit@@ -217,11 +218,11 @@ public struct LeftoverScanner: Sendable {kind: SearchLocation.Kind,matcher: LeftoverMatcher,home: String,- measure: Measure,+ measure: @escaping Measure,refuses: Refuses,limit: Int) async -> (found: [Leftover], wasCutShort: Bool, unreadable: [URL]) {- var found: [Leftover] = []+ var found: [Found] = []var unreadable: [URL] = []var pending = foldersvar visited = 0@@ -229,8 +230,8 @@ public struct LeftoverScanner: Sendable {for depth in 0..<nestedDepth {var deeper: [(url: URL, isAnotherApps: Bool)] = []for (folder, isAnotherApps) in pending {- guard !Task.isCancelled else { return (found, false, unreadable) }- guard visited < limit else { return (found, true, unreadable) }+ guard !Task.isCancelled else { return (await measured(found, kind: kind, home: home, measure: measure), false, unreadable) }+ guard visited < limit else { return (await measured(found, kind: kind, home: home, measure: measure), true, unreadable) }visited += 1let names: [String]do {@@ -255,7 +256,7 @@ public struct LeftoverScanner: Sendable {if let match = Self.match(name, at: url, kind: kind, matcher: matcher), match.confidence >= .likely,!isSharedWithTheWholeMac(url, home: home) {guard !refuses(path, home) else { continue }- found.append(await leftover(at: url, kind: kind, match: match, parent: parent, home: home, isInsideAnotherAppsFolder: isAnotherApps, measure: measure))+ found.append(Found(url: url, match: match, parent: parent, isInsideAnotherAppsFolder: isAnotherApps))} else if depth + 1 < nestedDepth, url.isRealFolder, !refuses(path, home) {deeper.append((url, isAnotherApps || isSomebodyElses(name, kind: kind, matcher: matcher)))}@@ -263,7 +264,31 @@ public struct LeftoverScanner: Sendable {}pending = deeper}- return (found, false, unreadable)+ return (await measured(found, kind: kind, home: home, measure: measure), false, unreadable)+ }++ /// Something a search took as the app's, waiting to be measured.+ private struct Found: Sendable {+ let url: URL+ let match: LeftoverMatch+ let parent: ParentAccess+ let isInsideAnotherAppsFolder: Bool+ }++ /// How many items of one location are measured at once. One after another, a few folders that never answer+ /// would each hold the location for the whole budget of `FileSize`.+ static let concurrentMeasurements = 4++ /// Measures what one location found, a few at a time, and keeps the order it was found in.+ private static func measured(+ _ found: [Found], kind: SearchLocation.Kind, home: String, measure: @escaping Measure+ ) async -> [Leftover] {+ await found.concurrentMap(width: concurrentMeasurements) { item in+ await leftover(+ at: item.url, kind: kind, match: item.match, parent: item.parent, home: home,+ isInsideAnotherAppsFolder: item.isInsideAnotherAppsFolder, measure: measure+ )+ }}/// Builds a leftover: measures it and decides whether to hold it back. The scan, cask paths, and installerdiff --git a/Packages/PeelCore/Sources/PeelCore/Orphans/OrphanScanner.swift b/Packages/PeelCore/Sources/PeelCore/Orphans/OrphanScanner.swiftindex 10d14e84..960dfbdd 100644--- a/Packages/PeelCore/Sources/PeelCore/Orphans/OrphanScanner.swift+++ b/Packages/PeelCore/Sources/PeelCore/Orphans/OrphanScanner.swift@@ -301,7 +301,7 @@ public struct OrphanScanner: Sendable {goneApps: [String],goneNames: [String: String],home: String,- walk: LeftoverScanner.Measure+ walk: @escaping LeftoverScanner.Measure) async -> LocationResult {let entries: [String]do {@@ -314,7 +314,7 @@ public struct OrphanScanner: Sendable {}let parent = ParentAccess(location.url)- var found: [(identifier: String, item: OrphanItem)] = []+ var candidates: [(url: URL, identifier: String, namedAfter: String?)] = []for name in entries where location.kind.considers(fileName: name) {guard !Task.isCancelled else { break }let url = location.url.appending(path: name)@@ -329,11 +329,16 @@ public struct OrphanScanner: Sendable {}guard let identifier else { continue }guard !location.kind.isLoadedCode || cameWithAnAppThatLeft(identifier, goneApps: goneApps) else { continue }+ candidates.append((url, identifier, namedAfter))+ }+ // A few walks at a time, so a few folders that never answer do not each hold the location for a whole budget.+ let walked = await candidates.map(\.url).concurrentMap(width: LeftoverScanner.concurrentMeasurements) { await walk($0) }+ var found: [(identifier: String, item: OrphanItem)] = []+ for ((url, identifier, namedAfter), contents) in zip(candidates, walked) {// The item's own date changes when something is taken out of it, but not when a file inside is// rewritten in place, so the newest date inside comes from the walk.let own = try? url.resourceValues(forKeys: [.contentModificationDateKey]).contentModificationDate- let contents = await walk(url)let path = url.path(percentEncoded: false)let heldBack: HoldBack? = if location.kind == .containers, ProtectedData.holdsAContainersDocuments(path) {.holdsDocumentsdiff --git a/Packages/PeelCore/Sources/PeelCore/Support/ConcurrentMap.swift b/Packages/PeelCore/Sources/PeelCore/Support/ConcurrentMap.swiftnew file mode 100644index 00000000..35009ecc--- /dev/null+++ b/Packages/PeelCore/Sources/PeelCore/Support/ConcurrentMap.swift@@ -0,0 +1,25 @@+extension Array where Element: Sendable {+ /// `transform` of every element, at most `width` at a time, in the elements' order. A few slow elements then+ /// hold only their own part of the wait, never the whole list's. Once the task is canceled nothing more starts,+ /// and only the results of the elements already started come back.+ func concurrentMap<Result: Sendable>(+ width: Int,+ _ transform: @escaping @Sendable (Element) async -> Result+ ) async -> [Result] {+ await withTaskGroup(of: (Int, Result).self) { group in+ var pending = enumerated().makeIterator()+ func addNext() -> Bool {+ guard !Task.isCancelled, let (index, element) = pending.next() else { return false }+ group.addTask { (index, await transform(element)) }+ return true+ }+ for _ in 0..<width where addNext() {}+ var results = [Result?](repeating: nil, count: count)+ while let (index, result) = await group.next() {+ results[index] = result+ _ = addNext()+ }+ return results.compactMap(\.self)+ }+ }+}diff --git a/Packages/PeelCore/Tests/PeelCoreTests/ConcurrentMapTests.swift b/Packages/PeelCore/Tests/PeelCoreTests/ConcurrentMapTests.swiftnew file mode 100644index 00000000..b5828212--- /dev/null+++ b/Packages/PeelCore/Tests/PeelCoreTests/ConcurrentMapTests.swift@@ -0,0 +1,35 @@+import Foundation+@testable import PeelCore+import Synchronization+import Testing++struct ConcurrentMapTests {+ @Test func keepsTheOrderAndRunsAFewAtATime() async {+ let running = Mutex((now: 0, most: 0))++ let results = await Array(0..<8).concurrentMap(width: 4) { number in+ running.withLock { $0.now += 1; $0.most = max($0.most, $0.now) }+ try? await Task.sleep(for: .milliseconds(500))+ running.withLock { $0.now -= 1 }+ return number * 10+ }++ #expect(results == [0, 10, 20, 30, 40, 50, 60, 70])+ #expect(running.withLock { $0.most } == 4)+ }++ @Test func startsNothingMoreOnceCanceled() async {+ let started = Mutex(0)+ let mapping = Task {+ await Array(0..<20).concurrentMap(width: 2) { _ in+ started.withLock { $0 += 1 }+ while !Task.isCancelled { await Task.yield() }+ }+ }+ while started.withLock({ $0 }) == 0 { await Task.yield() }+ mapping.cancel()+ _ = await mapping.value++ #expect(started.withLock { $0 } <= 2)+ }+}diff --git a/Packages/PeelCore/Tests/PeelCoreTests/LeftoverScannerTests.swift b/Packages/PeelCore/Tests/PeelCoreTests/LeftoverScannerTests.swiftindex 4e983872..47a86ffd 100644--- a/Packages/PeelCore/Tests/PeelCoreTests/LeftoverScannerTests.swift+++ b/Packages/PeelCore/Tests/PeelCoreTests/LeftoverScannerTests.swift@@ -277,8 +277,8 @@ struct LeftoverScannerTests {#expect(byPath["Nobody/Tunewell"]?.match.isRecommended == true, "a folder no installed app answers to is nobody's")}- /// Leaving a page cancels its scan, and a canceled scan stops measuring. Otherwise, moving through a list of- /// apps would leave a scan running for every app passed.+ /// Leaving a page cancels its scan, and a canceled scan starts no other measurement, so only those already under+ /// way can have begun. Otherwise, moving through a list of apps would leave a scan running for every app passed.@Test func aScanThatWasCanceledStopsMeasuring() async throws {let directory = try TemporaryDirectory()for index in 0..<40 {@@ -297,7 +297,7 @@ struct LeftoverScannerTests {scan.cancel()_ = await scan.value- #expect(measured.withLock { $0 } == 1, "it went on measuring after it was canceled")+ #expect(measured.withLock { $0 } <= LeftoverScanner.concurrentMeasurements, "it went on measuring after it was canceled")}/// An app can keep a whole photo, music, or video library beside its settings. `RemovalGuard` refuses to