Skip to content

[Predicate] Use a global atomic counter for VariableID #434

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Feb 22, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 13 additions & 0 deletions Sources/FoundationEssentials/Predicate/Expression.swift
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,11 @@
//
//===----------------------------------------------------------------------===//

#if FOUNDATION_FRAMEWORK
@_implementationOnly
import Synchronization
#endif

@available(FoundationPredicate 0.1, *)
public protocol PredicateExpression<Output> {
associatedtype Output
Expand Down Expand Up @@ -81,15 +86,23 @@ public struct PredicateError: Error, Hashable, CustomDebugStringConvertible {
extension PredicateExpressions {
public struct VariableID: Hashable, Codable, Sendable {
let id: UInt
#if FOUNDATION_FRAMEWORK
private static let nextID = Atomic<UInt>(0)
#else
private static let nextID = LockedState(initialState: UInt(0))
#endif

init() {
#if FOUNDATION_FRAMEWORK
self.id = Self.nextID.wrappingAdd(1, ordering: .relaxed).oldValue
#else
self.id = Self.nextID.withLock { value in
defer {
(value, _) = value.addingReportingOverflow(1)
}
return value
}
#endif
}

public func encode(to encoder: Encoder) throws {
Expand Down