Skip to content

Commit e89eb31

Browse files
authored
[Observation] Optimize cancellation path to avoid excessive copies (#73288) (#73356)
1 parent 459fc38 commit e89eb31

File tree

1 file changed

+4
-6
lines changed

1 file changed

+4
-6
lines changed

stdlib/public/Observation/Sources/Observation/ObservationRegistrar.swift

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -163,12 +163,10 @@ public struct ObservationRegistrar: Sendable {
163163
internal mutating func cancel(_ id: Int) {
164164
if let observation = observations.removeValue(forKey: id) {
165165
for keyPath in observation.properties {
166-
if var ids = lookups[keyPath] {
167-
ids.remove(id)
168-
if ids.count == 0 {
169-
lookups.removeValue(forKey: keyPath)
170-
} else {
171-
lookups[keyPath] = ids
166+
if let index = lookups.index(forKey: keyPath) {
167+
lookups.values[index].remove(id)
168+
if lookups.values[index].isEmpty {
169+
lookups.remove(at: index)
172170
}
173171
}
174172
}

0 commit comments

Comments
 (0)