Only pass key and not value to LRUCache.removeAll(where:)

The value isn’t needed here and the calls become cleaner if only the key is passed.
This commit is contained in:
Alex Hoppen
2025-04-23 18:40:56 +02:00
parent bc4d9c78e7
commit d8aeeaffe6
5 changed files with 10 additions and 10 deletions

View File

@@ -41,15 +41,15 @@ package struct LRUCache<Key: Hashable, Value> {
/// A collection containing just the keys of the cache.
///
/// - Note: Keys will **not** be in the same order that they were added to the cache.
package var keys: any Collection<Key> { cache.keys }
package var keys: some Collection<Key> { cache.keys }
/// A collection containing just the values of the cache.
///
/// - Note: Values will **not** be in the same order that they were added to the cache.
package var values: any Collection<Value> { cache.values }
package var values: some Collection<Value> { cache.values }
package init(capacity: Int) {
assert(capacity > 0, "LRUCache capacity must be greater than 0")
precondition(capacity > 0, "LRUCache capacity must be greater than 0")
self.capacity = capacity
self.cache = Dictionary(minimumCapacity: capacity)
self.priorities = Dictionary(minimumCapacity: capacity)
@@ -101,9 +101,9 @@ package struct LRUCache<Key: Hashable, Value> {
}
/// Removes all the elements that satisfy the given predicate.
package mutating func removeAll(where shouldBeRemoved: (_: ((key: Key, value: Value)) throws -> Bool)) rethrows {
package mutating func removeAll(where shouldBeRemoved: (_ key: Key) throws -> Bool) rethrows {
cache = try cache.filter { entry in
guard try shouldBeRemoved(entry) else {
guard try shouldBeRemoved(entry.key) else {
return true
}
removePriority(forKey: entry.key)

View File

@@ -95,7 +95,7 @@ actor DiagnosticReportManager {
}
func removeItemsFromCache(with uri: DocumentURI) async {
reportTaskCache.removeAll(where: { $0.key.snapshotID.uri == uri })
reportTaskCache.removeAll(where: { $0.snapshotID.uri == uri })
}
private func requestReport(
@@ -193,7 +193,7 @@ actor DiagnosticReportManager {
reportTask: ReportTask
) {
// Remove any reportTasks for old versions of this document.
reportTaskCache.removeAll(where: { $0.key.snapshotID <= snapshotID })
reportTaskCache.removeAll(where: { $0.snapshotID <= snapshotID })
reportTaskCache[CacheKey(snapshotID: snapshotID, buildSettings: buildSettings)] = reportTask
}
}

View File

@@ -121,7 +121,7 @@ actor MacroExpansionManager {
/// Remove all cached macro expansions for the given primary file, eg. because the macro's plugin might have changed.
func purge(primaryFile: DocumentURI) {
cache.removeAll {
$0.key.snapshotID.uri.primaryFile ?? $0.key.snapshotID.uri == primaryFile
$0.snapshotID.uri.primaryFile ?? $0.snapshotID.uri == primaryFile
}
}
}

View File

@@ -45,7 +45,7 @@ actor SyntaxTreeManager {
/// Set the task that computes the syntax tree for the given document snapshot.
private func setComputation(for snapshotID: DocumentSnapshot.ID, computation: SyntaxTreeComputation) {
// Remove any syntax trees for old versions of this document.
syntaxTreeComputations.removeAll(where: { key, value in key < snapshotID })
syntaxTreeComputations.removeAll(where: { $0 < snapshotID })
syntaxTreeComputations[snapshotID] = computation
}

View File

@@ -81,7 +81,7 @@ final class LRUCacheTests: XCTestCase {
}
// Remove all even keys
lruCache.removeAll(where: { $0.key % 2 == 0 })
lruCache.removeAll(where: { $0 % 2 == 0 })
assertLRUCacheKeys(lruCache, expectedKeys: [1, 3, 5, 7, 9, 11, 13, 15, 17, 19])
// Remove all key-value pairs