Skip to content

Commit c63db6e

Browse files
Added a raw UUID comparison thats always available
1 parent f49a7f1 commit c63db6e

File tree

1 file changed

+23
-25
lines changed

1 file changed

+23
-25
lines changed

Sources/CodableDatastore/Indexes/UUID+Comparable.swift

Lines changed: 23 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -15,44 +15,42 @@ extension UUID: @retroactive Comparable {
1515
@inlinable
1616
@_disfavoredOverload
1717
public static func < (lhs: UUID, rhs: UUID) -> Bool {
18-
lhs.isLessThan(rhs: rhs)
18+
lhs.uuid < rhs.uuid
1919
}
2020
}
2121
#else
2222
extension UUID: Comparable {
2323
@inlinable
2424
@_disfavoredOverload
2525
public static func < (lhs: UUID, rhs: UUID) -> Bool {
26-
lhs.isLessThan(rhs: rhs)
26+
lhs.uuid < rhs.uuid
2727
}
2828
}
2929
#endif
3030
#endif
3131

32-
extension UUID {
33-
/// Make UUIDs comparable, so that they can be used transparently as an index.
34-
///
35-
/// - SeeAlso: https://github.com/apple/swift-foundation/blob/5388acf1d929865d4df97d3c50e4d08bc4c6bdf0/Sources/FoundationEssentials/UUID.swift#L135-L156
36-
@usableFromInline
37-
func isLessThan(rhs: UUID) -> Bool {
38-
var leftUUID = self.uuid
39-
var rightUUID = rhs.uuid
40-
var result: Int = 0
41-
var diff: Int = 0
42-
withUnsafeBytes(of: &leftUUID) { leftPtr in
43-
withUnsafeBytes(of: &rightUUID) { rightPtr in
44-
for offset in (0 ..< MemoryLayout<uuid_t>.size).reversed() {
45-
diff = Int(leftPtr.load(fromByteOffset: offset, as: UInt8.self)) -
46-
Int(rightPtr.load(fromByteOffset: offset, as: UInt8.self))
47-
// Constant time, no branching equivalent of
48-
// if (diff != 0) {
49-
// result = diff;
50-
// }
51-
result = (result & (((diff - 1) & ~diff) >> 8)) | diff
52-
}
32+
/// Make UUIDs comparable, so that they can be used transparently as an index.
33+
///
34+
/// - SeeAlso: https://github.com/apple/swift-foundation/blob/5388acf1d929865d4df97d3c50e4d08bc4c6bdf0/Sources/FoundationEssentials/UUID.swift#L135-L156
35+
@inlinable
36+
public func < (lhs: uuid_t, rhs: uuid_t) -> Bool {
37+
var lhs = lhs
38+
var rhs = rhs
39+
var result: Int = 0
40+
var diff: Int = 0
41+
withUnsafeBytes(of: &lhs) { leftPtr in
42+
withUnsafeBytes(of: &rhs) { rightPtr in
43+
for offset in (0 ..< MemoryLayout<uuid_t>.size).reversed() {
44+
diff = Int(leftPtr.load(fromByteOffset: offset, as: UInt8.self)) -
45+
Int(rightPtr.load(fromByteOffset: offset, as: UInt8.self))
46+
// Constant time, no branching equivalent of
47+
// if (diff != 0) {
48+
// result = diff;
49+
// }
50+
result = (result & (((diff - 1) & ~diff) >> 8)) | diff
5351
}
5452
}
55-
56-
return result < 0
5753
}
54+
55+
return result < 0
5856
}

0 commit comments

Comments
 (0)