Skip to content

Commit

Permalink
adopt swift-atomics
Browse files Browse the repository at this point in the history
  • Loading branch information
ddddxxx committed Jul 4, 2021
1 parent f9218de commit a995fa6
Show file tree
Hide file tree
Showing 6 changed files with 59 additions and 3 deletions.
9 changes: 9 additions & 0 deletions Package.resolved
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,15 @@
"revision": "6094e6f23a02b52b5d211fd114a4750c4f3ecef3",
"version": "0.2.1"
}
},
{
"package": "swift-atomics",
"repositoryURL": "https://github.com/apple/swift-atomics.git",
"state": {
"branch": null,
"revision": "3e95ba32cd1b4c877f6163e8eea54afc4e63bf9f",
"version": "0.0.3"
}
}
]
},
Expand Down
3 changes: 3 additions & 0 deletions Package.swift
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,9 @@ let package = Package(
products: [
.library(name: "CombineX", targets: ["CombineX", "CXFoundation"]),
],
dependencies: [
// TODO: use swift-atomics which requires swift 5.1
],
targets: [
.target(name: "CXUtility"),
.target(name: "CombineX", dependencies: ["CXUtility"]),
Expand Down
12 changes: 10 additions & 2 deletions Package@swift-5.2.swift
Original file line number Diff line number Diff line change
Expand Up @@ -15,14 +15,22 @@ let package = Package(
.library(name: "CombineX", targets: ["CombineX", "CXFoundation"]),
],
dependencies: [
.package(url: "https://github.com/apple/swift-atomics.git", .upToNextMinor(from: "0.0.3")),
.package(url: "https://github.com/Quick/Quick.git", from: "3.0.0"),
.package(url: "https://github.com/Quick/Nimble.git", from: "9.0.0"),
.package(url: "https://github.com/ddddxxx/Semver.git", .upToNextMinor(from: "0.2.1")),
],
targets: [
.target(name: "CXUtility"),
.target(name: "CombineX", dependencies: ["CXUtility"]),
.target(name: "CXFoundation", dependencies: ["CXUtility", "CombineX"]),
.target(
name: "CombineX",
dependencies: [
"CXUtility",
.product(name: "Atomics", package: "swift-atomics")],
swiftSettings: [.define("CX_LOCK_FREE_ATOMIC")]),
.target(
name: "CXFoundation",
dependencies: ["CXUtility", "CombineX"]),

// We have circular dependency CombineXTests -> CXTest -> CXShim -> CombineX
//
Expand Down
8 changes: 8 additions & 0 deletions Sources/CXTestUtility/Common.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
@inline(never)
public func blackHole<T>(_ x: T) {
}

@inline(never)
public func identity<T>(_ x: T) -> T {
return x
}
10 changes: 9 additions & 1 deletion Sources/CombineX/CombineIdentifier.swift
Original file line number Diff line number Diff line change
@@ -1,15 +1,23 @@
#if CX_LOCK_FREE_ATOMIC
@_implementationOnly import Atomics
private let counter = UnsafeAtomic<UInt64>.create(0)
#else
#if !COCOAPODS
import CXUtility
#endif

private let counter = LockedAtomic<UInt64>(0)
#endif

public struct CombineIdentifier: Hashable, CustomStringConvertible {

private let value: UInt64

public init() {
#if CX_LOCK_FREE_ATOMIC
self.value = counter.loadThenWrappingIncrement(ordering: .relaxed)
#else
self.value = counter.loadThenWrappingIncrement()
#endif
}

public init(_ obj: AnyObject) {
Expand Down
20 changes: 20 additions & 0 deletions Tests/CombineXTests/CombineIdentifierSpec.swift
Original file line number Diff line number Diff line change
Expand Up @@ -39,3 +39,23 @@ class CombineIdentifierSpec: QuickSpec {
}
}
}

import XCTest

class CombineIdentifierTests: XCTestCase {

func testPerformance() {
measure {
let g = DispatchGroup()
for _ in 0..<1000 {
DispatchQueue.global().async(group: g) {
for _ in 0..<1000 {
blackHole(CombineIdentifier())
}
}
}
g.wait()
}
}
}

0 comments on commit a995fa6

Please sign in to comment.