Skip to content

Commit db46428

Browse files
authored
[Predicate] Use a global atomic counter for VariableID (#434)
* Use a global atomic counter for VariableID * Make the synchronization import implementation only
1 parent 3e268af commit db46428

File tree

1 file changed

+13
-0
lines changed

1 file changed

+13
-0
lines changed

Sources/FoundationEssentials/Predicate/Expression.swift

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,11 @@
1010
//
1111
//===----------------------------------------------------------------------===//
1212

13+
#if FOUNDATION_FRAMEWORK
14+
@_implementationOnly
15+
import Synchronization
16+
#endif
17+
1318
@available(FoundationPredicate 0.1, *)
1419
public protocol PredicateExpression<Output> {
1520
associatedtype Output
@@ -81,15 +86,23 @@ public struct PredicateError: Error, Hashable, CustomDebugStringConvertible {
8186
extension PredicateExpressions {
8287
public struct VariableID: Hashable, Codable, Sendable {
8388
let id: UInt
89+
#if FOUNDATION_FRAMEWORK
90+
private static let nextID = Atomic<UInt>(0)
91+
#else
8492
private static let nextID = LockedState(initialState: UInt(0))
93+
#endif
8594

8695
init() {
96+
#if FOUNDATION_FRAMEWORK
97+
self.id = Self.nextID.wrappingAdd(1, ordering: .relaxed).oldValue
98+
#else
8799
self.id = Self.nextID.withLock { value in
88100
defer {
89101
(value, _) = value.addingReportingOverflow(1)
90102
}
91103
return value
92104
}
105+
#endif
93106
}
94107

95108
public func encode(to encoder: Encoder) throws {

0 commit comments

Comments
 (0)