Skip to content

Commit 66ece21

Browse files
committed
Add a version of Prims that uses Unmanaged._withUnsafeGuaranteedRef.
1 parent 7c5f4d8 commit 66ece21

File tree

1 file changed

+108
-0
lines changed

1 file changed

+108
-0
lines changed

benchmark/single-source/PrimsNonStrongRef.swift

Lines changed: 108 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -101,6 +101,22 @@ public let PrimsNonStrongRef: [BenchmarkInfo] = ({
101101
touchGlobalInfo()
102102
blackHole(unmanagedPrimsState)
103103
}))
104+
benchmarks.append(BenchmarkInfo(
105+
name: "Prims.NonStrongRef.UnmanagedUGR",
106+
runFunction: run_PrimsUnmanagedUGR,
107+
tags: [.validation, .algorithm, .api],
108+
setUpFunction: {
109+
touchGlobalInfo()
110+
blackHole(unmanagedUGRPrimsState)
111+
}))
112+
benchmarks.append(BenchmarkInfo(
113+
name: "Prims.NonStrongRef.UnmanagedUGR.ClosureAccess",
114+
runFunction: run_PrimsUnmanagedUGRClosureAccess,
115+
tags: [.validation, .algorithm, .api],
116+
setUpFunction: {
117+
touchGlobalInfo()
118+
blackHole(unmanagedUGRPrimsState)
119+
}))
104120
return benchmarks
105121
})()
106122

@@ -650,6 +666,7 @@ let weakPrimsState = PrimsState(WeakGraphNode.self)
650666
let unownedSafePrimsState = PrimsState(UnownedSafeGraphNode.self)
651667
let unownedUnsafePrimsState = PrimsState(UnownedUnsafeGraphNode.self)
652668
let unmanagedPrimsState = PrimsState(UnmanagedGraphNode.self)
669+
let unmanagedUGRPrimsState = PrimsState(UnmanagedUGRGraphNode.self)
653670

654671
//===----------------------------------------------------------------------===//
655672
// Protocols
@@ -765,6 +782,46 @@ extension UnmanagedVarBox : Hashable where T : Hashable {
765782
}
766783
}
767784

785+
struct UnmanagedUGRVarBox<T : AnyObject & Hashable> {
786+
var _value: Unmanaged<T>
787+
788+
init(_ inputValue: T) {
789+
_value = Unmanaged<T>.passRetained(inputValue)
790+
}
791+
}
792+
793+
extension UnmanagedUGRVarBox : ValueBox where T : GraphNode {
794+
typealias ValueType = T
795+
796+
func free() {
797+
_value.release()
798+
}
799+
800+
var value: T { return _value._withUnsafeGuaranteedRef { $0 } }
801+
802+
func withValue<Result>(_ f: (ValueType) throws -> Result) rethrows -> Result {
803+
try _value._withUnsafeGuaranteedRef { try f($0) }
804+
}
805+
}
806+
807+
extension UnmanagedUGRVarBox : Equatable where T : Equatable {
808+
}
809+
810+
func ==<T>(lhs: UnmanagedUGRVarBox<T>, rhs: UnmanagedUGRVarBox<T>) -> Bool {
811+
return lhs._value._withUnsafeGuaranteedRef { x in
812+
return rhs._value._withUnsafeGuaranteedRef { y in
813+
return x == y
814+
}}
815+
}
816+
817+
extension UnmanagedUGRVarBox : Hashable where T : Hashable {
818+
func hash(into hasher: inout Hasher) {
819+
_value._withUnsafeGuaranteedRef {
820+
hasher.combine(ObjectIdentifier($0))
821+
}
822+
}
823+
}
824+
768825
//===----------------------------------------------------------------------===//
769826
// Graph Node Implementations
770827
//===----------------------------------------------------------------------===//
@@ -887,6 +944,41 @@ func ==(lhs: UnmanagedGraphNode, rhs: UnmanagedGraphNode) -> Bool {
887944
return lhs === rhs
888945
}
889946

947+
948+
final class UnmanagedUGRGraphNode {
949+
/// This id is only meant for dumping the state of the graph. It is not meant
950+
/// to be used functionally by the algorithm.
951+
var id: Int
952+
953+
var adjList: Array<UnmanagedUGRVarBox<UnmanagedUGRGraphNode>>
954+
955+
init(id inputId: Int) {
956+
id = inputId
957+
adjList = Array<UnmanagedUGRVarBox<UnmanagedUGRGraphNode>>()
958+
}
959+
960+
deinit {
961+
for x in adjList {
962+
x.free()
963+
}
964+
}
965+
}
966+
967+
extension UnmanagedUGRGraphNode : GraphNode {
968+
typealias BoxType = UnmanagedUGRVarBox<UnmanagedUGRGraphNode>
969+
}
970+
971+
extension UnmanagedUGRGraphNode : Equatable {}
972+
extension UnmanagedUGRGraphNode : Hashable {
973+
func hash(into hasher: inout Hasher) {
974+
hasher.combine(ObjectIdentifier(self))
975+
}
976+
}
977+
978+
func ==(lhs: UnmanagedUGRGraphNode, rhs: UnmanagedUGRGraphNode) -> Bool {
979+
return lhs === rhs
980+
}
981+
890982
//===----------------------------------------------------------------------===//
891983
// Edge Implementation
892984
//===----------------------------------------------------------------------===//
@@ -1236,3 +1328,19 @@ public func run_PrimsUnmanagedClosureAccess(_ N: Int) {
12361328
run_PrimsNonStrongRefClosureAccess(state)
12371329
}
12381330
}
1331+
1332+
@inline(never)
1333+
public func run_PrimsUnmanagedUGR(_ N: Int) {
1334+
let state = unmanagedUGRPrimsState
1335+
for _ in 0..<N {
1336+
run_PrimsNonStrongRef(state)
1337+
}
1338+
}
1339+
1340+
@inline(never)
1341+
public func run_PrimsUnmanagedUGRClosureAccess(_ N: Int) {
1342+
let state = unmanagedUGRPrimsState
1343+
for _ in 0..<N {
1344+
run_PrimsNonStrongRefClosureAccess(state)
1345+
}
1346+
}

0 commit comments

Comments
 (0)