Skip to content

Commit 2af8f6c

Browse files
committed
Improve performance of type-safe effect cancel token lookup (#1077)
* Add type discriminator to effect cancel id hashing * wip
1 parent 8d19735 commit 2af8f6c

File tree

1 file changed

+13
-2
lines changed

1 file changed

+13
-2
lines changed

Sources/ComposableArchitecture/Effects/Cancellation.swift

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,7 @@ extension Effect {
4343
cancellablesLock.lock()
4444
defer { cancellablesLock.unlock() }
4545

46+
let id = CancelToken(id: id)
4647
if cancelInFlight {
4748
cancellationCancellables[id]?.forEach { $0.dispose() }
4849
}
@@ -96,7 +97,7 @@ extension Effect {
9697
public static func cancel(id: AnyHashable) -> Effect {
9798
return .fireAndForget {
9899
cancellablesLock.sync {
99-
cancellationCancellables[id]?.forEach { $0.dispose() }
100+
cancellationCancellables[.init(id: id)]?.forEach { $0.dispose() }
100101
}
101102
}
102103
}
@@ -111,5 +112,15 @@ extension Effect {
111112
}
112113
}
113114

114-
var cancellationCancellables: [AnyHashable: Set<AnyDisposable>] = [:]
115+
struct CancelToken: Hashable {
116+
let id: AnyHashable
117+
let discriminator: ObjectIdentifier
118+
119+
init(id: AnyHashable) {
120+
self.id = id
121+
self.discriminator = ObjectIdentifier(type(of: id.base))
122+
}
123+
}
124+
125+
var cancellationCancellables: [CancelToken: Set<AnyDisposable>] = [:]
115126
let cancellablesLock = NSRecursiveLock()

0 commit comments

Comments
 (0)