Skip to content

Commit d8aeeaf

Browse files
committed
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.
1 parent bc4d9c7 commit d8aeeaf

File tree

5 files changed

+10
-10
lines changed

5 files changed

+10
-10
lines changed

Sources/SKUtilities/LRUCache.swift

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -41,15 +41,15 @@ package struct LRUCache<Key: Hashable, Value> {
4141
/// A collection containing just the keys of the cache.
4242
///
4343
/// - Note: Keys will **not** be in the same order that they were added to the cache.
44-
package var keys: any Collection<Key> { cache.keys }
44+
package var keys: some Collection<Key> { cache.keys }
4545

4646
/// A collection containing just the values of the cache.
4747
///
4848
/// - Note: Values will **not** be in the same order that they were added to the cache.
49-
package var values: any Collection<Value> { cache.values }
49+
package var values: some Collection<Value> { cache.values }
5050

5151
package init(capacity: Int) {
52-
assert(capacity > 0, "LRUCache capacity must be greater than 0")
52+
precondition(capacity > 0, "LRUCache capacity must be greater than 0")
5353
self.capacity = capacity
5454
self.cache = Dictionary(minimumCapacity: capacity)
5555
self.priorities = Dictionary(minimumCapacity: capacity)
@@ -101,9 +101,9 @@ package struct LRUCache<Key: Hashable, Value> {
101101
}
102102

103103
/// Removes all the elements that satisfy the given predicate.
104-
package mutating func removeAll(where shouldBeRemoved: (_: ((key: Key, value: Value)) throws -> Bool)) rethrows {
104+
package mutating func removeAll(where shouldBeRemoved: (_ key: Key) throws -> Bool) rethrows {
105105
cache = try cache.filter { entry in
106-
guard try shouldBeRemoved(entry) else {
106+
guard try shouldBeRemoved(entry.key) else {
107107
return true
108108
}
109109
removePriority(forKey: entry.key)

Sources/SourceKitLSP/Swift/DiagnosticReportManager.swift

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -95,7 +95,7 @@ actor DiagnosticReportManager {
9595
}
9696

9797
func removeItemsFromCache(with uri: DocumentURI) async {
98-
reportTaskCache.removeAll(where: { $0.key.snapshotID.uri == uri })
98+
reportTaskCache.removeAll(where: { $0.snapshotID.uri == uri })
9999
}
100100

101101
private func requestReport(
@@ -193,7 +193,7 @@ actor DiagnosticReportManager {
193193
reportTask: ReportTask
194194
) {
195195
// Remove any reportTasks for old versions of this document.
196-
reportTaskCache.removeAll(where: { $0.key.snapshotID <= snapshotID })
196+
reportTaskCache.removeAll(where: { $0.snapshotID <= snapshotID })
197197
reportTaskCache[CacheKey(snapshotID: snapshotID, buildSettings: buildSettings)] = reportTask
198198
}
199199
}

Sources/SourceKitLSP/Swift/MacroExpansion.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -121,7 +121,7 @@ actor MacroExpansionManager {
121121
/// Remove all cached macro expansions for the given primary file, eg. because the macro's plugin might have changed.
122122
func purge(primaryFile: DocumentURI) {
123123
cache.removeAll {
124-
$0.key.snapshotID.uri.primaryFile ?? $0.key.snapshotID.uri == primaryFile
124+
$0.snapshotID.uri.primaryFile ?? $0.snapshotID.uri == primaryFile
125125
}
126126
}
127127
}

Sources/SourceKitLSP/Swift/SyntaxTreeManager.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@ actor SyntaxTreeManager {
4545
/// Set the task that computes the syntax tree for the given document snapshot.
4646
private func setComputation(for snapshotID: DocumentSnapshot.ID, computation: SyntaxTreeComputation) {
4747
// Remove any syntax trees for old versions of this document.
48-
syntaxTreeComputations.removeAll(where: { key, value in key < snapshotID })
48+
syntaxTreeComputations.removeAll(where: { $0 < snapshotID })
4949
syntaxTreeComputations[snapshotID] = computation
5050
}
5151

Tests/SKUtilitiesTests/LRUCacheTests.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -81,7 +81,7 @@ final class LRUCacheTests: XCTestCase {
8181
}
8282

8383
// Remove all even keys
84-
lruCache.removeAll(where: { $0.key % 2 == 0 })
84+
lruCache.removeAll(where: { $0 % 2 == 0 })
8585
assertLRUCacheKeys(lruCache, expectedKeys: [1, 3, 5, 7, 9, 11, 13, 15, 17, 19])
8686

8787
// Remove all key-value pairs

0 commit comments

Comments
 (0)